How to download pdf in inside panel control RRS feed

  • Question

  • User1827380138 posted

    How to download my webpage in Pdf document inside the panel controls

    Create pdf in asp web page inside the panel controls

    Thursday, January 2, 2020 6:55 AM

All replies

  • User288213138 posted

    Hi Dhoni,


    How to download my webpage in Pdf document inside the panel controls

    Create pdf in asp web page inside the panel controls

    According to your description, I couldn’t understand your requirement clearly.

    Do you want to create a pdf file in the panel? if so,It seems impossible.

    But if you want to output the contents of the panel control as a pdf, then you can try Spire.PDF.

    The demo for you as a reference:

     <asp:Panel ID="Panel1" runat="server">
                        PDF is generated by using Spire.PDF in Asp.net C#
     protected void Page_Load(object sender, EventArgs e)
                if (!IsPostBack)
                    PdfDocument pdf = new PdfDocument();          
                    PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
                    htmlLayoutFormat.IsWaiting = false;
                    PdfPageSettings setting = new PdfPageSettings();
                    setting.Size = PdfPageSize.A4;
                    var sb_htmlCode = new StringBuilder();
                    Panel1.RenderControl(new HtmlTextWriter(new StringWriter(sb_htmlCode)));
                    string htmlCode = sb_htmlCode.ToString();
                    Thread thread = new Thread(() =>
                    { pdf.LoadFromHTML(htmlCode, false, setting, htmlLayoutFormat); });

    The result:

    If I misunderstand your requirement, please post more details information about your requirement.

    Best regards,


    Thursday, January 2, 2020 8:55 AM
  • User-1038772411 posted

    Hello Dhoni,

    Attached code for the download pdf file using the panel control.

    Also demo is using text as well as image. Read text from the panel.

    Just execute the below command in package manager console. 

    Install-Package iTextSharp -Version

    .aspx page code

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div id="page-wrapper" >
             <div class="col-md-12">
      <!-- Form Elements -->
             <div class="panel panel-info">
             <div class="panel-heading">
                   <h3 style="text-shadow: 2px 2px #79aed8;">Barcode List - PDF </h3>  
              <div class="panel-body">
              <div id="Barcode">
               <h1 align="center" 
                      style="text-decoration: underline; font-size: x-large; font-weight: bold">Barcode</h1>
                <div id="excel1" runat="server">
                    <asp:Panel ID="Panel1" runat="server">
                                <td><asp:Label ID="lblPname" runat="server" Text="" Font-Bold="True"></asp:Label> </td>                        
    <td><asp:Label ID="lblLName" runat="server" Text="" Font-Bold="True"></asp:Label> </td>
    </tr> </table> </asp:Panel> </div> <div class="table-responsive";> <table class="table"> <tbody> <tr> <td> <asp:Button ID="btnPPdf" runat="server" Text="Print" CssClass="btn-info" Width="100px" OnClientClick = "return PrintPanel();" /> <asp:Button ID="btnExcel" runat="server" Text="Excel" CssClass="btn-success" Width="100px" onclick="btnExcel_Click" /> <asp:Button ID="btnBack" runat="server" Text="Back" CssClass="btn-danger" Height="35px" Width="100px" /> </td> </tr> </tbody> </table> </div> </div> </div> </div> </div> </div> </asp:Content>

    .aspx.cs code behind page code

    string barCode = "";
            string serverMapPath = "";
            protected void Page_Load(object sender, EventArgs e)
                lblPname.Text = "Raj";
                lblLName.Text = "Laptop";
                barCode = "G-212012123";
            protected void btnExcel_Click(object sender, EventArgs e)
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
                StringWriter stringWriter = new StringWriter();
                HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
                StringReader stringReader = new StringReader(stringWriter.ToString());
                Document Doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
                HTMLWorker htmlparser = new HTMLWorker(Doc);
                PdfWriter.GetInstance(Doc, Response.OutputStream);
                iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(serverMapPath);
                jpg.ScaleToFit(140f, 120f);
                jpg.SpacingBefore = 10f;
                jpg.Alignment = Element.ALIGN_RIGHT;
                iTextSharp.text.Image jpg2 = iTextSharp.text.Image.GetInstance(serverMapPath);
                jpg2.ScaleToFit(140f, 120f);
                jpg2.SpacingBefore = 10f;
                jpg2.Alignment = Element.ALIGN_CENTER;
                iTextSharp.text.Image jpg3 = iTextSharp.text.Image.GetInstance(serverMapPath);
                jpg3.ScaleToFit(140f, 120f);
                jpg3.SpacingBefore = 10f;
                jpg3.Alignment = Element.ALIGN_LEFT;
            private void Barcode()
                using (Bitmap bitMap = new Bitmap(barCode.Length * 40, 80))
                    using (Graphics graphics = Graphics.FromImage(bitMap))
                        System.Drawing.Font oFont = new System.Drawing.Font("IDAutomationHC39M", 16);
                        PointF point = new PointF(2f, 2f);
                        SolidBrush blackBrush = new SolidBrush(Color.Black);
                        SolidBrush whiteBrush = new SolidBrush(Color.White);
                        graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
                        graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
                    using (MemoryStream ms = new MemoryStream())
                        bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                        byte[] byteImage = ms.ToArray();
                        if (!Directory.Exists(Server.MapPath("~/Content/images/")))
                        serverMapPath = Server.MapPath("~/Content/images/" + DateTime.Now.ToString("ddMMyyyy_hhmmss") + ".jpg");
                        using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(byteImage)))
                            image.Save(serverMapPath, ImageFormat.Jpeg);  // Or Png

    Thursday, January 2, 2020 10:30 AM