Solved

iTextSharp Create New Page When Parsing HTML

Posted on 2011-09-08
1
1,162 Views
Last Modified: 2012-05-12
I am using iTextSharp (ver 5.1.2.0)  to create a PDF.  To generate my PDF file, I am parsing HTML to iTextSharp which is working fine.

But I want to be able to create a new page after certain sections of content in my HTML. I have seen some examples on the web that use "<newpage />" tag in the generated HTML for content to go onto another page. But it doesn't seem to be working.

Here is my code:

public static MemoryStream CreatePDF()
    {
        MemoryStream pdfMemStream = new MemoryStream();
        
        Document pdfDoc = new Document(PageSize.A4);
        PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, pdfMemStream);

        StringBuilder pdfBuilder = new StringBuilder();

        pdfBuilder.Append("<html>");
        pdfBuilder.Append("<body>");

        #region Executive Summary

        pdfBuilder.Append("<h1 style=\"color:#00a7c6;font-size:16px;\">Summary</h1><br />");
        pdfBuilder.Append("some text");
        pdfBuilder.Append("<br />");
        pdfBuilder.Append("<newpage />");
        pdfBuilder.Append("Text on new page.");

        #endregion

        pdfBuilder.Append("</body>");
        pdfBuilder.Append("</html>");

        StringReader reader = new StringReader(pdfBuilder.ToString());

        HTMLWorker parser = new HTMLWorker(pdfDoc);

        pdfDoc.Open();

        Font standardFont = FontFactory.GetFont("Verdana", 11);
        Font headerFont = FontFactory.GetFont("Arial", 24, new BaseColor(0, 167, 168));

        parser.Parse(reader);

        pdfDoc.Close();

        pdfMemStream.Close();

        return pdfMemStream;
    }

Open in new window

0
Comment
Question by:R1ND3R
1 Comment
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 36506915
I could be wrong, but I was under the impression that functionality was available with the HtmlToPdfBuilder class.

iTextSharp Utility
http://hugoware.net/blog/itextsharp-utility

This code allows for very simple HTML to be used to generate PDFs. It will not create complex pages automatically.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now