1

我必须填写具有 xfa 字段并为此使用 iText 的 pdf 表单(用于在线提交数据)。我能够生成支持阅读器的 pdf 文档,但未填写字段。

请建议我怎样才能让它工作。

4

1 回答 1

1

你只需要这个:

private void fillXmlInPdf(File xmlFile, File inputPdf, File outputPdf) throws IOException, DocumentException, FileNotFoundException, CsmartException {
    PdfStamper stamper=null;
    try {
        PdfReader reader = new PdfReader(inputPdf.getAbsolutePath());
        stamper = new PdfStamper(reader, new FileOutputStream(outputPdf), '\0', true);
        AcroFields afields = stamper.getAcroFields();
        XfaForm xfa = afields.getXfa();
        xfa.fillXfaForm(new FileInputStream(xmlFile));
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        try {
            stamper.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这段代码对我很有效......

于 2011-10-20T14:14:11.627 回答