0

I want to add two parallel tables (tables contain content more than one page) side by side in iText7. Rendering should be done as:

Render two tables on page 1, then add new page. Then render remaining part of tables on second page. If they still overflows add another page. Add remaining part of table on page 3 and so on.

Here is the approach that is used in iText5 for this scenario. Main code:

ColumnText[] columns = new ColumnText[2];
columns[0]=column1;
columns[1]=column3;
while (addColumns(columns)) {
    addNewPage(true, pageId, document, writer);
    columns[0].setSimpleColumn(10 * dpiRatio, pageStart * dpiRatio,(10+434) * dpiRatio,pageFooter * dpiRatio);
    columns[1].setSimpleColumn(400 * dpiRatio, pageStart * dpiRatio,800 * dpiRatio,pageFooter * dpiRatio);
}

Helper methods:

public boolean addColumns(ColumnText[] columns) throws DocumentException {
    int status = ColumnText.NO_MORE_TEXT;

    for (ColumnText column : columns) {
        if (ColumnText.hasMoreText(column.go()))
            status = ColumnText.NO_MORE_COLUMN;
    }
    return ColumnText.hasMoreText(status);
}


public void addNewPage(boolean applyHeaderFooter, int pageId,Document document, PdfWriter writer) {
    document.newPage();
    writer.setPageEmpty(false);
}

Kindly suggest the approach like this in iText7.

Alexey Subach
  • 11,903
  • 7
  • 34
  • 60

1 Answers1

0
public class TableOperations {

  public static void main(String[] args) {

    try {
        File outputFile = new File("Abc.pdf");
        PdfWriter writer = new PdfWriter(outputFile);
        PdfDocument pdfDocument = new PdfDocument(writer);
        pdfDocument.setTagged();
        addTablesInDocument(pdfDocument);
        Document document = new Document(pdfDocument);
        document.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

public static void addTablesInDocument(PdfDocument pdfDocument) {
    PdfPage pdfPage = pdfDocument.addNewPage();
    PdfCanvas pdfCanvas = new PdfCanvas(pdfPage);
    Rectangle pageSize = pdfPage.getPageSizeWithRotation();
    float currentHeight = pageSize.getHeight() - 40;
    Canvas canvas = new Canvas(pdfCanvas, pdfDocument, pageSize);
    for (int i = 0; i < 100; i++) {
        float[] columnWidth = {
            5f, 5f
        };
        Table tableLeft =
            new Table(columnWidth).setFontSize(8).setTextAlignment(TextAlignment.CENTER);
        Table tableRight =
            new Table(columnWidth).setFontSize(8).setTextAlignment(TextAlignment.CENTER);
        // Adds the data in the table
        addRowInTable(tableLeft);
        addRowInTable(tableRight);
        // If you want to operate left and right tables differently then you can get the height of
        // right table as well
        float height = getHeightOfTable(canvas, tableLeft);
        // Here we are checking if the height of table is getting out of the page, if yes then we move
        // the table to next page
        if ((currentHeight - height) <= 40) {
            PdfPage newPdfPage = pdfDocument.addNewPage();
            PdfCanvas newPdfCanvas = new PdfCanvas(newPdfPage);
            canvas = new Canvas(newPdfCanvas, pdfDocument, pageSize);
            currentHeight = pageSize.getHeight() - 40;
        }
        tableLeft.setFixedPosition(20, currentHeight, (pageSize.getWidth() / 2) - 30);
        tableRight.setFixedPosition((pageSize.getWidth() / 2) + 10, currentHeight, (pageSize.getWidth() / 2) - 30);
        currentHeight = currentHeight - height;
        canvas.add(tableLeft);
        canvas.add(tableRight);
    }
}

public static float getHeightOfTable(Canvas canvas, Table table) {
    IRenderer pRenderer = table.createRendererSubTree().setParent(canvas.getRenderer());
    LayoutResult pLayoutResult =
        pRenderer.layout(new LayoutContext(new LayoutArea(0, canvas.getRootArea())));
    return pLayoutResult.getOccupiedArea().getBBox().getHeight();
}

public static Cell getNewCellForColumn1() {
    Paragraph paragraph1 = new Paragraph("This is sample for column 1");
    return new Cell().add(paragraph1);
}
public static Cell getNewCellForColumn2() {
    Paragraph paragraph1 = new Paragraph("This is sample for column 2");
    return new Cell().add(paragraph1);
}

public static Table addRowInTable(Table table) {
    table.addCell(getNewCellForColumn1());
    table.addCell(getNewCellForColumn2());
    table.startNewRow();
    return table;
}}

See if it works for you. In this solution:-

  1. I'm treating each row as a new table.
  2. I'm assuming that both the tables are having the same rows but you can modify the code according to your requirement.
Vakul Gupta
  • 90
  • 1
  • 9