Java
  Home arrow Java arrow Page 4 - Creating Simple PDF Files With iTextSh...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 5 stars5 stars5 stars5 stars5 stars / 4
    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:
      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


    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hello, all,I'm not entirely sure why this wasn't placed in ASPFree. Sorry about...
       · I have search over the net looking for examples on how to connect to sql database...
       · Generating a PDF document from a database would simply involved combining PDF...
     

       

    JAVA ARTICLES

    - 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...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT