locked
string builder issue RRS feed

  • Question

  • User1623409651 posted

    Hi all,

    I am using the below code and parsing the string builder for generating pdf on button click

    StringBuilder sb = new StringBuilder();               
                    sb.Append("<table  border='1'>"); 
    sb.Append("<tr>");
                            foreach (DataColumn headerColumn in dtGetSeatingDetailsRoomWise.Columns)
                            {
                                sb.Append("<th>");
                                sb.Append(headerColumn.ColumnName);
                                sb.Append("</th>");
                            }
                            sb.Append("</tr>");
                            foreach (DataRow row in dtGetSeatingDetailsRoomWise.Rows)
                            {
                                sb.Append("<tr>");
                                foreach (DataColumn dtcolumn in dtGetSeatingDetailsRoomWise.Columns)
                                {
                                    sb.Append("<td>");
                                    sb.Append(row[dtcolumn]);
                                    sb.Append("</td>");
                                }
                                sb.Append("</tr>");
                            } 
                        sb.Append("</table>");
    Response.ContentType = "application/pdf";                
     Response.AddHeader("content-disposition", "attachment;filename=" + dtGetReservedRoomDetails.Rows[0]["EmployeeName"].ToString() + "_Seating.pdf");
     Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    StringReader sr = new StringReader(sb.ToString());
                    Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 20f, 0f);
                    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                    pdfDoc.Open();
                    htmlparser.Parse(sr);
                    pdfDoc.Close();
                    Response.Write(pdfDoc);
                    Response.End();

    How i break the page after each loop iteration ?

    Tuesday, May 29, 2018 7:16 AM

Answers

  • User-330142929 posted

    Hi Rameezwaheed,

    According to your description, I try to reproduce your problem.  I ‘d like to give you a workaround to achieve your requirement.

    I override the htmlwork class and then add the page break for “newpage” tags.

    Code behind.

    public partial class WebForm7 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<table  border='1'>");
                sb.Append("<tr>");
                foreach (DataColumn headerColumn in GetDatatable().Columns)
                {
                    sb.Append("<th>");
                    sb.Append(headerColumn.ColumnName);
                    sb.Append("</th>");
                }
                sb.Append("</tr>");
               foreach (DataRow row in GetDatatable().Rows)
                {
                    sb.Append("<tr>");
                    foreach (DataColumn dtcolumn in GetDatatable().Columns)
                    {
                        sb.Append("<td>");
                        sb.Append(row[dtcolumn.ColumnName]);
                        sb.Append("</td>");
                    }
                    sb.Append("</tr>");
     
                }
                sb.Append("</table>");
                string s1 = sb.ToString();
                sb.Append("<newpage />");
                sb.Append(s1);
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=" + "abcd" + "_Seating.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                StringReader sr = new StringReader(sb.ToString());
                Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 20f, 0f);
                HTMLWorkExtended htmlparser = new HTMLWorkExtended(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
                Response.Write(pdfDoc);
                Response.End();
     
            }
            public DataTable GetDatatable()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Name", typeof(string));
                dt.Columns.Add("City", typeof(string));
     
                DataRow dr = dt.NewRow();
                dr[0] = "Abraham";
                dr[1] = "London";
                dt.Rows.Add(dr);
                DataRow dr1 = dt.NewRow();
                dr1[0] = "Clauss";
                dr1[1] = "Beijing";
                dt.Rows.Add(dr1);
                return dt;
            }
        }
        public class HTMLWorkExtended : HTMLWorker
        {
            public HTMLWorkExtended(IDocListener document) : base(document)
            {
            }
     
            public override void StartElement(string tag, IDictionary<string, string> attrs)
            {
                if (tag.Equals("newpage"))
                {
                    document.Add(Chunk.NEXTPAGE);
                }
                else
                {
                    base.StartElement(tag, attrs);
                }
            }
    }

    GiF Demo.

    Besides, as you know, htmlworker is an obsolete class.

    https://www.aspsnippets.com/Articles/iTextSharp-HTMLWorker-is-Obselete-Depreciated-Replacement-of-iTextSharp-HTMLWorker-in-ASPNet-C-and-VBNet.aspx

    https://stackoverflow.com/questions/38851976/itextsharp-htmlworker-deprecated

    I suggest you use the XMLWorker library. Solution is as follows.

    <div style="page-break-before:always">&nbsp;</div>

    If you still have any question, please feel free to let me know.

     

    Best Regards,

    Abraham

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 31, 2018 2:06 AM

