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 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:
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())
{
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");