在同一页面上将表添加到现有PDF - ITEXT [英] Adding table to existing PDF on the same page - ITEXT

查看:243
本文介绍了在同一页面上将表添加到现有PDF - ITEXT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的java项目有两个部分。

I have two parts to my java project.


  • 我需要填写pdf的字段

  • 我需要在下面添加一个表格页面空白区域的填充部分(此表需要能够翻转到下一页)。

我是能够单独做这些事情(填充pdf并创建一个表)。但我无法有效地合并它们。我试过做一个doc.add(table),这会导致表格在pdf的下一页上,这是我不想要的。

I am able to do these things separately (populate the pdf and create a table). 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.

我本质上只需要能够指定表在页面上的起始位置(因此它不会与现有内容重叠),然后将表格标记到现有的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).

有任何建议吗?

编辑:

我是iText的新手并且没有使用过columntext之前,但我试图在下面的代码中测试它,但表没有显示。我查看了其他列文本示例,我还没有看到将columntext添加回pdf的确切位置。

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. I looked at other columntext examples and I have not seen exactly where the columntext is added back into the 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();


推荐答案

请看一下 AddExtraTable 示例。它简化了 AddExtraPage 回答问题的例子如何继续现场输出在第二页上?

Please take a look at the AddExtraTable example. It's a simplification of the AddExtraPage example written in answer to the question How to continue field output on a second page?

这个问题几乎与您的问题完全重复,唯一不同的是您的要求更容易实现。

That question is almost an exact duplicate of your question, with as only difference the fact that your requirement is easier to achieve.

我简化了这样的代码:

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. 我们为第一页创建 rectPage1 ,并为 rectPage2 和后面的所有页面。那是因为我们不需要第一页上的整页。

  2. 我们不需要加载 PdfImportedPage ,而是我们只是添加与第一页相同大小的空白页。

  1. We create a rectPage1 for the first page and a rectPage2 for page 2 and all pages that follow. 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.

可能的改进:我硬编码 Rectangle 实例。毫无疑问, rect1Page 取决于原始表单的位置。我还硬编码 rect2Page 。如果我有更多时间,我会根​​据 pagesize 值计算 rect2Page

Possible improvements: I hardcoded the Rectangle instances. It goes without saying that rect1Page depends on the location of your original form. I also hardcoded rect2Page. If I had more time, I would calculate rect2Page based on the pagesize value.

请参阅官方常见问题解答的以下问题和答案:

See the following questions and answers of the official FAQ:

  • How to add a table on a form (and maybe insert a new page)?
  • How to continue field output on a second page?

这篇关于在同一页面上将表添加到现有PDF - ITEXT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