All replies

  • User475983607 posted

    The code you've posted is HTML and there are no page breaks in HTML.  Page breaks work in a browser with CSS and media but I'm pretty sure that's not going to work with your approach.

    Use an API like iTextSharp for control over page breaks. 

    https://www.nuget.org/packages/iTextSharp/

    Tuesday, May 29, 2018 10:39 AM
  • User632428103 posted

    Hello all,

    i have reply to the same question, why you don't use a method who load the data and add a page break with iTextSharp ?

    https://forums.asp.net/p/2141220/6209261.aspx?Re+Adding+page+break+in+pdf+doucment+using+itextsharp

    Tuesday, May 29, 2018 11:27 AM
  • Tuesday, May 29, 2018 2:29 PM
  • User1623409651 posted

    Use an API like iTextSharp for control over page breaks. 

    Hi, mgrbhard ,

    I am also using the itextsharp library but unable to identify how i can break the page after each loop iteration . is there any sample or link you suggest?

    Thanking you,

    Regards,

    Wednesday, May 30, 2018 3:51 AM
  • User1623409651 posted

    i tried this (style="page-break-before: always;) but did not succeed . can you suggest any other method or way to add page breaks.

    Thanking you,

    Wednesday, May 30, 2018 3:53 AM
  • User-330142929 posted

    Hi Rameezwaheed,

    According to your description, I try to reproduce your problem.  I ‘d like to give you a workaround to achieve your requirement.

    I override the htmlwork class and then add the page break for “newpage” tags.

    Code behind.

    public partial class WebForm7 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<table  border='1'>");
                sb.Append("<tr>");
                foreach (DataColumn headerColumn in GetDatatable().Columns)
                {
                    sb.Append("<th>");
                    sb.Append(headerColumn.ColumnName);
                    sb.Append("</th>");
                }
                sb.Append("</tr>");
               foreach (DataRow row in GetDatatable().Rows)
                {
                    sb.Append("<tr>");
                    foreach (DataColumn dtcolumn in GetDatatable().Columns)
                    {
                        sb.Append("<td>");
                        sb.Append(row[dtcolumn.ColumnName]);
                        sb.Append("</td>");
                    }
                    sb.Append("</tr>");
     
                }
                sb.Append("</table>");
                string s1 = sb.ToString();
                sb.Append("<newpage />");
                sb.Append(s1);
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=" + "abcd" + "_Seating.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                StringReader sr = new StringReader(sb.ToString());
                Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 20f, 0f);
                HTMLWorkExtended htmlparser = new HTMLWorkExtended(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
                Response.Write(pdfDoc);
                Response.End();
     
            }
            public DataTable GetDatatable()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Name", typeof(string));
                dt.Columns.Add("City", typeof(string));
     
                DataRow dr = dt.NewRow();
                dr[0] = "Abraham";
                dr[1] = "London";
                dt.Rows.Add(dr);
                DataRow dr1 = dt.NewRow();
                dr1[0] = "Clauss";
                dr1[1] = "Beijing";
                dt.Rows.Add(dr1);
                return dt;
            }
        }
        public class HTMLWorkExtended : HTMLWorker
        {
            public HTMLWorkExtended(IDocListener document) : base(document)
            {
            }
     
            public override void StartElement(string tag, IDictionary<string, string> attrs)
            {
                if (tag.Equals("newpage"))
                {
                    document.Add(Chunk.NEXTPAGE);
                }
                else
                {
                    base.StartElement(tag, attrs);
                }
            }
    }

    GiF Demo.

    Besides, as you know, htmlworker is an obsolete class.

    https://www.aspsnippets.com/Articles/iTextSharp-HTMLWorker-is-Obselete-Depreciated-Replacement-of-iTextSharp-HTMLWorker-in-ASPNet-C-and-VBNet.aspx

    https://stackoverflow.com/questions/38851976/itextsharp-htmlworker-deprecated

    I suggest you use the XMLWorker library. Solution is as follows.

    <div style="page-break-before:always">&nbsp;</div>

    If you still have any question, please feel free to let me know.

     

    Best Regards,

    Abraham

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 31, 2018 2:06 AM
  • User1623409651 posted

    Thank you Abraham,

    Your solution worked perfectly what i needed i will try to use XML Worker library again thanks 

    Regards,

    Thursday, May 31, 2018 4:05 AM