Itext - 如何克隆带有 acrofields 的页面?

发布于 2024-11-04 00:54:20 字数 277 浏览 1 评论 0原文

我正在用 Java 编写一个程序来打印宾果卡的 PDF 文件。每页都是一张卡片。为了方便起见,我创建了一个带有 acrofields 的模板 PDF 文件,这样程序只需要创建该模板的副本,用数字填充 acrofields,然后将其展平。截至目前,我可以创建 1 张宾果卡。我想在一个 PDF 文件中包含多个页面(即多张卡片)。但我不知道该怎么做。我读到的是 PDFStamper 与唯一一个 PDFReader 对象关联。有没有一种方法可以做到这一点,而无需创建多个 PDF 文件并将它们合并为一个(我上次这样做了,我发现非常慢)提前致谢!

I'm writing a program in Java that prints PDF files of Bingo Cards. Each page is one card. To make it easy for me, I created a template PDF file with acrofields, so that the program will only need to create a copy of this template, fill the acrofields with numbers, then flatten it. As of now, I can create 1 bingo card. I want to have multiple pages (thus, multiple cards) in one PDF file. But I do not have an idea how to do this. What I read is that a PDFStamper is associated to a one and only one PDFReader object. Is there a way I can do this without resorting to creation of multiple PDF files and combining them into one (I did this last time and I found really slow) Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声静候 2024-11-11 00:54:20

我花了一段时间才弄清楚这一点。这不是最有效的编码方式,但本质上它的作用如下:

  • 创建一个文档
  • 使用 acrofield 为每个页面
  • :复制模板
  • 填充表单
  • 压平表单
  • 添加页面

这是我的实现,您可以尝试修改以满足您的需求:

private void createPdf() throws Exception {
    Document doc = new Document();
    PdfSmartCopy copy = new PdfSmartCopy(doc, new FileOutputStream("result.pdf"));
    doc.open();

    PdfReader reader;
    PdfStamper stamper;
    AcroFields form;
    ByteArrayOutputStream baos;

    for(int i = 0; i < getTotalPages(); i++) {
        copyPdf(i);

        reader = new PdfReader(String.format("%d%s", i, "template.pdf"));
        baos = new ByteArrayOutputStream();
        stamper = new PdfStamper(reader, baos);
        form = stamper.getAcroFields();

        //methods to fill forms

        stamper.setFormFlattening(true);
        stamper.close();

        reader = new PdfReader(baos.toByteArray());
        copy.addPage(copy.getImportedPage(reader, 1));
    }

    doc.close();
}

private void copyPdf(int currentPage) throws Exception {
    PdfReader reader = new PdfReader("timesheet.pdf");
    Document doc = new Document();
    File file = new File(String.format("%d%s", currentPage, "template.pdf"));
    file.deleteOnExit();
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(file));
    stamper.close();
}

copyPdf() 方法创建临时文件,用于允许填写表单而不影响整个文档。如果您找到更有效的方法来做到这一点,请告诉我。

另外,我发现在基于 Intel 的 Mac 与 Windows 计算机上,Mac 完成此操作的速度要快得多。

如果您不反对购买 iText 参考书,我会推荐 Bruno Lowagie 的“iText in Action,第二版”。这是一本很棒的书,而且非常有帮助。

Took me a while to figure this out. It's not the most efficient way to code, but here's essentially what it does:

  • create a document
  • for each page(s) with an acrofield:
  • copy your template
  • fill the form
  • flatten the form
  • add the page

Here's my implementation that you can try and modify to fit your needs:

private void createPdf() throws Exception {
    Document doc = new Document();
    PdfSmartCopy copy = new PdfSmartCopy(doc, new FileOutputStream("result.pdf"));
    doc.open();

    PdfReader reader;
    PdfStamper stamper;
    AcroFields form;
    ByteArrayOutputStream baos;

    for(int i = 0; i < getTotalPages(); i++) {
        copyPdf(i);

        reader = new PdfReader(String.format("%d%s", i, "template.pdf"));
        baos = new ByteArrayOutputStream();
        stamper = new PdfStamper(reader, baos);
        form = stamper.getAcroFields();

        //methods to fill forms

        stamper.setFormFlattening(true);
        stamper.close();

        reader = new PdfReader(baos.toByteArray());
        copy.addPage(copy.getImportedPage(reader, 1));
    }

    doc.close();
}

private void copyPdf(int currentPage) throws Exception {
    PdfReader reader = new PdfReader("timesheet.pdf");
    Document doc = new Document();
    File file = new File(String.format("%d%s", currentPage, "template.pdf"));
    file.deleteOnExit();
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(file));
    stamper.close();
}

The copyPdf() method creates temporary files that are used to allow form filling without affecting the entire document. If you find a more efficient way to do this, let me know.

Also, I've found that on Intel Based Mac vs Windows Computer, the Mac completes this much faster.

If you're not opposed to getting a reference book for iText, I would recommend "iText in Action, Second Edition" by Bruno Lowagie. It is a great book and very helpful.

醉态萌生 2024-11-11 00:54:20

因此,这是不使用 Zach 的“copyPdf”方法的代码,正如 Mark Storer 和 MaxArt 所建议的:

private void createPdf() throws Exception {
    Document doc = new Document();
    PdfSmartCopy copy = new PdfSmartCopy(doc, new FileOutputStream("result.pdf"));
    doc.open();
    
    PdfReader mainReader = new PdfReader("timesheet.pdf");
    
    PdfReader reader;
    ByteArrayOutputStream baos;
    PdfStamper stamper;
    AcroFields form;
    
    for(int i = 0; i < getTotalPages(); i++) {
        
        reader = new PdfReader(mainReader);
        baos = new ByteArrayOutputStream();
        stamper = new PdfStamper(reader, baos);
        form = stamper.getAcroFields();
        
        //methods to fill forms
        
        stamper.setFormFlattening(true);
        stamper.close();
        
        reader = new PdfReader(baos.toByteArray());
        copy.addPage(copy.getImportedPage(reader, 1));
    }

    doc.close();
}

So, here's the code without using Zach's "copyPdf" method, as Mark Storer and MaxArt suggested:

private void createPdf() throws Exception {
    Document doc = new Document();
    PdfSmartCopy copy = new PdfSmartCopy(doc, new FileOutputStream("result.pdf"));
    doc.open();
    
    PdfReader mainReader = new PdfReader("timesheet.pdf");
    
    PdfReader reader;
    ByteArrayOutputStream baos;
    PdfStamper stamper;
    AcroFields form;
    
    for(int i = 0; i < getTotalPages(); i++) {
        
        reader = new PdfReader(mainReader);
        baos = new ByteArrayOutputStream();
        stamper = new PdfStamper(reader, baos);
        form = stamper.getAcroFields();
        
        //methods to fill forms
        
        stamper.setFormFlattening(true);
        stamper.close();
        
        reader = new PdfReader(baos.toByteArray());
        copy.addPage(copy.getImportedPage(reader, 1));
    }

    doc.close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文