Using iTextSharp to generate pdf file in asp.net

Introduction

One of the common requirement of web applications is to provide users a way to download some contents. In case of a report, almost all of the reporting tools have the export to pdf kind of function available.

But sometime, it is not a report that needs to be generated in pdf. But it could be simple web page or content of a page or sometimes a plain text needs to be generated in pdf format.

There are many third party tools are available for this task. We can import their API’s in Asp.net application and it takes care of all core function of creating pdf files. iTexSharp being one of the widely used and Open source (FREE) component, we are going to see how easily it can be used and range of features it provides

Background

Sometime back, I required to use pdf component for some of my project and i took the most obvious option of iTextSharp. Most obvious because i wanted it for free. I am just trying to include my learning here in the series of iTextSharp tutorials.

Article Body

I intend to cover other features of iTextSharp component. Focus of this article would be on simply creating a pdf file with different sources of text.

To start with, download iTextSharp dll from official page here iTexSharp is a c# version of original Java library iText. To demonstrate the use, i have created a sample web application in c# and added reference to iTextSharp.dll. I wanted to test using some other site’s html page as a source to convert into pdf also, i would user entered string to convert into pdf. After creating couple of textboxes for input, I want to write code on button click event.

Before that, lets add few references in our code.

using System.IO;
using iTextSharp;
using iTextSharp.text.api;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

In button click event, create object of Document and PDFWriter

Document itextDoc = new Document();
PdfWriter pdfDoc = PdfWriter.GetInstance(itextDoc,Response.OutputStream);

At the end generated PDF file is going to be outputted to outputstream. The following code is just for reading other web page using WebClient and converting response into string reader (or reading text file into stringreader). You may want ignore this code if you are aware about how to do it.

 System.Net.WebClient webClient=new System.Net.WebClient(); 
 //passing url of local web page to read its html content
 Stream responseData = webClient.OpenRead("http://localhost:51951/Test/Default4.aspx");
 //converting stream into stream reader object
 StreamReader inputstream = new StreamReader(responseData);
 //If you want to read text from other source like plain text file or user input, ignore all above lines
 StringWriter sw = new StringWriter();
 HtmlTextWriter writer = new HtmlTextWriter(sw);
 writer.Write(inputstream.ReadToEnd());
 //comment above line and uncomment below line if you wish to convert text file to pdf
 //writer.Write(File.ReadAllText(@"E:\MyFolder\filename.txt"));
 StringReader sr = new StringReader(sw.ToString());

Now that e have the source content, we need to parse the source content for valid HTML format and extract iTextSharp elements. The parsed elements will be returned into List of IElement. We will then need to loop through all elements and add them individually using Document object we just created. Below lines of code will do just that

List<IElement> elements =HTMLWorker.ParseToList(sr,null);  

  itextDoc.Open();
  //htmlwrite.Parse(sr);
  foreach (IElement el in elements)
  {
      itextDoc.Add(el);
  }                  
  itextDoc.Close();

Before adding elements using Document object, Open method is called on Document object and Close afterwards

Lets run the test. I already have another simple asp.net page running in another Visual studio instance. When viewed in browser, the page looks like this

From my sample application when I call this page using WebClient and generate the PDF file, it opens up as

Our basic task is completed. You might want to add little more stuff to generated PDF. Like adding header/footer, images, watermark etc. Most of these functionality available in ITextSharp. We will look at them in following articles. Please find complete code for this test below. I would like to hear comments/crtics 🙂

System.Net.WebClient webClient=new System.Net.WebClient(); 
 //passing url of local web page to read its html content
 Stream responseData = webClient.OpenRead("http://localhost:51951/Test/Default4.aspx");
 //converting stream into stream reader object
 StreamReader inputstream = new StreamReader(responseData);
 //If you want to read text from other source like plain text file or user input, ignore all above lines
 StringWriter sw = new StringWriter();
 HtmlTextWriter writer = new HtmlTextWriter(sw);
 writer.Write(inputstream.ReadToEnd());
 //comment above line and uncomment below line if you wish to convert text file to pdf
 //writer.Write(File.ReadAllText(@"E:\MyFolder\filename.txt"));
 StringReader sr = new StringReader(sw.ToString());

 //Parse into IElement
 List<IElement> elements =HTMLWorker.ParseToList(sr,null);        
 //Open the Document
 itextDoc.Open();
 //loop through all elements
 foreach (IElement el in elements)
 {
     //add individual element to Document
     itextDoc.Add(el);
 }                  
 //Close the document
 itextDoc.Close();
 //set the Response object
 Response.ContentType = "application/pdf";
 Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
 Response.End();

All the code in this post is in c#. If you prefer VB.Net, Please use online VB.Net <-> C# converter here