使用iText 7使用表单字段创建PDF / A-3会导致PdfAConformanceException

时间:2018-07-16 13:50:10

标签: java pdf itext7

我想使用 iText 7(实际上是7.0.7)创建带有表单字段的 PDF / A-3 文件。

我检查了示例和跳转教程。

here中添加表单字段后 像显示的here一样,出现以下错误:

  

线程“ main”中的异常com.itextpdf.pdfa.PdfAConformanceException:注释字典应包含f键       在com.itextpdf.pdfa.checker.PdfA2Checker.checkAnnotation(PdfA2Checker.java:336)       在com.itextpdf.pdfa.checker.PdfAChecker.checkAnnotations(PdfAChecker.java:467)       在com.itextpdf.pdfa.checker.PdfAChecker.checkPage(PdfAChecker.java:446)       在com.itextpdf.pdfa.checker.PdfAChecker.checkPages(PdfAChecker.java:434)       在com.itextpdf.pdfa.checker.PdfAChecker.checkDocument(PdfAChecker.java:182)       在com.itextpdf.pdfa.PdfADocument.checkIsoConformance(PdfADocument.java:296)       在com.itextpdf.kernel.pdf.PdfDocument.close(PdfDocument.java:742)       在com.itextpdf.layout.Document.close(Document.java:120)       在pdfatest.C07E03_UnitedStates_PDFA_3a.createPdf(C07E03_UnitedStates_PDFA_3a.java:158)       在pdfatest.C07E03_UnitedStates_PDFA_3a.main(C07E03_UnitedStates_PDFA_3a.java:40)

这是修改后的示例代码:

public void createPdf(String dest) throws IOException {
    PdfADocument pdf = new PdfADocument(new PdfWriter(dest),
            PdfAConformanceLevel.PDF_A_3A,
            new PdfOutputIntent("Custom", "", "http://www.color.org",
                    "sRGB IEC61966-2.1", new FileInputStream(INTENT)));
    Document document = new Document(pdf, PageSize.A4.rotate());
    document.setMargins(20, 20, 20, 20);

    //Setting some required parameters
    pdf.setTagged();
    pdf.getCatalog().setLang(new PdfString("en-US"));
    pdf.getCatalog().setViewerPreferences(
            new PdfViewerPreferences().setDisplayDocTitle(true));
    PdfDocumentInfo info = pdf.getDocumentInfo();
    info.setTitle("iText7 PDF/A-3 example");

    //Add attachment
    PdfDictionary parameters = new PdfDictionary();
    parameters.put(PdfName.ModDate, new PdfDate().getPdfObject());
    PdfFileSpec fileSpec = PdfFileSpec.createEmbeddedFileSpec(
            pdf, Files.readAllBytes(Paths.get(DATA)), "united_states.csv",
            "united_states.csv", new PdfName("text/csv"), parameters,
            PdfName.Data, false);
    fileSpec.put(new PdfName("AFRelationship"), new PdfName("Data"));
    pdf.addFileAttachment("united_states.csv", fileSpec);
    PdfArray array = new PdfArray();
    array.add(fileSpec.getPdfObject().getIndirectReference());
    pdf.getCatalog().put(new PdfName("AF"), array);

    //Embed fonts
    PdfFont font = PdfFontFactory.createFont(FONT, true);
    PdfFont bold = PdfFontFactory.createFont(BOLD_FONT, true);

    // Create content
    Table table = new Table(new float[]{4, 1, 3, 4, 3, 3, 3, 3, 1});
    table.setWidthPercent(100);
    BufferedReader br = new BufferedReader(new FileReader(DATA));
    String line = br.readLine();
    process(table, line, bold, true);
    while ((line = br.readLine()) != null) {
        process(table, line, font, false);
    }
    br.close();
    document.add(table);

// START additional code to add a form field
    PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
    PdfFormField textFormField = PdfFormField.createText(
            pdf,
            new Rectangle(50, 50, 200, 15),
            "vo-1-text", "bla", font, 12.0f);

    form.addField(textFormField);
// END additional code to add a form field

    //Close document
    document.close();
}

我想念什么吗?

1 个答案:

答案 0 :(得分:2)

如果可以切换到iText 7.1.x,则相对容易:createText有一个新的重载,它允许您设置PdfAConformanceLevel参数,因此

PdfFormField textFormField = PdfFormField.createText(
        pdf,
        new Rectangle(50, 50, 200, 15),
        "vo-1-text", "bla", font, 12.0f,
        false, // multiline parameter
        PdfAConformanceLevel.PDF_A_3A // <-- this is the important one
);

如果您无法或不会切换到iText 7.1,则可以尝试手动触发将设置该标志的代码:

for (PdfWidgetAnnotation wid : textFormField.getWidgets()) {
    wid.setFlag(PdfAnnotation.PRINT);
}

我不确定最后一种方法是否可行,因为您可能需要显式定义PdfWidgetAnnotation,但是使用iText 7.1则容易得多。