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.

using iTextSharp;
using iTextSharp.text;
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)

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.

// Create an instance of the document class which represents the PDF document itself.
Document document = new Document(PageSize.A4, 25, 25, 30, 30);
// Create an instance to the PDF file by creating an instance of the PDF
// Writer class using the document and the filestrem in the constructor.

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!)

// Add meta information to the document

document.AddAuthor("Micke Blomquist");

document.AddCreator("Sample application using iTextSharp");

document.AddKeywords("PDF tutorial education");

document.AddSubject("Document subject - Describing the steps creating a PDF document");

document.AddTitle("The document title - PDF creation using iTextSharp");

 

Before we can write to the document, we need to open it.

// Open the document to enable you to write to the document

document.Open();

// Add a simple and wellknown phrase to the document in a flow layout manner

document.Add(new Paragraph("Hello World!"));

// Close the document

document.Close();
// Close the writer instance

writer.Close();
// Always close open filehandles explicity
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:

using (MemoryStream ms = new MemoryStream())

{

   Document document = new Document(PageSize.A4, 25, 25, 30, 30);

   PdfWriter writer = PdfWriter.GetInstance(document, ms);

   document.Open();

   document.Add(new Paragraph("Hello World"));

   document.Close();

   writer.Close();
   Response.ContentType = "pdf/application";

   Response.AddHeader("content-disposition",
   "attachment;filename=First PDF document.pdf");

   Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);

}

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

Up Next
    Ebook Download
    View all
    Learn
    View all