简体   繁体   English

在同一页面上将表添加到现有PDF-ITEXT

[英]Adding table to existing PDF on the same page - ITEXT

I have two parts to my java project. 我的java项目分为两部分。

  • I need to populate the fields of a pdf 我需要填充pdf的字段
  • I need to add a table below the populated section on the blank area of the page (and this table needs to be able to rollover to the next page). 我需要在页面空白区域的填充部分下方添加一个表(并且该表需要能够滚动到下一页)。

I am able to do these things separately (populate the pdf and create a table). 我能够分别进行这些操作(填充pdf并创建表格)。 But I cannot effectively merge them. 但是我无法有效地合并它们。 I have tried doing a doc.add(table) which will result in the table being on the next page of the pdf, which I don't want. 我尝试做一个doc.add(table),这将导致该表在pdf的下一页上,这是我不想要的。

I essentially just need to be able to specify where the table starts on the page (so it wouldn't overlap the existing content) and then stamp the table onto the existing pdf. 我基本上只需要能够指定表格在页面上的起始位置(这样就不会与现有内容重叠),然后将表格标记到现有pdf上。

My other option if this doesn't work is trying to add fields to the original pdf that will be filled by the table contents (so it will instead be a field-based table). 我的另一个选择(如果这样不起作用)是尝试将字段添加到将由表内容填充的原始pdf中(因此它将改为基于字段的表)。

Any suggestions? 有什么建议么?

EDIT: 编辑:

I'm new to iText and have not used columntext before, but I'm trying to test it out in the following code but the table is not being displayed. 我是iText的新手,以前没有使用columntext,但是我正在尝试在以下代码中对其进行测试,但未显示该表。 I looked at other columntext examples and I have not seen exactly where the columntext is added back into the pdf. 我看了其他columntext示例,但没有确切看到columntext添加回pdf的位置。

//CREATE FILLED FORM PDF
PdfReader reader = new PdfReader(sourcePath);  
PdfStamper pdfStamper = new PdfStamper(reader, new FileOutputStream(destPath));
pdfStamper.setFormFlattening(true);
AcroFields form = pdfStamper.getAcroFields();

form.setField("ID", "99999");
form.setField("ADDR1", "425 Test Street");
form.setField("ADDR2", "Test, WA 91334");
form.setField("PHNBR", "(999)999-9999");
form.setField("NAME", "John Smith");

//CREATE TABLE
PdfPTable table = new PdfPTable(3);
Font bfBold12 = new Font(FontFamily.HELVETICA, 12, Font.BOLD, new BaseColor(0, 0, 0)); 
insertCell(table, "Table", Element.ALIGN_CENTER, 1, bfBold12);
table.completeRow(); 

ColumnText column = new ColumnText(pdfStamper.getOverContent(1));
column.addElement(table);

pdfStamper.close();
reader.close();

Please take a look at the AddExtraTable example. 请看一下AddExtraTable示例。 It's a simplification of the AddExtraPage example written in answer to the question How to continue field output on a second page? 它是为回答“ 如何继续第二页上的字段输出 ”问题而编写的AddExtraPage示例的简化形式

That question is almost an exact duplicate of your question, with as only difference the fact that your requirement is easier to achieve. 该问题几乎与您的问题完全相同,唯一的不同是您的要求更容易实现。

I simplified the code like this: 我简化了这样的代码:

public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
    PdfReader reader = new PdfReader(src);
    Rectangle pagesize = reader.getPageSize(1);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    AcroFields form = stamper.getAcroFields();
    form.setField("Name", "Jennifer");
    form.setField("Company", "iText's next customer");
    form.setField("Country", "No Man's Land");
    PdfPTable table = new PdfPTable(2);
    table.addCell("#");
    table.addCell("description");
    table.setHeaderRows(1);
    table.setWidths(new int[]{ 1, 15 });
    for (int i = 1; i <= 150; i++) {
        table.addCell(String.valueOf(i));
        table.addCell("test " + i);
    }
    ColumnText column = new ColumnText(stamper.getOverContent(1));
    Rectangle rectPage1 = new Rectangle(36, 36, 559, 540);
    column.setSimpleColumn(rectPage1);
    column.addElement(table);
    int pagecount = 1;
    Rectangle rectPage2 = new Rectangle(36, 36, 559, 806);
    int status = column.go();
    while (ColumnText.hasMoreText(status)) {
        status = triggerNewPage(stamper, pagesize, column, rectPage2, ++pagecount);
    }
    stamper.setFormFlattening(true);
    stamper.close();
    reader.close();
}

public int triggerNewPage(PdfStamper stamper, Rectangle pagesize, ColumnText column, Rectangle rect, int pagecount) throws DocumentException {
    stamper.insertPage(pagecount, pagesize);
    PdfContentByte canvas = stamper.getOverContent(pagecount);
    column.setCanvas(canvas);
    column.setSimpleColumn(rect);
    return column.go();
}

As you can see, the main differences are: 如您所见,主要区别在于:

  1. We create a rectPage1 for the first page and a rectPage2 for page 2 and all pages that follow. 我们为第一页创建一个rectPage1 ,为第二页和rectPage2的所有页面创建一个rectPage1 That's because we don't need a full page on the first page. 那是因为我们不需要第一页上的整页。
  2. We don't need to load a PdfImportedPage , instead we're just adding blank pages of the same size as the first page. 我们不需要加载PdfImportedPage ,而是只添加与第一页相同大小的空白页。

Possible improvements: I hardcoded the Rectangle instances. 可能的改进:我对Rectangle实例进行了硬编码。 It goes without saying that rect1Page depends on the location of your original form. rect1Pagerect1Page取决于原始表单的位置。 I also hardcoded rect2Page . 我还对rect2Page硬编码。 If I had more time, I would calculate rect2Page based on the pagesize value. 如果我有更多时间,我将根据pagesize值计算rect2Page

See the following questions and answers of the official FAQ: 请参阅官方常见问题解答的以下问题和答案:

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

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