Home arrow Java & J2EE arrow Page 3 - Adding Columns With iTextSharp

Creating multiple columns - Java

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



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:


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);


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


 MultiColumnText columns = new MultiColumnText();

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

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


columns.AddElement(new Paragraph(text));






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 & 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: