Java
  Home arrow Java arrow Adding Columns 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

Adding Columns With iTextSharp
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2008-06-24


    Table of Contents:
  • Adding Columns With iTextSharp
  • An example
  • Creating multiple columns
  • Manually adding columns

  • 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


    Adding Columns With iTextSharp
    ( Page 1 of 4 )

    The iTextSharp library makes it very easy to create a PDF document with text. The text can be styled in various ways, aligned in various ways, indented and spaced in various ways, etc. When the PDF document must be printed out, the document's readability can be improved by breaking the text into columns. The iTextSharp library provides support for columns, and in this article, we're going to take a look at them.

    Creating simple columns of text

    Creating a simple column of text is more complex than creating a paragraph—you can't just pass a line of text to a constructor and then simply add the result to the document. There are more steps involved in the process. One of the first steps is to create a reference to the document's PdfWriter, rather than just passively creating it:


    PdfWriter writer = PdfWriter.GetInstance(doc,

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


    With a document that just involves simple paragraphs, iTextSharp will automatically position things on the page. However, with more complex content, such as columns, positioning will have to be done manually. All of the user-positioned content in a page is stored in a PdfContentByte object. So, to add a column, we're going to need access to this. Access is provided through the DirectContent attribute of the PdfWriter:


    PdfContentByte cb = writer.DirectContent;


    Using PdfContentByte, we're able to create a column of text. A column of text is represented using the ColumnText class. So, to create a new column of text, we need to create a ColumnText object. The constructor accepts the PdfContentByte that we just retrieved:


    ColumnText column1 = new ColumnText(cb);


    The column isn't ready yet. It still needs to be placed on the page. To place it on the page, we need to give it a rectangle to occupy. The text will be placed within this rectangle. The rectangle is specified by passing two sets of coordinates. These coordinates correspond to two positions on the map. The rectangle will be drawn with the lower left corner at the location represented by the first set of coordinates and the upper right corner at the location represented by the second set of coordinates. Note that when specifying coordinates, the lower left corner of the page is the origin, just as in the first quadrant of a graph.

    To create a column two inches wide and three inches tall at the bottom left corner of the page, we'd do something like this:


    column1.SetSimpleColumn(72, 72, 72*3, 72*4);


    There are two ways to add text to the column. The first way will accept a Chunk or Phrase of text and add it to the column:


    column1.AddText(new Chunk("A chunk."));


    If you're using this method, then the column has some control over the text. Through the column, you can, for example, justify the text, creating the classic newspaper column look:


    column1.Alignment = Element.ALIGN_JUSTIFIED;


    The second way accepts an IElement. Using this method, we can add a paragraph to the column (which is probably what you'll want to do in most cases):


    column1.AddElement(new Paragraph("A paragraph."));


    In order to actually make the addition, however, the Go method must be called:


    column1.Go();


    This actually writes the text to the document.

    Before, we had the column justify the text. Now, however, the paragraph must justify the text itself:


    Paragraph p = new Paragraph("The text in this paragraph" +

     " is justified.");

    p.Alignment = Element.ALIGN_JUSTIFIED;

    column1.AddElement(p);

    column1.Go();




     
     
    >>> 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
    Stay green...Green IT