locked
export to pdf c# code for my asp.net Repater RRS feed

Answers

  • User665608656 posted

    Hi Gopi.MCA,

    According to your request, I suggest that you use StyleSheet to add styles when exporting PDF in the code behind.

    Use the LoadTagStyle method of this class to style the tables you want to export.

    For the first parameter of the LoadTagStyle method, you could use HtmlTags directly to get the current table or tr or th elements.

    You could also set the className on the table in the foreground and call it in the first parameter of the method.

    The second parameter of this method is to set which style you want to modify the css, and the third parameter is to set the specific value of the CSS style.

    Finally, StyleSheet class objects are added as parameters to HTMLWorker's SetStyleSheet method.

    For more details, you could refer to the following code:

    You could also refer to this link:

    https://csharp.hotexamples.com/examples/iTextSharp.text.html.simpleparser/StyleSheet/LoadTagStyle/php-stylesheet-loadtagstyle-method-examples.html

      <form id="form1" runat="server">
            <asp:Panel ID="Panel1" runat="server">
                <table border="1"  style="border-collapse:collapse">
                    <tr>
                        <td>Name
                        </td>
                        <td>City
                        </td>
                        <td>Country
                        </td>
                    </tr>
                    <asp:Repeater ID="Repeater1" runat="server">
                        <ItemTemplate>
                            <tr>
                                <td>
                                    <asp:Label ID="lblContactName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                                </td>
                                <td>
                                    <asp:Label ID="lblCity" runat="server" Text='<%# Eval("City") %>'></asp:Label>
                                </td>
                                <td>
                                    <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
                                </td>
                            </tr>
                        </ItemTemplate>
                    </asp:Repeater>
                </table>
    
            </asp:Panel>
            <asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />
    
    
        </form>

    code behind:

    using iTextSharp.text;
    using iTextSharp.text.html;
    using iTextSharp.text.html.simpleparser;
    using iTextSharp.text.pdf;
    using System;
    using System.Data;
    using System.IO;
    using System.Web;
    using System.Web.UI;
    
    namespace WebApplication1.Cases
    {
        public partial class WebForm_0614_2156568 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    if (!IsPostBack)
                    {
                        DataTable dt = new DataTable();
                        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Name"), new DataColumn("City"), new DataColumn("Country") });
                        dt.Rows.Add("Mudassar", "Mumbai", "India");
                        dt.Rows.Add("John", "Chicago", "USA");
                        dt.Rows.Add("Rick", "London", "UK");
                        dt.Rows.Add("Mike", "California", "USA");
                        dt.Rows.Add("Peter", "Toronto", "Canada");
                        Repeater1.DataSource = dt;
                        Repeater1.DataBind();
                    }
                }
            } 
            protected void btnExport_Click(object sender, EventArgs e)
            {  
                StyleSheet styles = new StyleSheet();
                styles.LoadTagStyle(HtmlTags.TABLE, HtmlTags.FONTSIZE, "15pt");
                styles.LoadTagStyle(HtmlTags.TABLE, HtmlTags.COLSPAN, "collapse");
                styles.LoadTagStyle(HtmlTags.TABLE, HtmlTags.COLOR, "blue");
                styles.LoadTagStyle(HtmlTags.TH, HtmlTags.BORDER, "1");
                styles.LoadTagStyle(HtmlTags.TR, HtmlTags.ALIGN, HtmlTags.ALIGN_CENTER);
                styles.LoadTagStyle(HtmlTags.TD, HtmlTags.BORDER, "1");
    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); Panel1.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); htmlparser.SetStyleSheet(styles); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); } } }

    The result of my work demo:

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 17, 2019 8:04 AM

