locked
itextsharp document has no pages RRS feed

  • Question

  • User627014225 posted


    I am trying to generate pdf of my gridview using following code:

    HtmlForm form = new HtmlForm(); form.Controls.Add(PGGridViewDetail); StringWriter sw = new StringWriter(); HtmlTextWriter hTextWriter = new HtmlTextWriter(sw); form.Controls[0].RenderControl(hTextWriter); 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";

    PdfWriter.GetInstance(document, new FileStream(Path, FileMode.Create));

    // step 3: we open the document
    document.Open();

    // 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);
    xmlHandler.Parse(_xmlr);


    //HtmlParser.Parse(document, _xmlr);

    // step 5: we close the document
    document.Close();

    but is is showing the html markup of the grid instead of grid in newly generated pdf.

    if I comment step 4

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

    then I get document has no pages.

    please suggest me solution

    Sunday, March 18, 2012 2:25 AM

All replies

  • User-305388250 posted


    I am trying to generate pdf of my gridview using following code:

    HtmlForm form = new HtmlForm(); form.Controls.Add(PGGridViewDetail); StringWriter sw = new StringWriter(); HtmlTextWriter hTextWriter = new HtmlTextWriter(sw); form.Controls[0].RenderControl(hTextWriter); 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";

    PdfWriter.GetInstance(document, new FileStream(Path, FileMode.Create));

    // step 3: we open the document
    document.Open();

    // 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);
    xmlHandler.Parse(_xmlr);


    //HtmlParser.Parse(document, _xmlr);

    // step 5: we close the document
    document.Close();

    but is is showing the html markup of the grid instead of grid in newly generated pdf.

    if I comment step 4

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

    then I get document has no pages.

    please suggest me solution

    I had this problem too, try removing 

    // step 5: we close the document 
    document.Close();

    And see if this works for you...

    Sunday, March 25, 2012 9:54 AM
  • User-1984520607 posted

    use this:

     HtmlForm form = new HtmlForm(); form.Controls.Add(PGGridViewDetail);
                StringWriter sw = new StringWriter();
                HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);
                form.Controls[0].RenderControl(hTextWriter);
                string htmlContent = sw.ToString();


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

                Document document = new Document();

                string Path = Server.MapPath("~/Jaram PDF/PDFS/") + "Sample.pdf";

                PdfWriter.GetInstance(document, new FileStream(Path, FileMode.Create));

                // step 3: we open the document
                document.Open();

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

                HTMLWorker worker = new HTMLWorker(document);

                document.Open();

                // step 5: parse the html into the document
                worker.Parse(reader);

                worker.Close();
                document.Close();

    Tuesday, March 27, 2012 4:51 AM