ASP.NET : How to Generate PDF from HTML with iTextSharp

We intended this article in order to make global asp.net MVC development community aware of the technique to generate PDF file from HTML with the help of iTextSharp. iTextSharp is a .net PDF library that enables developers to produce PDF file. How it makes the things possible, let’s find out.

ITextSharp is a .NET PDF library which allows you to generate PDF (Portable Document Format). There are many other feature of ITextSharp but currently we are implementing the feature to generate PDF from HTML content in ASP.NET MVC.

So first of all we need to add reference of iTextSharp in our project.

We can get iTextSharp reference using package manager, we just need to execute following command to download and add reference.

PM> Install-Package iTextSharp

After executing this command in Package Manager Console you can find it in solution explorer under Reference like this:

Package Manager Console - iTextSharp

Now you need to create a method which will give you byte array of PDF content, so our code will be .

public byte[] GetPDF(string pHTML) {
    byte[] bPDF = null;

    MemoryStream ms = new MemoryStream();
    TextReader txtReader = new StringReader(pHTML);

    // 1: create object of a itextsharp document class
    Document doc = new Document(PageSize.A4, 25, 25, 25, 25);

    // 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file
    PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);

    // 3: we create a worker parse the document
    HTMLWorker htmlWorker = new HTMLWorker(doc);

    // 4: we open document and start the worker on the document
    doc.Open();
    htmlWorker.StartDocument();

    // 5: parse the html into the document
    htmlWorker.Parse(txtReader);

    // 6: close the document and the worker
    htmlWorker.EndDocument();
    htmlWorker.Close();
    doc.Close();

    bPDF = ms.ToArray();

    return bPDF;
}

So this method will returns byte array of PDF and you can use that according to your requirement.

For example if you want to download this pdf in ASP.NET MVC application then do the following code.

public void DownloadPDF() {
    string HTMLContent = "Hello <b>World</b>";

    Response.Clear();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=" + "PDFfile.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.BinaryWrite(GetPDF(HTMLContent));
    Response.End();
}

Set this method in controller and call this method by executing following URL to download the PDF file:

http://www.yourdomainname.com/controllername/DownloadPDF
This article is written by Ethan Millar. He shared the technique to generate PDF from HTML using iTextSharp with asp.net MVC development platform. You can try it and share your experience with us. Also, if you face any issue while performing, you can leave the comment and get expert assistance for your query.
Disclosure: Some of our articles may contain affiliate links; this means each time you make a purchase, we get a small commission. However, the input we produce is reliable; we always handpick and review all information before publishing it on our website. We can ensure you will always get genuine as well as valuable knowledge and resources.

Article Published By

Souvik Banerjee

Web developer and SEO specialist with 20+ years of experience in open-source web development, digital marketing, and search engine optimization. He is also the moderator of this blog "RS Web Solutions (RSWEBSOLS)".

Share the Love

Related Articles