Solved

iTextSharp Create New Page When Parsing HTML

Posted on 2011-09-08
1
1,190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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