We help IT Professionals succeed at work.
Get Started

HTML Code to PDF C#

DB_Fury asked
Last Modified: 2013-12-17
I have the following code that will open as a pdf when the page is opened.  all that will display on the pdf is "Hello World!" becuase of the pdfDocument.Add(new Paragraph("Hello world"); line.  But I have been playing around with it and I cant seen to figure out how to add the HTML code.  I should mention that I have the attached code in the page_load seciton in the code behind of the aspx page that i want to go to a PDF.   So my question is how can i get my html page to load on to this PDF? Is this possible or do I have to create the tables and stuff in the C# code?
Document document = new Document(); 
        try { 
            // step 2: 
            // we create a writer that listens to the document 
            // and directs a PDF-stream to a file 
            Document pdfDocument = new Document(PageSize.A4); 
            MemoryStream pdfStream = new MemoryStream(); 
            PdfWriter.GetInstance(pdfDocument, pdfStream); 
            // step 3: we open the document 
            // step 4: we Add a paragraph to the document 
            pdfDocument.Add(new Paragraph("Hello World!"));

            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-disposition", "attachment; filename=NAME.pdf"); 
        catch (DocumentException de) { 
        catch (IOException ioe) { 

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE