本文介绍了如何在iText7中并排打印两个并行表,一次将它们呈现在一页上,然后添加新页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iText7中并排添加两个并行表(表包含的内容超过一页).呈现方式应为:

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

在第1页上呈现两个表,然后添加新页.然后在第二页上呈现表的其余部分.如果它们仍然溢出,请添加另一页.在第3页上添加表的其余部分,依此类推.

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.

在此情况下,这是iText5中使用的方法.主要代码:

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方法:

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

请在iText7中建议这样的方法.

Kindly suggest the approach like this in iText7.

推荐答案

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. 我将每一行都视为一个新表.
  2. 我假设两个表都具有相同的行,但是您可以根据需要修改代码.

这篇关于如何在iText7中并排打印两个并行表,一次将它们呈现在一页上,然后添加新页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 12:01