简体   繁体   English

使用iText7在现有PDF的特定页面中添加表格

[英]Using iText7 to add a table in a specific page of an existing PDF

I'm trying to add a dynamically created table to a dynamically created existing PDF. 我正在尝试将动态创建的表添加到动态创建的现有PDF。 I want to "append" the table to a specific position on the last page of the existing PDF. 我想将表格“追加”到现有PDF 最后一页的特定位置。

My current code is based on the answer given by Uladzimir Asipchuk on this thread, it goes like this: 我当前的代码基于Uladzimir Asipchuk在线程上给出的答案,它看起来像这样:

public byte[] completePDF(byte[] originalPDF){

    ByteArrayInputStream is = new ByteArrayInputStream(originalPDF);
    ByteArrayOutputStream os = new ByteArrayOutputStream();

    PdfDocument pdfDoc;
    try {
        pdfDoc = new PdfDocument(new PdfReader(is), new PdfWriter(os));
        final Document doc = new Document(pdfDoc);
         //FAKE TABLE TO TEST
        Table table = new Table(UnitValue.createPercentArray(new float[] {10f,90f}));

        for (int i = 0; i<150; i++){

            table.addCell(new String(new Integer(i).toString()));
            table.addCell("FAKE DATA");
        }

        doc.setRenderer(new DocumentRenderer(doc) {
            @Override
            protected LayoutArea updateCurrentArea(LayoutResult overflowResult) {

                int lastPage = doc.getPdfDocument().getNumberOfPages();

                LayoutArea area = super.updateCurrentArea(overflowResult);

                if (area.getPageNumber() == lastPage) {
                    PdfDocumentContentParser parser = new PdfDocumentContentParser(doc.getPdfDocument());
                    TextMarginFinder finder = parser.processContent(lastPage, new TextMarginFinder());
                    area.getBBox().decreaseHeight(finder.getTextRectangle().getHeight() + 30);
                }
                return area;
            }
        });

        doc.add(table);

        doc.close();
    } catch (Exception e) {
        return null;
    }

    return os.toByteArray();

}

This code does almost what I want, (new pages are added based on the table size, and the table position on the last page starts after the text of that page), but I don't know of to force the table to start getting drawn on the last page. 这段代码几乎可以实现我想要的功能(根据表的大小添加新页面,并且最后一页的表位置在该页面的文本之后开始),但是我不知道要强制该表开始获取在最后一页上绘制。

Is is possible? 有可能吗? Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks! 谢谢!

Found the solution by myself, I was really close to it. 我自己找到了解决方案,我真的很了解。 I'll post it just in case someone has the same problem: 我将其发布,以防万一有人遇到相同的问题:

if (area.getPageNumber() == lastPage) {
    PdfDocumentContentParser parser = new PdfDocumentContentParser(doc.getPdfDocument());
    TextMarginFinder finder = parser.processContent(lastPage, new TextMarginFinder());
    area.getBBox().decreaseHeight(finder.getTextRectangle().getHeight() + 30);
} else if (area.getPageNumber() < lastPage){
    area.getBBox().decreaseHeight(doc.getPdfDocument().getDefaultPageSize().getHeight());
}
return area;

This way the table gets "appended" just after the end of the text of the last page of the PDF. 这样,表格就紧接在PDF最后一页的文本结尾之后被“追加”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM