There are no pages in the itextsharp document

I am trying to create a PDF from my gridview using the following code:

HtmlForm form = new HtmlForm();

StringWriter sw = new StringWriter();
HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);
string htmlContent = sw.ToString();

htmlContent = Regex.Replace(htmlContent, "</?(a|A).*?>", "");
htmlContent = Regex.Replace(htmlContent, "px", "");

Document document = new Document();

// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
string Path = Server.MapPath("~/Jaram PDF/PDFS/") + "Sample.pdf";
                      new FileStream(Path, FileMode.Create));

// step 3: we open the document

// step 4: we add a paragraph to the document
document.Add(new Paragraph(htmlContent.ToString()));
System.Xml.XmlTextReader _xmlr = new 
       System.Xml.XmlTextReader(new StringReader(htmlContent));
_xmlr.WhitespaceHandling = WhitespaceHandling.None;
ITextHandler xmlHandler = new ITextHandler(document);
//HtmlParser.Parse(document, _xmlr);

// step 5: we close the document          


But it shows the HTML grid markup instead of the grid in the newly generated PDF.

If I comment on step 4

// step 4: we add a paragraph to the document
document.Add(new Paragraph(htmlContent.ToString()));


then I get a document that has no pages.

Any idea what I am doing wrong?


source to share

1 answer

Actually, you are writing html String to PDF. Instead, add cells to the / foreach loop in PDF according to your grid you want to show in PDF.


PdfPTable DataTable0 = new PdfPTable(dtCommodities.Rows.Count);

            Chunk DataHeaderCH01 = new Chunk("Commodity", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL, BaseColor.WHITE));
            Phrase dhph01 = new Phrase();
            PdfPCell dhcell01 = new PdfPCell();
            dhcell01.BackgroundColor = new BaseColor(System.Drawing.Color.FromArgb(80, 124, 209));

            for (int i = 0; i < dtCommodities.Rows.Count; i++)
                DataTable0.AddCell(new Phrase(dtCommodities.Rows[i]["Code"].ToString(), new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL, BaseColor.BLACK)));
DataTable0.HorizontalAlignment = Element.ALIGN_LEFT;
            DataTable0.WidthPercentage = 100f;


Hope this helps. Don't forget to raise it if it solves your problem. Thank..:)



All Articles