Java
  Home arrow Java arrow Page 3 - 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 - Working with paragraphs


    (Page 3 of 4 )


    However, Chunk is a bit low-level. More often, you'll be using Paragraph, which represents a paragraph of text. A paragraph has space before it, and it can be indented. Unlike a Chunk, a paragraph's text does not have to be consistent. That is, portions of the text can be underlined, struck through, etc. Here's a basic Paragraph:


    Paragraph hello = new Paragraph("Hello. This is a paragraph.");


    A Paragraph can be styled just like a Chunk:


    hello.Font.sestyle(Font.ITALIC);


    However, applying styles to specific sections of text within a Paragraph is a bit trickier. To do this, we must break the Paragraph into Chunk objects and then style each Chunk object. Here, we create a paragraph with multiple styled sections of text:


    Paragraph fancy = new Paragraph();


    Chunk bold = new Chunk("This ");

    bold.Font.SetStyle(Font.BOLD);

    fancy.Add(bold);


    Chunk italics = new Chunk("is a");

    italics.Font.SetStyle(Font.ITALIC);

    fancy.Add(italics);


    Chunk big = new Chunk("fancy");

    big.Font.Size = 20;

    big.Font.SetStyle(Font.BOLD);

    fancy.Add(big);


    Chunk struck = new Chunk("paragraph.");

    struck.Font.SetStyle(Font.STRIKETHRU);

    fancy.Add(struck);


    A paragraph will automatically have some spacing, but it's possible to add more:


    Paragraph spaced =

     new Paragraph("This has a lot of space around it.");

    spaced.SpacingBefore = 72;

    spaced.SpacingAfter = 72;

    spaced.IndentationLeft = 72;

    spaced.IndentationRight = 72;


    Indenting the paragraph by setting the IndentationLeft property indents the entire paragraph, though. Often, you'll want to indent only the first line of a paragraph. This can be done by setting the FirstLineIndent property:


    Paragraph p = new Paragraph();

    p.FirstLineIndent = 36;


    While paragraphs are left-aligned by default, the alignment can be changed using the Alignment property:


    Paragraph p = new Paragraph();

    p.Alignment = Element.ALIGN_LEFT;

    p.Alignment = Element.ALIGN_CENTER;

    p.Alignment = Element.ALIGN_RIGHT;

    p.Alignment = Element.ALIGN_JUSTIFIED;


    The lines in a paragraph will naturally need some space between them, called a leading. The default leading will often do fine, but sometimes the leading must be modified. This can be done by setting the Leading property:


    Paragraph spacy = new Paragraph();

    spacy.Leading = 72;


    The leading can also be specified in the constructor:


    Paragraph spacy2 = new Paragraph(72);

    Paragraph spacy3 = new Paragraph(72,

     "The leading is an inch.");


    The leading can also be calculated by multiplying the font size by a number. This feature can be used, for example, to double space a paragraph:


    Paragraph doubleSpaced = new Paragraph();

    doubleSpaced.MultipliedLeading = 2;


    More Java Articles
    More By Peyton McCullough


       · 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 3 hosted by Hostway
    Stay green...Green IT