Home arrow Java & J2EE arrow Adding Columns With iTextSharp

Adding Columns With iTextSharp

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.

  1. Adding Columns With iTextSharp
  2. An example
  3. Creating multiple columns
  4. Manually adding columns
By: Peyton McCullough
Rating: starstarstarstarstar / 7
June 24, 2008

print this article



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:


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;



>>> More Java & J2EE Articles          >>> More By Peyton McCullough

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: