如何继续场输出在第二页上? [英] How to continue field output on a second page?

查看:198
本文介绍了如何继续场输出在第二页上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经生成的模板的PDF文件。的PDF具有在它的中间是可变长度的字段。我试图去解决它,这样,如果该字段的内容溢出,那么程序将使用第二个实例模板作为第二页,并继续在同一领域出现。这可能吗?

I have generated a PDF from a template. The PDF has a field in the middle of it that is of variable length. I am trying to work it so that if the field's content overflows, then the program will use a second instance template as a second page and continue in the same field there. Is this possible?

推荐答案

这将仅当您扁平化的形式工作。我已经写了一个概念证明,我有一个PDF格式的src ,有一个名为field 身体

This will only work if you flatten the form. I've written a proof of concept where I have a PDF form src that has a field named "body":

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));
    Paragraph p = new Paragraph();
    p.add(new Chunk("Hello "));
    p.add(new Chunk("World", new Font(FontFamily.HELVETICA, 12, Font.BOLD)));
    AcroFields form = stamper.getAcroFields();
    Rectangle rect = form.getFieldPositions("body").get(0).position;
    int status;
    PdfImportedPage newPage = null;
    ColumnText column = new ColumnText(stamper.getOverContent(1));
    column.setSimpleColumn(rect);
    int pagecount = 1;
    for (int i = 0; i < 100; ) {
        i++;
        column.addElement(new Paragraph("Hello " + i));
        column.addElement(p);
        status = column.go();
        if (ColumnText.hasMoreText(status)) {
            newPage = loadPage(newPage, reader, stamper);
            triggerNewPage(stamper, pagesize, newPage, column, rect, ++pagecount);
        }
    }
    stamper.setFormFlattening(true);
    stamper.close();
    reader.close();
}

public PdfImportedPage loadPage(PdfImportedPage page, PdfReader reader, PdfStamper stamper) {
    if (page == null) {
        return stamper.getImportedPage(reader, 1);
    }
    return page;
}

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

正如你所看到的,我们创建了一个 PdfImportedPage 实例,我们插入一个新的页面,此页面为背景。我们添加的内容,在使用 Col​​umnText

As you can see, we create a PdfImportedPage instance and we insert a new page with this page as background. We add the content at the position defined by the field using ColumnText.

这篇关于如何继续场输出在第二页上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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