BrainDump
  Home arrow BrainDump arrow Page 2 - Working with iTextSharp
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web 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? 
BRAINDUMP

Working with iTextSharp
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    2007-08-30

    Table of Contents:
  • Working with iTextSharp
  • Using iTextSharp
  • Manipulating PDFs with iTextSharp
  • 4.13 For More Information

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    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:

    1. Create and work with tables
    2. Create headers and footers
    3. Create chapters and sections
    4. Create anchors, lists, and annotations

    More BrainDump Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Windows Developer Power Tools," published...
     

    Buy this book now. 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.

    BRAINDUMP ARTICLES

    - Introduction to Office Live Workspace
    - Using MS Excel for One-way Analysis of Varia...
    - Comparing Data Sets Using Statistical Analys...
    - Import Blogger Posts into WordPress Using Wi...
    - Download WordPress from an FTP Server and Ru...
    - Install and Run WordPress in XAMPP Local Host
    - What Windows 7 Brings to the Table
    - Virtualization and Sandbox Detection
    - Advanced Firebug Techniques in Windows XP Ho...
    - Editing CSS with Firebug in Windows XP Home
    - Using Firebug in Windows XP Home
    - Migrating to Exchange Server 2007
    - Using System Restore on a Non-Bootable PC
    - Finding Logged on Users and More Scripting S...
    - Developing Macro Commands in MS Excel





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek