locked
iTextSharp PDFTable and Cell Issue ? RRS feed

  • Question

  • User1002483009 posted

    Itext

    i have create one Pdf table and set add cell ( PdfPTable mainTable = new PdfPTable(1);)                 

    how to add DIV like below in PDF cell

                       string Body = "<table>";
                        for (int j = 0; j < dtTitleNOrder.Rows.Count; j++)
                        {
                            Body += "<tr><td>" + dtTitleNOrder.Rows[j]["cc_displaytitleName"].ToString() + "</td></tr>";
                            Body += "<tr><td><b>My Test Data </b></td></tr>";
                        }
                        div.InnerHtml = Body + "</table>";

    when I take div , issue is that it should not set bold property as <b>My Test Data </b> and  my out put is like  <b>My Test Data </b>

    Actually my out put will be like this : My Test Data

    Tuesday, November 25, 2014 1:25 AM

Answers

  • User1002483009 posted

      StringWriter stringWriter = new StringWriter();

     /*Set Chart Data*/
                        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
                        var Maindiv = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
                        var div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");

     div.InnerHtml = Body + "</table><br/><br/>";
                        Maindiv.Controls.Add(div);
                        Maindiv.RenderControl(htmlTextWriter);




                        PdfPTable mainTable = new PdfPTable(1);

                      //  mainTable.WidthPercentage = 100;
                        mainTable.ExtendLastRow = true;
                        mainTable.HeaderRows = 2;
                        mainTable.FooterRows = 1;
                        mainTable.SkipLastFooter = true;
                        mainTable.SkipFirstHeader = true;
                        mainTable.SplitLate = false;
                        //Header Row
                        mainTable.DefaultCell.Border = 0;
                        mainTable.DefaultCell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
                        mainTable.AddCell(new Phrase(""));

      StringReader stringReader = new StringReader(stringWriter.ToString());
                        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                        pdfDoc.Open();
                        pdfDoc.Add(patientDetails);
                        htmlparser.Parse(stringReader);
                        pdfDoc.Add(mainTable);
                        pdfDoc.Close();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 27, 2014 1:57 AM

All replies

  • User1508394307 posted

    PDF is not an html page so there are no <div>, <table> and so on.

    Your target is completely unclear. A PDF cell is already act as a cell of a table. If you mean that the code above is to add a new table (<table> tag) into a PDF cell, then do not use html tags but PdfPTable and PdfPCell objects of itextsharp. Read http://www.mikesdotnetting.com/article/86/itextsharp-introducing-tables 

    in other words, you could have something similar to

    PdfPTable table = new PdfPTable(1);
       
    for (int j = 0; j < dtTitleNOrder.Rows.Count; j++)
    {
    	table.AddCell(dtTitleNOrder.Rows[j]["cc_displaytitleName"].ToString());
    	PdfPCell cell = new PdfPCell(new Phrase("My Test Data", new Font(Font.HELVETICA, 8f, Font.BOLD, Color.WHITE)));
    	table.AddCell(cell); 
    }
       
    doc.Add(table); 

    Tuesday, November 25, 2014 2:55 AM
  • User1002483009 posted

      StringWriter stringWriter = new StringWriter();

     /*Set Chart Data*/
                        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
                        var Maindiv = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
                        var div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");

     div.InnerHtml = Body + "</table><br/><br/>";
                        Maindiv.Controls.Add(div);
                        Maindiv.RenderControl(htmlTextWriter);




                        PdfPTable mainTable = new PdfPTable(1);

                      //  mainTable.WidthPercentage = 100;
                        mainTable.ExtendLastRow = true;
                        mainTable.HeaderRows = 2;
                        mainTable.FooterRows = 1;
                        mainTable.SkipLastFooter = true;
                        mainTable.SkipFirstHeader = true;
                        mainTable.SplitLate = false;
                        //Header Row
                        mainTable.DefaultCell.Border = 0;
                        mainTable.DefaultCell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
                        mainTable.AddCell(new Phrase(""));

      StringReader stringReader = new StringReader(stringWriter.ToString());
                        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                        pdfDoc.Open();
                        pdfDoc.Add(patientDetails);
                        htmlparser.Parse(stringReader);
                        pdfDoc.Add(mainTable);
                        pdfDoc.Close();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 27, 2014 1:57 AM