Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 707057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:14:05+00:00 2026-05-14T04:14:05+00:00

I’m trying to crate a PDF out of a HTML page. The CMS I’m

  • 0

I’m trying to crate a PDF out of a HTML page. The CMS I’m using is EPiServer.

This is my code so far:

    protected void Button1_Click(object sender, EventArgs e)
    {
        naaflib.pdfDocument(CurrentPage);
    }


    public static void pdfDocument(PageData pd)
    {
        //Extract data from Page (pd).
        string intro = pd["MainIntro"].ToString(); // Attribute
        string mainBody = pd["MainBody"].ToString(); // Attribute

        // makae ready HttpContext
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "application/pdf";

        // Create PDF document
        Document pdfDocument = new Document(PageSize.A4, 80, 50, 30, 65);
        //PdfWriter pw = PdfWriter.GetInstance(pdfDocument, HttpContext.Current.Response.OutputStream);
        PdfWriter.GetInstance(pdfDocument, HttpContext.Current.Response.OutputStream);  

        pdfDocument.Open();
        pdfDocument.Add(new Paragraph(pd.PageName));
        pdfDocument.Add(new Paragraph(intro));
        pdfDocument.Add(new Paragraph(mainBody));
        pdfDocument.Close();
        HttpContext.Current.Response.End();
    }

This outputs the content of the article name, intro-text and main body.
But it does not pars HTML which is in the article text and there is no layout.

I’ve tried having a look at http://itextsharp.sourceforge.net/tutorial/index.html without becomming any wiser.

Any pointers to the right direction is greatly appreciated 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:14:06+00:00Added an answer on May 14, 2026 at 4:14 am

    For later versions of iTextSharp:

    Using iTextSharp you can use the iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList() method to create a PDF from HTML.

    ParseToList() takes a TextReader (an abstract class) for its HTML source, which means you can use a StringReader or StreamReader (both of which use TextReader as a base type). I used a StringReader and was able to generate PDFs from simple mark up. I tried to use the HTML returned from a webpage and got errors on all but the simplist pages. Even the simplist webpage I retrieved (http://black.ea.com/) was rendering the content of the page’s ‘head’ tag onto the PDF, so I think the HTMLWorker.ParseToList() method is picky about the formatting of the HTML it parses.

    Anyway, if you want to try here’s the test code I used:

    // Download content from a very, very simple "Hello World" web page.
    string download = new WebClient().DownloadString("http://black.ea.com/");
    
    Document document = new Document(PageSize.A4, 80, 50, 30, 65);
    try {
        using (FileStream fs = new FileStream("TestOutput.pdf", FileMode.Create)) {
            PdfWriter.GetInstance(document, fs);
            using (StringReader stringReader = new StringReader(download)) {
                ArrayList parsedList = HTMLWorker.ParseToList(stringReader, null);
                document.Open();
                foreach (object item in parsedList) {
                    document.Add((IElement)item);
                }
                document.Close();
            }
        }
    
    } catch (Exception exc) {
        Console.Error.WriteLine(exc.Message);
    }
    

    I couldn’t find any documentation on which HTML constructs HTMLWorker.ParseToList() supports; if you do please post it here. I’m sure a lot of people would be interested.

    For older versions of iTextSharp:
    You can use the iTextSharp.text.html.HtmlParser.Parse method to create a PDF based on html.

    Here’s a snippet demonstrating this:

    Document document = new Document(PageSize.A4, 80, 50, 30, 65); 
    try  {
       using (FileStream fs = new FileStream("TestOutput.pdf", FileMode.Create)) {
          PdfWriter.GetInstance(document, fs);
          HtmlParser.Parse(document, "YourHtmlDocument.html");
       }
    } catch(Exception exc)  { 
       Console.Error.WriteLine(exc.Message); 
    } 
    

    The one (major for me) problem is the HTML must be strictly XHTML compliant.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.