Java
  Home arrow Java arrow Page 4 - 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 - A short example
    ( Page 4 of 4 )

    In order to recap everything before we move on, let's create a simple application that generates a very basic PDF document. Nothing fancy is needed here. We'll create a few paragraphs of text and style each of them a bit differently. This application will create a PDF in the file 1.pdf:


    using System;

    using System.IO;


    using iTextSharp.text;

    using iTextSharp.text.pdf;


    class Example1

    {

     static void Main(string[] args)

    {

     // Create and open a document

    Document doc = new Document();

    PdfWriter.GetInstance(doc,

     new FileStream("1.pdf", FileMode.Create));

    doc.Open();


     // Create a string to use for paragraphs

     string text = "This is a paragraph. It is represted" +

     " by a Paragraph object in the iTextSharp " +

     "library. Here, we're creating paragraphs with " +

     "various styles in order to test out iTextSharp." +

     " This paragraph will take up multiple lines " +

     "and allow for a more complete example.";


     // Add a basic paragraph

    doc.Add(new Paragraph(text));


     // Add a paragraph that's underlined and indented

    Paragraph p2 = new Paragraph(text);

    p2.IndentationLeft = 36;

    p2.Font.SetStyle(Font.UNDERLINE);

    doc.Add(p2);


     // Add a paragraph that's underlined and in bold

    doc.Add(new Paragraph(text,

    FontFactory.GetFont("Courier",

    Font.DEFAULTSIZE, Font.UNDERLINE | Font.BOLD)));

     

     // Add a really big paragraph

    doc.Add(new Paragraph(text,

     new Font(Font.HELVETICA, 36)));


     // Add a green, centered paragraph

    Paragraph p5 = new Paragraph(text,

     new Font(Font.TIMES_ROMAN,

    Font.DEFAULTSIZE, Font.NORMAL, Color.GREEN));

    p5.Alignment = Element.ALIGN_CENTER;

    doc.Add(p5);


     // Add a double-spaced paragraph with

     // an indented first line

    Paragraph p6 = new Paragraph(text);

    p6.FirstLineIndent = 36;

    p6.MultipliedLeading = 2;

    doc.Add(p6);


     // Add a paragraph with multiple styles

     // Each word gets bigger

    Paragraph ascending = new Paragraph();

    ascending.SpacingBefore = 72;

     for (int i = 1; i <= 5; i++)

    {

    ascending.Add(new Chunk("Hello",

     new Font(Font.HELVETICA, i * 5)));

    }

    doc.Add(ascending);


     // Close the document

    doc.Close();

    }

    }


    The example is pretty straightforward. Everything should be familiar to you, and, moreover, comments explain the different sections of code. When you run the above code, the file 1.pdf will be created. If you open the new PDF file, you should see multiple paragraphs with various styles.

    You've looked at two basic text objects, fonts, and a document creation in the iTextSharp library, but, of course, the library supports more features. Besides adding text, you can add graphics and metadata to a PDF document. You can even enhance the text parts of a document with features such as columns.



     
     
    >>> 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 1 Hosted by Hostway
    Stay green...Green IT