PdfCopy和带有iText的表单值:表单值不可见 [英] PdfCopy and form values with iText: form values not visible

查看:208
本文介绍了PdfCopy和带有iText的表单值:表单值不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iText的第6章介绍了如何使用PdfSmartCopy/PdfCopy复制页面:

Chapter 6 of iText in action describes how to replicate a page using PdfSmartCopy / PdfCopy:

public void addDataSheets(PdfCopy copy)
        throws SQLException, IOException, DocumentException {
        // Create a database connection
        DatabaseConnection connection = new HsqldbConnection("filmfestival");
        List<Movie> movies = PojoFactory.getMovies(connection);
        PdfReader reader;
        PdfStamper stamper;
        ByteArrayOutputStream baos;
        // Loop over all the movies and fill out the data sheet
        for (Movie movie : movies) {
            reader = new PdfReader(DATASHEET);
            baos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, baos);
            fill(stamper.getAcroFields(), movie);
            stamper.setFormFlattening(true);
            stamper.close();

            reader = new PdfReader(baos.toByteArray());
            copy.addPage(copy.getImportedPage(reader, 1));
        }
        // Close the database connection
        connection.close();
    }

这很好用,但是在我新创建的文档上,除非我单击它,否则表单字段内的值是不可见的.如果我在Chrome中打开PDF,则可以看到表单值.

This works great, but on my newly created document, the values inside the form fields are not visible unless I click on it. If I open the PDF in Chrome I can see the form values.

显然来自可编辑保存后,.pdf字段消失(但在字段焦点上可见),结果是在pdf上需要设置一个标志.

Apparently from Editable .pdf fields disappear (but visible on field focus) after save with evince, it comes out that there is a Flag which needs to be set on the pdf.

public void createPdf(String filename)
    throws IOException, DocumentException, SQLException {
    // step 1
    Document document = new Document();
    // step 2
    PdfCopy copy
        = new PdfCopy(document, new FileOutputStream(filename));
    // step 3
    document.open();
    // step 4
    addDataSheets(copy);
    // step 5
    document.close();
}

是否有一种方法可以使用当前的API,而无需使用反射或在pdf中重新打开?

Is there a way to do it with the current API, without using reflection or re-openin the pdf?

推荐答案

请查看更新的

Please take a look at the updated FillDataSheet example on the iText web site. You'll discover that the following line was added:

fields.setGenerateAppearances(true);

iText过去常常忽略此标志并始终创建外观,即使PDF明确表示不需要创建外观也是如此.最新版本会考虑该标志的值,并且不会创建外观,以防PDF表示不需要外观(在您的PDF中可能就是这种情况).

iText used to ignore this flag and always created appearances, even if the PDF explicitly said that no appearances needed to be created. More recent versions take the value of the flag into account and don't create appearances in case the PDF says that no appearances are needed (which is probably the case in your PDF).

这篇关于PdfCopy和带有iText的表单值:表单值不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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