Semil Sebastian
Semil Sebastian

Reputation: 519

How to split columns in itextsharp?

I have 200 columns.I want to set 50 columns in one page,Total 4 page.I used

doc.NewPage() 

but it does not affect in my page?How can I split this 200 column as 50 in 4 pages.Now it looks like

enter image description here

Upvotes: 0

Views: 1532

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

There are different ways to do this.

The first way is explained in my answer to the question What's an easy to print "first right, then down"?

In the TableTemplate example, we write the full table to a PdfTemplate that is much bigger than a single page:

PdfPTable table = new PdfPTable(15);
table.setTotalWidth(1500);
PdfPCell cell;
for (int r = 'A'; r <= 'Z'; r++) {
    for (int c = 1; c <= 15; c++) {
        cell = new PdfPCell();
        cell.setFixedHeight(50);
        cell.addElement(new Paragraph(String.valueOf((char) r) + String.valueOf(c)));
        table.addCell(cell);
    }
}
PdfContentByte canvas = writer.getDirectContent();
PdfTemplate tableTemplate = canvas.createTemplate(1500, 1300);
table.writeSelectedRows(0, -1, 0, 1300, tableTemplate);

Once the table is completed, you can then distribute the tableTemplate object over different pages:

for (int j = 0; j < 1500; j += 500) {
    for (int i = 1300; i > 0; i -= 650) {
        clip = canvas.createTemplate(500, 650);
        clip.addTemplate(tableTemplate, -j, 650 - i);
        canvas.addTemplate(clip, 36, 156);
        document.newPage();
    }
}

If this is what you want to do, then your question is a duplicate of itext -- what's an easy to print "first right, then down"

The second way is to use the writeSelectedRows() method. In this case, your question is a duplicate of Auto Split Columns using iTextSharp and Itextsharp: Adjust 2 elements on exactly one page

The Zhang example from the second edition of iText in Action shows you how this method works:

PdfContentByte canvas = writer.getDirectContent();
// draw the first two columns on one page
table.writeSelectedRows(0, 2, 0, -1, 236, 806, canvas);
document.newPage();
// draw the remaining two columns on the next page
table.writeSelectedRows(2, -1, 0, -1, 36, 806, canvas);

The first time, we use writeSelectedRows, we draw column 0 to 2 (not included) and row 0 to the total number of rows on a page. The second time, we draw all the columns, starting with column 2, and all the rows.

The third way is to adapt the size of your page to the size of your table. This is explained in my answer to the question How to define the page size based on the content?

Upvotes: 1

Related Questions