Download or Print PDF file with Image in ASP.Net

Last Reply one month ago By dharmendr

Posted 2 months ago

Hello forum,

Is it possible to make an <img> tag control to stick on a PDF file in literal control?

In my project, when I navigate to the page where PDF file is displayed from the database by the use of a literal control, there is also an image control that has a QR code but when I download or print the PDF file, the QR code will not be on the downloaded or printed PDF file. So I want to make the QR code which is in image control and on the page to be part of the PDF when downloading or printing the file. Can this be possible please?

PDFprint.aspx (HTML)

<asp:Panel ID="printpanel" runat="server">
    <div class="contentt">
        <asp:Literal ID="ltEmbed" runat="server" />
        <asp:Image ID="Image1" runat="server" BorderStyle="None" Width="80px" Height="85px" />
    </div>
</asp:Panel>

Style.CSS

    <style type="text/css">
        .contentt {
            max-width: 100%;
            position: relative;
            margin: auto;
            height: auto;
            margin-top: 0%;
            margin-left: 2%;
        }
        #Image1 {
            position: absolute;
            left: 40px;
            bottom: 60px;
            cursor: move;
            background-color: #fafafa;
            border: solid 1px #00003D;
            font-size: 24px;
            text-align: center;
            backface-visibility:hidden;
            background:unset;
            height:90px;
            width:86px
        }

    </style>

 

protected void buttonmail_Click(object sender, EventArgs e)
{
    string fileName = "Invoice" + DateTime.Now.ToString() + ".pdf";
 
    byte[] QRBytes = GetQRCodeBytes(Server.MapPath("/PDFprint.aspx") + "?Id=" + Session["paperinv"]);
    Image1.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(QRBytes);
    var ImgUrl = Image1.ImageUrl;
    // Write image as file to folder.
    File.WriteAllBytes(Server.MapPath("qrimg.jpg"), QRBytes);
    // convert and set absolute url in image.
    Image1.ImageUrl = GetUrl("qrimg.jpg");
 
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    printpanel.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
    PdfWriter PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
    ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
    cssResolver.AddCssFile(Server.MapPath("~/css/style2.css"), true);
    IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, PdfWriter)));
    var worker = new XMLWorker(pipeline, true);
    var xmlParse = new XMLParser(true, worker);
    pdfDoc.Open();
    xmlParse.Parse(sr);
    xmlParse.Flush();
    pdfDoc.Close();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ";");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Write(pdfDoc);
    // Delete the temp image.
    File.Delete(Server.MapPath("qrimg.jpg"));
    Image1.ImageUrl = ImgUrl;
    Response.End();
}

 

You are viewing reply posted by: dharmendr one month ago.
Posted one month ago

You need to rebind the PDF file in Literal control on button click.

    protected void buttonmail_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Session["paperinv"]);
        // Rebinding PDF file in Literal control.
        string embed = "<object data=\"{0}{1}\" type=\"application/pdf\" width=\"400px\" height=\"400px\"></object>";
        ltEmbed.Text = string.Format(embed, ResolveUrl("Handler.ashx?Id="), id);
        
        string fileName = "Invoice" + DateTime.Now.ToString() + ".pdf";
 
        byte[] QRBytes = GetQRCodeBytes(Server.MapPath("/PDFprint.aspx") + "?Id=" + id);
        Image1.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(QRBytes);
        var ImgUrl = Image1.ImageUrl;
        // Write image as file to folder.
        File.WriteAllBytes(Server.MapPath("qrimg.jpg"), QRBytes);
        // convert and set absolute url in image.
        Image1.ImageUrl = GetUrl("qrimg.jpg");
 
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        printpanel.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
        PdfWriter PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
        htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
        ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
        cssResolver.AddCssFile(Server.MapPath("~/css/style2.css"), true);
        IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, PdfWriter)));
        var worker = new XMLWorker(pipeline, true);
        var xmlParse = new XMLParser(true, worker);
        pdfDoc.Open();
        xmlParse.Parse(sr);
        xmlParse.Flush();
        pdfDoc.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ";");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(pdfDoc);
        // Delete the temp image.
        File.Delete(Server.MapPath("qrimg.jpg"));
        Image1.ImageUrl = ImgUrl;
        Response.End();
    }