All replies

  • User-1038772411 posted

    Hi, Gopi.MCA

    We can use multiple technique for download pdf because this one is very big concept but i found best and easy way to do this. so please Go below link and download project "C# code export pdf with repater control"

    https://www.aspsnippets.com/Articles/Export-Repeater-Control-to-PDF-Document-in-ASPNet-using-iTextSharp.aspx

    Thanks.

    Thursday, June 13, 2019 9:55 AM
  • User-807418713 posted

    Hello

    That converts to pdf perfect

    But its not showing border?

    How to add border

    Thursday, June 13, 2019 9:58 AM
  • User-1038772411 posted

    You Can use below code for add border in pdf page

            PdfContentByte content = writer.DirectContent;
            Rectangle rectangle = new Rectangle(document.PageSize);
            rectangle.Left += document.LeftMargin;
            rectangle.Right -= document.RightMargin;
            rectangle.Top -= document.TopMargin;
            rectangle.Bottom += document.BottomMargin;
            content.SetColorStroke(Color.BLACK);
            content.Rectangle(rectangle.Left, rectangle.Bottom, rectangle.Width, rectangle.Height);
            content.Stroke();
     

    Please Use below Link For More UnderStanding :

    https://www.aspforums.net/Threads/151175/How-to-add-border-to-PDF-page-using-iTextSharp-in-C-Net/

    Thursday, June 13, 2019 10:12 AM
  • User-807418713 posted

    Hello

    Can it be possible to get full code on button click

    Thanks

    Thursday, June 13, 2019 11:23 AM
  • User665608656 posted

    Hi Gopi.MCA,

    What do you mean by full code here?

    Does it refer to the HTML code output to pdf?

    If this is the case, you can set the breakpoint on the btnExport_Click event to detect the value of the StrngWriter type variable sw, which is the HTML code output to pdf.

    If not, please tell us what full code mean, thanks.

    Best Regards,

    YongQing.

    Friday, June 14, 2019 7:29 AM
  • User-807418713 posted

    Hello Yongqing Yu

    Thanks For Your Reply

    I Used This below code to convert excel its works perfect, the same i want for PDF

      Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=Report.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            string style = @" <style > table {border-collapse: collapse;} table, th, td { border: 1px solid black;}</style>";
            Response.Write(style);
    
            Response.Output.Write(hfGridHtml.Value);
            Response.Flush();
            Response.End();

    Now Please Give Me The Code For PDF Convert.. With <style> 

    Thanks

    Friday, June 14, 2019 8:35 AM
  • User665608656 posted

    Hi Gopi.MCA,

    According to your requirements,I made an example about how to convert the code to PDF.

    Here is an example:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:Panel ID="Panel1" runat="server">
                <table border="1" style="text-align: center">
                    <tr>
                        <td style="line-height: 200%; color: black; width: 100px; font-size: 10pt; font-family: Arial">Name
                        </td>
                        <td style="line-height: 200%; color: black; width: 100px; font-size: 10pt; font-family: Arial">City
                        </td>
                        <td style="line-height: 200%; color: black; width: 100px; font-size: 10pt; font-family: Arial">Country
                        </td>
                    </tr>
                    <asp:Repeater ID="Repeater1" runat="server">
                        <ItemTemplate>
                            <tr>
                                <td style="text-align: center; color: black; font-size: 10pt; width: 100px; font-family: Arial">
                                    <asp:Label ID="lblContactName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                                </td>
                                <td style="text-align: center; color: black; font-size: 10pt; width: 100px; font-family: Arial">
                                    <asp:Label ID="lblCity" runat="server" Text='<%# Eval("City") %>'></asp:Label>
                                </td>
                                <td style="text-align: center; color: black; font-size: 10pt; width: 100px; font-family: Arial">
                                    <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
                                </td>
                            </tr>
                        </ItemTemplate>
                    </asp:Repeater>
                </table>
            </asp:Panel>
            <asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />
        </form>
    </body>
    </html>
    

    code behind:

    using iTextSharp.text;
    using iTextSharp.text.html.simpleparser;
    using iTextSharp.text.pdf;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebApplication1.Cases
    {
        public partial class WebForm_0614_2156568 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    if (!IsPostBack)
                    {
                        DataTable dt = new DataTable();
                        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Name"), new DataColumn("City"), new DataColumn("Country") });
                        dt.Rows.Add("Mudassar", "Mumbai", "India");
                        dt.Rows.Add("John", "Chicago", "USA");
                        dt.Rows.Add("Rick", "London", "UK");
                        dt.Rows.Add("Mike", "California", "USA");
                        dt.Rows.Add("Peter", "Toronto", "Canada");
                        Repeater1.DataSource = dt;
                        Repeater1.DataBind();
                    }
                }
            }
            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);
                Panel1.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();
            }
        }
    }

    Here is the result of this work demo:

    Best Regards,

    YongQing.

    Friday, June 14, 2019 9:33 AM
  • User-807418713 posted

    Hello

    This is my excel code same i want this in pdf 

      Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=Report.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            string style = @" <style > table {border-collapse: collapse;} table, th, td { border: 1px solid black;}</style>";
            Response.Write(style);
    
            Response.Output.Write(hfGridHtml.Value);
            Response.Flush();
            Response.End();

    Friday, June 14, 2019 10:07 AM
  • User-807418713 posted

    I want to print this below code  in pdf then it will come perfect 

    excel coming perfect with line

    string style = @" <style > table {border-collapse: collapse;} table, th, td { border: 1px solid black;}</style>";
            Response.Write(style);
     Response.Output.Write(hfGridHtml.Value);
    Friday, June 14, 2019 10:09 AM
  • User-807418713 posted
    Hi

    My code is above that works fine to convert to excel the same i want for pdf
    Monday, June 17, 2019 6:00 AM
  • User665608656 posted

    Hi Gopi.MCA,

    According to your request, I suggest that you use StyleSheet to add styles when exporting PDF in the code behind.

    Use the LoadTagStyle method of this class to style the tables you want to export.

    For the first parameter of the LoadTagStyle method, you could use HtmlTags directly to get the current table or tr or th elements.

    You could also set the className on the table in the foreground and call it in the first parameter of the method.

    The second parameter of this method is to set which style you want to modify the css, and the third parameter is to set the specific value of the CSS style.

    Finally, StyleSheet class objects are added as parameters to HTMLWorker's SetStyleSheet method.

    For more details, you could refer to the following code:

    You could also refer to this link:

    https://csharp.hotexamples.com/examples/iTextSharp.text.html.simpleparser/StyleSheet/LoadTagStyle/php-stylesheet-loadtagstyle-method-examples.html

      <form id="form1" runat="server">
            <asp:Panel ID="Panel1" runat="server">
                <table border="1"  style="border-collapse:collapse">
                    <tr>
                        <td>Name
                        </td>
                        <td>City
                        </td>
                        <td>Country
                        </td>
                    </tr>
                    <asp:Repeater ID="Repeater1" runat="server">
                        <ItemTemplate>
                            <tr>
                                <td>
                                    <asp:Label ID="lblContactName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                                </td>
                                <td>
                                    <asp:Label ID="lblCity" runat="server" Text='<%# Eval("City") %>'></asp:Label>
                                </td>
                                <td>
                                    <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
                                </td>
                            </tr>
                        </ItemTemplate>
                    </asp:Repeater>
                </table>
    
            </asp:Panel>
            <asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />
    
    
        </form>

    code behind:

    using iTextSharp.text;
    using iTextSharp.text.html;
    using iTextSharp.text.html.simpleparser;
    using iTextSharp.text.pdf;
    using System;
    using System.Data;
    using System.IO;
    using System.Web;
    using System.Web.UI;
    
    namespace WebApplication1.Cases
    {
        public partial class WebForm_0614_2156568 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    if (!IsPostBack)
                    {
                        DataTable dt = new DataTable();
                        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Name"), new DataColumn("City"), new DataColumn("Country") });
                        dt.Rows.Add("Mudassar", "Mumbai", "India");
                        dt.Rows.Add("John", "Chicago", "USA");
                        dt.Rows.Add("Rick", "London", "UK");
                        dt.Rows.Add("Mike", "California", "USA");
                        dt.Rows.Add("Peter", "Toronto", "Canada");
                        Repeater1.DataSource = dt;
                        Repeater1.DataBind();
                    }
                }
            } 
            protected void btnExport_Click(object sender, EventArgs e)
            {  
                StyleSheet styles = new StyleSheet();
                styles.LoadTagStyle(HtmlTags.TABLE, HtmlTags.FONTSIZE, "15pt");
                styles.LoadTagStyle(HtmlTags.TABLE, HtmlTags.COLSPAN, "collapse");
                styles.LoadTagStyle(HtmlTags.TABLE, HtmlTags.COLOR, "blue");
                styles.LoadTagStyle(HtmlTags.TH, HtmlTags.BORDER, "1");
                styles.LoadTagStyle(HtmlTags.TR, HtmlTags.ALIGN, HtmlTags.ALIGN_CENTER);
                styles.LoadTagStyle(HtmlTags.TD, HtmlTags.BORDER, "1");
    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); Panel1.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); htmlparser.SetStyleSheet(styles); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); } } }

    The result of my work demo:

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 17, 2019 8:04 AM