Java & J2EE Creating Simple PDF Files With iTextSharp |
The Portable Document Format (PDF) is one of the most popular ways to exchange documents because it allows for a consistent look across multiple platforms and configurations. When a PDF document is created, everything is fixed, including the page size, the font size, the margins, and so forth. The way the document looks on the machine that created it is the way that the document will look on another machine and when it's printed out. “Statically” creating PDF files is quite simple. Applications, such as OpenOffice.org Writer, give the user the option to export a word processor document as a PDF file. Users can even find utilities that allow them to “print” directly to a PDF file. Features like these are well-known to end users and are useful for creating a PDF based off a document originally created in another format. But what about dynamic PDF generation? Some programs need to be able to write out PDF files. A number of third-party libraries exist for this purpose. One popular library is iText for Java. The iText library allows for PDF (among other formats, actually, but we're only concerned with PDF) generation and manipulation, and it's open source. In fact, it's available under either the Mozilla Public License, which is the recommended license, or the LGPL—so you're allowed to use it in proprietary applications. Both iText and iTextSharp can be found on SourceForge: http://sourceforge.net/projects/itextsharp/ http://sourceforge.net/projects/itext/ Download the iTextSharp DLL, and let's get to work. The basics of PDF documents If you're using Visual Studio, create a project using the Console Application template and go ahead and add the DLL as a reference. Then, create a file and add a few using directives to the top: using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; Before we start working with a PDF document, we must first create one. A document in iTextSharp is represented by the appropriately-named Document object. The easiest way to instantiate a Document object is to use the parameterless constructor: Document doc = new Document(); This will create a document using the A4 page size. However, a number of other standard page sizes are also available. Many users will be more familiar with the letter page size (8.5” x 11”). Creating a document based off of this size isn't difficult: Document doc = new Document(PageSize.LETTER); The above two constructors create documents with margins of 36 points each (72 points make up a single inch). We can change this, though, by passing the size of each margin in the constructor: passing the left margin size, the right margin size, the top margin size, and the bottom margin size, in that order. So, to create a document with margins of one inch, we'd do this: Document doc = new Document(PageSize.LETTER, 72, 72, 72, 72); Of course, we need to actually create the document to disk before we can use it. This only takes one short step: PdfWriter.GetInstance(doc, new FileStream("filename.pdf", FileMode.Create)); Before being written to, the document needs to be opened: doc.Open(); And when everything is done, the document needs to be closed: doc.Close(); (Note that before a document can be properly closed, it must have content.)
blog comments powered by Disqus |
|
|
|
|
|
|
|