Itext - 如何使用acrofields克隆页面? [英] Itext - How to clone pages with acrofields?

查看:879
本文介绍了Itext - 如何使用acrofields克隆页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用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!

推荐答案

我花了一些时间才弄明白这一点。这不是最有效的编码方式,但基本上它的作用是:

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


  • 创建文档

  • 对于每个页面都有一个acrofield:

  • 复制你的模板

  • 填写表格

  • 展平表格

  • 添加页面

  • 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();
}

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

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.

此外,我发现在基于Intel的Mac与Windows计算机上,Mac完成了这么多快点。

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

如果您不反对获取iText的参考书,我会推荐Bruno Lowagie撰写的iText in Action,Second Edition。这是一本很棒的书,非常有帮助。

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.

这篇关于Itext - 如何使用acrofields克隆页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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