Java
  Home arrow Java arrow Creating Simple PDF Files With iTextSharp
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Creating Simple PDF Files With iTextSharp
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2008-06-17


    Table of Contents:
  • Creating Simple PDF Files With iTextSharp
  • Working with fonts and text
  • Working with paragraphs
  • A short example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Creating Simple PDF Files With iTextSharp
    ( Page 1 of 4 )

    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.

    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 Articles          >>> More By Peyton McCullough
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT