Basic PDF Creation Using iTextSharp - Part I

This is the first of three articles about creating PDF documents using iTextSharp. The Namespace is really big, so I will focus on the parts you'll probably use when you need to create PDFs on a daily basis. There are tons of articles out there but they often just show a specific task, so I thought I might do one simple step by step, starting off with the basics.

In this article series I use a web application to show how easily you can create a valid PDF document with just a few lines of code, using the tool iTextSharp which is a free .NET component downloadable at http://sourceforge.net/projects/itextsharp/. (Version 5.0.6) You can easily do the same thing with some other project type, as well, so choose what suits you best.

After we have downloaded and unzipped the iTextSharp dll and created our project we need to add a reference to iTextSharp.dll. Do that by right clicking the Reference folder in your solution.

pdf_add_reference.jpg

Select the file by browsing to the save directory and selecting the file iTextSharp.dll.

pdf_add_reference_browse.jpg

To make the use of the component simple in code, add the following using statements in your code.

  1. using iTextSharp;  
  2. using iTextSharp.text;  
  3. using iTextSharp.text.pdf;  
Let's also create a folder where we save our PDF's; right click the solution and add a folder, name it "pdf". Okay, we are now all set to create our first PDF document.

Our first document

First we create a file stream object representing the actual file and name it to whatever you want.

(By using the method MapPath we target the folder we created earlier as this is a Web application)
  1. System.IO.FileStream fs = new FileStream(Server.MapPath("pdf") + "\\" + "First PDF document.pdf", FileMode.Create)  
To create a PDF document, create an instance of the class Document and pass the page size and the page margins to the constructor.
 
Then use that object and the file stream to create the PdfWriter instance enabling us to output text and other elements to the PDF file.
  1. // Create an instance of the document class which represents the PDF document itself.  
  2. Document document = new Document(PageSize.A4, 25, 25, 30, 30);  
  3. // Create an instance to the PDF file by creating an instance of the PDF   
  4. // Writer class using the document and the filestrem in the constructor.  
  5.   
  6. PdfWriter writer = PdfWriter.GetInstance(document, fs);  
A good thing is always to add meta information to files, this does it easier to index the file in a proper way. You can easilly add meta information by using these methods. (NOTE: This is optional, you don't have to do it, just keep in mind that it's good to do it!)
  1. // Add meta information to the document  
  2. document.AddAuthor("Micke Blomquist");  
  3. document.AddCreator("Sample application using iTextSharp");  
  4. document.AddKeywords("PDF tutorial education");  
  5. document.AddSubject("Document subject - Describing the steps creating a PDF document");  
  6. document.AddTitle("The document title - PDF creation using iTextSharp");  
Before we can write to the document, we need to open it.
  1. // Open the document to enable you to write to the document  
  2. document.Open();  
  3. // Add a simple and wellknown phrase to the document in a flow layout manner  
  4. document.Add(new Paragraph("Hello World!"));  
  5. // Close the document  
  6. document.Close();  
  7. // Close the writer instance  
  8. writer.Close();  
  9. // Always close open filehandles explicity  
  10. fs.Close();  
That's it, now you've got the file in your pdf folder and the document output looks like follows:

pdf_hello_world.jpg

Simple, but it is a PDF and that was our goal!

The properties we added are found in the document by choosing File - Properties in the open PDF document:

pdf_document_properties.jpg

 

Well, I have a Swedish version of Acrobat Reader as you all can see, I mean; as you all CAN'T see (!), but the fields are self explained.
(Title, author, Subject, keywords and the creator "Sample application using iTextSharp)

You could also wrap the PDF document instance in a memory stream if you want to just output the file directly to the client without saving it to disk, like this:

  1. using (MemoryStream ms = new MemoryStream())  
  2. {  
  3.    Document document = new Document(PageSize.A4, 25, 25, 30, 30);  
  4.    PdfWriter writer = PdfWriter.GetInstance(document, ms);  
  5.    document.Open();  
  6.    document.Add(new Paragraph("Hello World"));  
  7.    document.Close();  
  8.    writer.Close();  
  9.    Response.ContentType = "pdf/application";  
  10.    Response.AddHeader("content-disposition",   
  11.    "attachment;filename=First PDF document.pdf");  
  12.    Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);  
  13. }  
Okay, that's it for Part I. 

Please, check out Part II describing how to write text, place images and some simple graphics in the PDF document.

Part II, Part III


Similar Articles