Printing panel on webform in a4 format

sahin kaya 20 Reputation points
2023-09-22T17:49:45.2966667+00:00

hello,

how can I print the panel on the webform in A4 format?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,490 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,132 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,936 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 29,341 Reputation points Microsoft Vendor
    2023-09-25T03:10:08.4466667+00:00

    Hi @sahin kaya,

    how can I print the panel on the webform in A4 format?

    The exact method depends on how you want to print the panel.

    You can choose one of the following methods based on your needs.

    Window print() Method

    The print() method opens the print dialog box, where you can then specify the page size.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script type = "text/javascript">
            function PrintPanel() {
                var panel = document.getElementById("<%=pnlContents.ClientID %>");
                var printWindow = window.open('', '', 'height=400,width=800');
                printWindow.document.write('<html><head><title>DIV Contents</title>');
                printWindow.document.write('</head><body >');
                printWindow.document.write(panel.innerHTML);
                printWindow.document.write('</body></html>');
                printWindow.document.close();
                setTimeout(function () {
                    printWindow.print();
                }, 500);
                return false;
            }
        </script>
    </head>
    <body>
        <form id="form1" runat = "server">
        <asp:Panel id="pnlContents" runat = "server">
            <span style="font-size: 10pt; font-weight:bold; font-family: Arial">Hello,
                <br />
                This is <span style="color: #18B5F0">Mudassar Khan</span>.<br />
                Hoping that you are enjoying my articles!</span>
        </asp:Panel>
        <br />
        <asp:Button ID="btnPrint" runat="server" Text="Print" OnClientClick = "return PrintPanel();" />
        </form>
    </body>
    </html>
    
    

    User's image

    Or set Paper size using CSS.

    @page { 
        size: A4;
    }
    

    iTextSharp

    You can use iTextSharp to export ASP.Net panel content to PDF and specify the page size.

    using iTextSharp.text;
    using iTextSharp.text.html.simpleparser;
    using iTextSharp.text.pdf;
    
    protected void btnExport_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        panel.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
    }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.