How to continue an ordered list on a second page?

Tags: ColumnTextListListItemiText 5

I am currently creating the document by creating ColumnText objects that are placed at specific coordinates. The content consists of variable data (text/barcodes/images) that is added to existing PDF documents/templates (think boiler plate). Most commonly, I have to place various sections of text in specific places.

I know how to create an ordered list, but I have come across a situation where the list begins with #1 on the first page and then #2-4 on the top of the second page. I use two different templates for p1 and p2.

However, when I start a new list for the part on the second page, iText starts numbering that new list from 1 instead of continuing with 2.

Posted on StackOverflow on Mar 26, 2015 by rcurrydev

There are two answers to your question. The first one is to point you to the official documentation. There is a method setFirst() that sets the number that has to come first in the list.

You are using the C# port of iText, so if you want the list to start counting at 10, you need to do something like:

list.First = 10;

The second answer takes more time, but it is probably the better one. You don't need two List objects, one for the first page and one for the second page. It's better to add the List to a ColumnText object and then distribute the column over two pages.

Take a look at the ListInColumn example. It takes an existing PDF (with the text "Hello World Hello People") and it adds a list using ColumnText: list_in_column.pdf

Screen shot
Screen shot

This is how it's done:

PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
List list = new List(List.ORDERED);
for (int i = 0; i < 10; i++) {
        list.add("...");
}
ColumnText ct = new ColumnText(stamper.getOverContent(1));
ct.addElement(list);
Rectangle rect = new Rectangle(250, 400, 500, 806);
ct.setSimpleColumn(rect);
int status = ct.go();
if (ColumnText.hasMoreText(status)) {
    ct.setCanvas(stamper.getOverContent(2));
    ct.setSimpleColumn(rect);
    ct.go();
}
stamper.close();

To add the content on the first page, I use:

ColumnText ct = new ColumnText(stamper.getOverContent(1));

You are probably using similar code.

The content is added using the line:

int status = ct.go();

If not all the content was added, I change the canvas to add the rest of the content on the second page:

ct.setCanvas(stamper.getOverContent(2));

The rest of the code is pretty standard.

I think the setCanvas() method is the missing piece in your puzzle, although in your case, you'll need:

ct.Canvas = stamper.GetOverContent(2);