downloading or printing file in Literal control along with image control value RRS feed

  • Question

  • User-1994446809 posted

    Hello Forum,

    I have a web form the displays PDF file in literal control, an image control that is placed on the literal control and a button. When I click the button, I want to download or print the file (in PDF format), along with value of the image control. The value in the image control should be on the file when the printing out.

    I tried this but when I click the button, it is only the value in the image control that can be seen; the PDF file in the literal control is not seen. Please I need help, if any, in solving this. Thank you.

    Here is the HTML in the print page

    I put the two controls (Literal and image controls) inside a Panel

     <asp:Button ID="buttonmail" runat="server" CssClass="btn btn-primary navbar-btn" BackColor="SteelBlue" Font-Size="Small" Text="Download" OnClick="buttonmail_Click"/>
                            <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" />

    This is the method that calls the literal control to display the PDF file in an object, when the page loads

     protected void Page_Load(object sender, EventArgs e)
        private void BindCardAndQR()
            if (Session["paperinv"] != null)
                int id = Convert.ToInt32(Session["paperinv"]);
                // Binding PDF file in Literal control
                string embed = "<object data=\"{0}{1}\" type=\"application/pdf\" width=\"100%\" height=\"auto\"></object>";
                ltEmbed.Text = string.Format(embed, ResolveUrl("Handler.ashx?Id="), id);
                // This is where I Bind Image1 control for the QR code generated
                byte[] QRBytes = GetQRCodeBytes(Server.MapPath("~/PDFprint.aspx") + "?Id=" + id);
                Image1.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(QRBytes);

    This is the code that downloads the entire file and image control value in PDF format. I rebind the PDF file in Literal control on button click event

     protected void buttonmail_Click(object sender, EventArgs e)
            if (Session["paperinv"] != null)
                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);
                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);
                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);
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ";");
                // Delete the temp image.
                Image1.ImageUrl = ImgUrl;
        public string GetUrl(string imagepath)
            string[] splits = Request.Url.AbsoluteUri.Split('/');
            if (splits.Length >= 2)
                string url = splits[0] + "//";
                for (int i = 2; i < splits.Length - 1; i++)
                    url += splits[i];
                    url += "/";
                return url + imagepath;
            return imagepath;
        public override void VerifyRenderingInServerForm(Control control)
            /* Verifies that the control is rendered */

    The idea here is that a PDF file will be uploaded and a QR code will be generated for that file; the QR code will be inside an image control. Then the file will be printed or downloaded with the QR code on it.

    Friday, November 27, 2020 10:35 AM


  • User-939850651 posted

    Hi georgeakpan233,

    Based on your description and related code, I did some searches and found a similar article.

    In the code you provided, there are many methods and variables(such as Session["paperinv"], GetQRCodeBytes()), so I am afraid that I cannot reproduce your current problem.

    I can only suggest that you use debugging tools to view the specific conditions that occur during the execution of the code, and then make corresponding adjustments.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 1, 2020 8:52 AM