Working with iTextSharp - Using iTextSharp
(Page 2 of 4 )
To start, you’ll need to add a couple of using statements to your code:
using iTextSharp.text;
using iTextSharp.text.pdf;
Then create a Document object:
Document pdfDocument = new Document();
Next, create an instance of the PdfWriter and point it to where you want to save this document. In this example I’ll save to a file on the hard drive, but you can save it to any valid IO stream:
PdfWriter.GetInstance(pdfDocument,
new FileStream("C:\\WDPT.PDF", FileMode.Create));
Open the document and add some content:
pdfDocument.Open();
pdfDocument.Add(new Paragraph("Here is a test of creating a PDF"));
Then close it. You don’t need to flush the stream or actually write out the document.Close()does it all for you in one call:
pdfDocument.Close();
The document created can be seen in Figure 4-25.

Figure 4-25. A simple PDF
Of course, your requirements will rarely be so simple. iTextSharp offers a wide range of features to create more complex PDFs.
In the previous example, we used a paragraph to add text to the document, but you can also use phrases and chunks to create the text you want. A chunk is simply any piece of text with a consistent style; using it, you can specify independent fonts and colors. A phrase is a collection of chunks that includes a leading separator (the amount of vertical space between lines). Chunks and phrases can be added to paragraphs or added directly to documents.
Let’s create a couple of chunks with different fonts:
Chunk c = new Chunk("Some text in Verdana \n",
FontFactory.GetFont("Verdana", 12 ));
Chunk c2 = new Chunk("More text in Tahoma",
FontFactory.GetFont("Tahoma", 14));
and then create a paragraph and add those chunks:
Paragraph p = new Paragraph();
p.Add(c);
p.Add(c2);
You can see the results in Figure 4-26, where both chunks have their respective fonts.

Figure 4-26. Chunks with fonts
iTextSharp also provides support for working with images and embedding those images in your documents. Images can be added through URLs:
Image image = Image.GetInstance(
"http://www.oreillynet.com/images/
oreilly/home_tarsier.jpg");
or from the filesystem:
Image image = Image.GetInstance("home_tarsier.jpg");
PNG, GIF, JPEG, and WMF images can be loaded in this way. You can now add the image to the document or paragraph with the following code:
Document pdfDocument = new Document();
PdfWriter.GetInstance(pdfDocument,
new FileStream("C:\\WDPT.PDF", FileMode.Create));
pdfDocument.Open();
Image image = Image.GetInstance(
http://www.oreillynet.com/images/ oreilly/home_tarsier.jpg);
Chunk c = new Chunk("Check out this wicked graphic: \n",
FontFactory.GetFont("Verdana", 12 ));
Paragraph p = new Paragraph();
p.Add(c);
p.Add(image);
pdfDocument.Add(p);
pdfDocument.Close();
The generated PDF is shown in Figure 4-27.

Figure 4-27. A PDF with an image
iTextSharp also includes functions to position and scale images inside your PDFs.
As you can see from these examples, it is easy to create PDFs using iTextSharp. iTextSharp also includes the functionality to:
- Create and work with tables
- Create headers and footers
- Create chapters and sections
- Create anchors, lists, and annotations
Next: Manipulating PDFs with iTextSharp >>
More BrainDump Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Windows Developer Power Tools, written by James Avery and Jim Holmes (O'Reilly; ISBN: 0596527543). Check it out today at your favorite bookstore. Buy this book now.
|
|