Home arrow Java & J2EE arrow Creating Simple PDF Files With iTextSharp

Creating Simple PDF Files With iTextSharp

Have you ever wondered how to generate PDF documents in .NET? Thankfully, there's a port of the iText library for .NET, called iTextSharp. Moreover, since C# and Java share a number of similarities, iText code in Java can be easily converted into C# in order to work with iTextSharp. In this article, we'll take a look at the iTextSharp library, using it for PDF generation and manipulation in .NET.

TABLE OF CONTENTS:
  1. Creating Simple PDF Files With iTextSharp
  2. Working with fonts and text
  3. Working with paragraphs
  4. A short example
By: Peyton McCullough
Rating: starstarstarstarstar / 20
June 17, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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



 
 
>>> More Java & J2EE Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: