ColumnText里面的iText表格不能保持一致

时间:2016-09-16 10:31:07

标签: itext pdfptable

我使用的是iText 5.5.9。我在PdfPTable内显示ColumnTextColumnText有一个小列和一个大列。

如果表格对于小列来说太大,它应该移动到第二个更大的列。我怎么能这样做?

我尝试使用table.setKeepTogether(true),但这不起作用。 我也尝试了这个页面(http://itext.2136553.n4.nabble.com/PdfPTable-KeepTogether-and-ColumnText-td2141501.html)中的建议将我的表包装在第二个表中并使用tableWrapper.setSplitRows(false)。但如果我这样做,我就再也看不到桌子了。

这是我的代码:

public class PdfTest {
    private static String filename = "C:/Users/Development/Documents/pdf_test.pdf";

    public static void main(String[] args) {
        try {
            new PdfTest();
            File f = new File(filename);
            Desktop.getDesktop().open(f);
        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        }
    }

    public PdfTest() throws DocumentException, IOException {
        File file = new File(filename);
        OutputStream os = new FileOutputStream(file);

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, os);

        document.open();

        PdfContentByte canvas = writer.getDirectContent();

        printPage1(document, canvas);

        document.newPage();

        printPage2(document, canvas);

        document.close();
        os.close();
    }

    private void printPage1(Document document, PdfContentByte canvas) throws DocumentException {
        int cols = 3;
        int rows = 15;

        PdfPTable table = new PdfPTable(cols);
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                table.addCell(new Phrase("Cell " + row + ", " + col));
            }
        }

        Paragraph paragraph = new Paragraph();
        paragraph.add(table);

        ColumnText columnText = new ColumnText(canvas);
        columnText.addElement(new Paragraph("This table should keep together!"));
        columnText.addElement(paragraph);

        int status = ColumnText.START_COLUMN;

        Rectangle docBounds = document.getPageSize();

        Rectangle bounds = new Rectangle(docBounds.getLeft(20), docBounds.getTop(20) - 200, docBounds.getRight(20), docBounds.getTop(20));
        bounds.setBorder(Rectangle.BOX);
        bounds.setBorderColor(BaseColor.BLACK);
        bounds.setBorderWidth(1);
        bounds.setBackgroundColor(new BaseColor(23, 142, 255, 20));

        canvas.rectangle(bounds);

        columnText.setSimpleColumn(bounds);

        status = columnText.go();

        if (ColumnText.hasMoreText(status)) {
            bounds = new Rectangle(docBounds.getLeft(20), docBounds.getBottom(20), docBounds.getRight(20), docBounds.getBottom(20) + 600);
            bounds.setBorder(Rectangle.BOX);
            bounds.setBorderColor(BaseColor.BLACK);
            bounds.setBorderWidth(1);
            bounds.setBackgroundColor(new BaseColor(255, 142, 23, 20));

            canvas.rectangle(bounds);

            columnText.setSimpleColumn(bounds);

            status = columnText.go();
        }
    }


    private void printPage2(Document document, PdfContentByte canvas) throws DocumentException {
        int cols = 3;
        int rows = 15;

        PdfPTable table = new PdfPTable(cols);
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                table.addCell(new Phrase("Cell " + row + ", " + col));
            }
        }

        PdfPTable tableWrapper = new PdfPTable(1);
        tableWrapper.addCell(table);
        tableWrapper.setSplitRows(false);

        Paragraph paragraph = new Paragraph();
        paragraph.add(tableWrapper);

        ColumnText columnText = new ColumnText(canvas);
        columnText.addElement(new Paragraph("This table should keep together!"));
        columnText.addElement(tableWrapper);

        int status = ColumnText.START_COLUMN;

        Rectangle docBounds = document.getPageSize();

        Rectangle bounds = new Rectangle(docBounds.getLeft(20), docBounds.getTop(20) - 200, docBounds.getRight(20), docBounds.getTop(20));
        bounds.setBorder(Rectangle.BOX);
        bounds.setBorderColor(BaseColor.BLACK);
        bounds.setBorderWidth(1);
        bounds.setBackgroundColor(new BaseColor(23, 142, 255, 20));

        canvas.rectangle(bounds);

        columnText.setSimpleColumn(bounds);

        status = columnText.go();

        if (ColumnText.hasMoreText(status)) {
            bounds = new Rectangle(docBounds.getLeft(20), docBounds.getBottom(20), docBounds.getRight(20), docBounds.getBottom(20) + 600);
            bounds.setBorder(Rectangle.BOX);
            bounds.setBorderColor(BaseColor.BLACK);
            bounds.setBorderWidth(1);
            bounds.setBackgroundColor(new BaseColor(255, 142, 23, 20));

            canvas.rectangle(bounds);

            columnText.setSimpleColumn(bounds);

            status = columnText.go();
        }
    }

}

编辑:我现在意识到我在问一个解决方案,而不是问问题本身。所以我在这里退一步。

这个想法是PDF在第一页上有一个可选的信封窗口空间。该窗口将显示一个地址。 这将(第一)页面的其余部分拆分为信封窗口上方和下方的两个区域。我尝试用ColumnText来解决这个问题。 我添加了一个标题,它整齐地出现在信封窗口的上方。然后我添加了一张桌子。该表部分显示在信封窗口的上方和下方。 因此,这些字母可以包含的不仅仅是表格,还可能包含多个表格和段落。

1 个答案:

答案 0 :(得分:1)

我建议使用一个辅助方法,该方法首先尝试在模拟模式下绘制Element,然后将其绘制到适合的第一个矩形中。

这样的方法可能如下所示:

Rectangle[] drawKeepTogether(Element element, PdfContentByte canvas, Rectangle... rectangles) throws DocumentException
{
    int i = 0;
    for (; i < rectangles.length; i++)
    {
        ColumnText columnText = new ColumnText(canvas);
        columnText.addElement(element);
        columnText.setSimpleColumn(rectangles[i]);
        int status = columnText.go(true);
        if (!ColumnText.hasMoreText(status))
            break;
    }

    if (i < rectangles.length)
    {
        Rectangle rectangle = rectangles[i];
        ColumnText columnText = new ColumnText(canvas);
        columnText.addElement(element);
        columnText.setSimpleColumn(rectangle);
        columnText.go(false);

        Rectangle[] remaining = new Rectangle[rectangles.length-i];
        System.arraycopy(rectangles, i, remaining, 0, remaining.length);
        remaining[0] = new Rectangle(rectangle.getLeft(), rectangle.getBottom(), rectangle.getRight(), columnText.getYLine());
        return remaining;
    }

    return new Rectangle[0];
}

TableKeepTogether.java方法drawKeepTogether

如果这样使用:

private void printPage3(Document document, PdfContentByte canvas) throws DocumentException {
    int cols = 3;
    int rows = 15;

    PdfPTable table = new PdfPTable(cols);
    for (int row = 0; row < rows; row++) {
        for (int col = 0; col < cols; col++) {
            table.addCell(new Phrase("Cell " + row + ", " + col));
        }
    }
    table.setSpacingBefore(5);

    Rectangle docBounds = document.getPageSize();
    Rectangle upper = new Rectangle(docBounds.getLeft(20), docBounds.getTop(20) - 200, docBounds.getRight(20), docBounds.getTop(20));
    upper.setBackgroundColor(new BaseColor(23, 142, 255, 20));
    Rectangle lower = new Rectangle(docBounds.getLeft(20), docBounds.getBottom(20), docBounds.getRight(20), docBounds.getBottom(20) + 600);
    lower.setBackgroundColor(new BaseColor(255, 142, 23, 20));
    Rectangle[] rectangles = new Rectangle[] { upper, lower };

    for (Rectangle bounds : rectangles)
    {
        bounds.setBorder(Rectangle.BOX);
        bounds.setBorderColor(BaseColor.BLACK);
        bounds.setBorderWidth(1);

        canvas.rectangle(bounds);
    }

    rectangles = drawKeepTogether(new Paragraph("This table should keep together!"), canvas, rectangles);
    rectangles = drawKeepTogether(table, canvas, rectangles);
}

TableKeepTogether.java方法printPage3

此方法生成的页面如下所示:

keepTogetherTableInColumnText.pdf page 3