Java
  Home arrow Java arrow Page 3 - 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? 
Google.com  
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 - 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
     

       

    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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek