Java
  Home arrow Java arrow Page 3 - Adding Columns With iTextSharp
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

Adding Columns With iTextSharp
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    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:
      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


    Adding Columns With iTextSharp - Creating multiple columns


    (Page 3 of 4 )

    In situations where a lot of text must be written to a document, there obviously needs to be multiple columns for the text to span across. While it's possible to do this manually using the ColumnText class, it's extra work. For situations where multiple columns are needed, it's best to use the MultiColumnText class, which supports multiple columns and will take care of much of the work involved for you. It's not even created and added to the page in the complicated manner that a ColumnText object is. Rather, it can be added to a document through the document's Add method.

    Creating an instance of MultiColumnText is very simple:


    MultiColumnText columns = new MultiColumnText();


    Once an instance is created, columns can be added. A number of columns can be automatically set up using the AddRegularColumns method. This method will create evenly spaced columns within an area, with a fixed space in between each column. For example, suppose we had a letter-sized page and needed to divide it into two columns with an inch on either side of the page and a half inch in between the two columns. The columns would be set up like this:


    columns.AddRegularColumns(72, 72*7.5f, 36, 2);


    The first argument is the left position of the first column (1”); the second argument is the right position of the last column (7.5”—one inch from the end of the page); the third argument is the amount of space in between each column (.5”); and the fourth argument is the number of columns to be created.

    Text can be added to the columns very easily using the AddElement method:


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


    Notice how there's no Go method to be called. AddElement does the job by itself.

    If there's not enough room in the column for the text, then the text will flow into the next column. If there's not enough room on the page for another column, then a new column will be created on a new page. As you can see, much of the work is done for us.

    The MultiColumnText object can be added to the document using the Add method of the Document object:


    doc.Add(columns);


    We can modify the last example to fit with MultiColumnText. However, let's add even more text to see how MultiColumnText handles multiple pages:


    using System;

    using System.IO;


    using iTextSharp.text;

    using iTextSharp.text.pdf;


    class Columns2

    {

     public static void Main()

    {

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

     " 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.";


     Document doc = new Document(PageSize.LETTER);

     PdfWriter.GetInstance(doc,

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

    doc.Open();

     MultiColumnText columns = new MultiColumnText();

    columns.AddRegularColumns(72, 72*7.5f, 36, 2);

     for (int i = 0; i < 20; i++)

    {

    columns.AddElement(new Paragraph(text));

    }

    doc.Add(columns);

    doc.Close();

    }

    }


    Everything should work as expected: overflow text will simply go into new columns. As you can see, this example is actually much simpler than the last one. All of the hard work is handled by the library itself, rather than by code written by us.

    More Java Articles
    More By Peyton McCullough


       · Hello,My previous article on the iTextLibrary (which you may want to read if you...
     

       

    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