This is the first of three articles about creating PDF documents using iTextSharp. The Namespace is big, so I will focus on the parts you'll probably use when you need to create PDFs daily. 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 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.
Select the file by browsing to the save directory and selecting the file iTextSharp.dll.
To make the use of the component simple in code, add the following statements in your code.
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
Let's also create a folder where we save our PDFs; right-click the solution and add a folder, naming 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 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 filestream in the constructor.
PdfWriter writer = PdfWriter.GetInstance(document, fs);
A good thing is always to add meta information to files, this makes it easier to index the file properly. You can easily 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 well-known 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 file handles explicitly
fs.Close();
That's it, now you've got the file in your PDF folder and the document output looks like the following:
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:
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())
{
// 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 memory stream in the constructor.
PdfWriter writer = PdfWriter.GetInstance(document, ms);
// Open the document to enable you to write to the document
document.Open();
// Add a simple and well-known 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();
// Set the response content type to PDF
Response.ContentType = "application/pdf";
// Add a header to specify the filename for the PDF
Response.AddHeader("content-disposition", "attachment; filename=First_PDF_document.pdf");
// Write the PDF content from the memory stream to the response output stream
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