# Tutorial: iText by Example

## ColumnText

Columns:
If you want to use iText to make product sheets, articles, newspaper pages, etc... you will probably want to present your data in columns. That's why the objects ColumnText and MultiColumnText were created.
With these objects you can add content within a given rectangle (or a userdefined irregular shape) and this content will be wrapped automatically when the right limit of the rectangle is reached (or it will be formatted to fit the irregular shape). All content that doesn't fit isn't shown, but it remains in the column object so that you can display it in a next column.
ColumnText
To show a certain phrase, centered in a rectangle between the coordinates (100, 300) and (200, 500), we write some code like this:

    PdfContentByte cb = writer.getDirectContent();
    ColumnText ct = new ColumnText(cb);
    ct.setSimpleColumn(phrase,
      100, 300, 200, 500, 15,
      Element.ALIGN_CENTER);
    ct.go();

setSimpleColumn(com.lowagie.text.Phrase, float, float, float, float, float, int). This specific method only works with content in a Phrase, but there are lots of methods in class ColumnText that allow you to specify indentation etc...
Example: java com.lowagie.examples.objects.columns.Column
Defining a simple single object ColumnText object: see column.pdf
It isn't necessary to add the text all at once in one Phrase object. You can first define the rectangle with setSimpleColumn(float, float, float, float, float) or setSimpleColumn(float, float, float, float, float, int), then add some text with addText(com.lowagie.text.Chunk) or addText(com.lowagie.text.Phrase) and show the column with the go()-method:

    PdfContentByte cb = writer.getDirectContent();
    ColumnText ct = new ColumnText(cb);
    ct.setSimpleColumn(60, 300, 100, 500, 15, Element.ALIGN_CENTER);
    ct.addText(phrase1);
    ct.addText(phrase2);
    ct.addText(chunk);
    ct.go();

Example: java com.lowagie.examples.objects.columns.ColumnSimple
Adding phrases to a simple ColumnText object: see columnsimple.pdf
Of course, if there is more text than space in the rectangle, we don't want to loose the text that didn't fit. Maybe we want this text to be shown in another column. That's why we are going to take a look at the return value of the go()-method. If this return value has the flag 'NO_MORE_COLUMN' turned on, there wasn't enough room for the text in the column. If all the text was shown, the flag 'NO_MORE_TEXT' is on.
go() causes (partial or complete) content to be written to the document. If you first want to check if the content will fit the rectangle you defined, use go(true) instead of go(). This will simulate adding content. Check if NO_MORE_TEXT or NO_MORE_COLUMN is returned and decide if you actually want to add the column or if you want to change its size first.
The next example gives you a column to which all kinds of objects were added with method addElement(com.lowagie.text.Element).
Example: java com.lowagie.examples.objects.columns.ColumnObjects
Adding different objects to a simple ColumnText object: see columnobjects.pdf
External resources for this example: cover.png
ColumnText can also be used to fit content in irregular, user-defined shapes. Use method setColumns(float[], float[]) to define a left and right boundary for the text:

    float[] left  = {70,790, 70,60};
    float[] right = {300,790, 300,700, 240,700, 240,590, 300,590, 300,106, 270,60};
    ct.setColumns(left, right);

The left border is a straight line (x = 70), but the right border is irregular.
Example: java com.lowagie.examples.objects.columns.ColumnIrregular
Defining an irregular ColumnText object: see columnirregular.pdf
External resources for this example: caesar_coin.jpg
MultiColumnText
If you want to add multiple columns automatically, you can use class MultiColumnText. It's a wrapper for ColumnText that does a lot of work in your place. With addRegularColumns(left, right, gutterWidth, numColumns), you define that a page should have numColumns columns, with gutterWidth space between them. The first column has left space to the left; the last column right space to the right. If you want iText to take care of the distribution of columns over multiple pages, you create the object like this:

    MultiColumnText mct = new MultiColumnText();

Otherwise, you specify the height of the column and a Y position; and you will have to check if all the text was displayed.
Example: java com.lowagie.examples.objects.columns.MultiColumnSimple
Defining a simple MultiColumnText object: see multicolumnsimple.pdf
If, for some reason, you want to add the columns from right to left, take a look at this example:
Example: java com.lowagie.examples.objects.columns.MultiColumnR2L
Defining a MultiColumnText object that adds its columns from right to left: see multicolumnR2L.pdf
MultiColumnText can also be used for irregular columns. Just add one or more columns with addColumn(float[],%20float[]), the way you would do it with ColumnText.setColumns(float[], float[]).
Example: java com.lowagie.examples.objects.columns.MultiColumnIrregular
Defining an irregular MultiColumnText object: see multicolumnirregular.pdf
You see that we used another constructor for this last example. We could have use automatic distribution over different pages (and add the diamond shape using a PageEvent), but instead we created the MultiColumnText object like this:

    MultiColumnText mct = new MultiColumnText(document.top() - document.bottom());

The parameter defines a maximum height for the column. If you do this, the columns will not automatically be distributed over different pages! You will need to use a loop:

    while (mct.hasMoreText()) {
        document.add(mct);
        mct.nextColumn();
        document.newPage();
    }

Go to top of the page
Amazon books:
amazon.co.uk-link