签署文档时PDFBox 2.0.8问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了签署文档时PDFBox 2.0.8问题相关的知识,希望对你有一定的参考价值。

我正在尝试使用此方法签署pdf,但获取没有大小的文档:

public static void sign(PDDocument doc) throws KeyStoreException, NoSuchAlgorithmException, CertificateException,
        IOException, UnrecoverableKeyException {
    System.out.println("Document pages ? " + doc.getNumberOfPages());
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(VisibleSignature.class.getResourceAsStream(CERT_FILE), ALIAS_PASS);
    System.out.println("KeyStore is null ? " + (ks == null));
    VisibleSignature vs = new VisibleSignature(ks, ALIAS_PASS.clone());
    InputStream is = Resource.get(IMAGE_FILE);
    int page = 1;
    vs.setVisibleSignDesigner(doc, 0, 0, -50, is, page);
    is.close();
    vs.setVisibleSignatureProperties("Test", "Test", "Test", 0, page, true);
    PDSignature signature = new PDSignature();
    PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
    System.out.println("Acroform is null ? " + (acroForm == null));
    System.out.println("Acroform getNeedAppearances ? " + (acroForm.getNeedAppearances()));
    if (acroForm != null && acroForm.getNeedAppearances())
        if (acroForm.getFields().isEmpty())
            acroForm.getCOSObject().removeItem(COSName.NEED_APPEARANCES);
        else
            System.out.println("/NeedAppearances is set, signature may be ignored by Adobe Reader");
    signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
    signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
    if (vs.visibleSignatureProperties != null) {
        vs.visibleSignatureProperties.buildSignature();
        signature.setName(vs.visibleSignatureProperties.getSignerName());
        signature.setLocation(vs.visibleSignatureProperties.getSignerLocation());
        signature.setReason(vs.visibleSignatureProperties.getSignatureReason());
        System.out.println("SignerName " + vs.visibleSignatureProperties.getSignerName());
    }
    signature.setSignDate(Calendar.getInstance());
    vs.signatureOptions = new SignatureOptions();
    vs.signatureOptions.setVisualSignature(vs.visibleSignatureProperties.getVisibleSignature());
    vs.signatureOptions.setPage(vs.visibleSignatureProperties.getPage() - 1);
    doc.addSignature(signature, vs.signatureOptions);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    doc.saveIncremental(baos);
    doc.close();
    IOUtils.closeQuietly(vs.signatureOptions);
    byte[] content = baos.toByteArray();
    System.out.println("Content length: >>>>>>>>>>>>>>>>>>> " + content.length);
}

这就是我在eclipse日志中得到的:

18:50:25,702 INFO [默认任务-14]标准输出 - 文档页面? 1

18:50:25,740 INFO [default task-14] stdout - KeyStore为空?假

18:50:25,779 INFO [默认任务-14]标准输出 - Acroform为空?假

18:50:25,780 INFO [默认任务-14]标准输出 - Acroform getNeedAppearances?假

18:50:25,782 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - PDF结构已创建

18:50:25,782 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDFTemplateCreator - pdf building已经启动

18:50:25,782 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 已创建ProcSet数组

18:50:25,782 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 已创建PDF页面

18:50:25,783 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - AcroForm已创建

18:50:25,788 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 已创建签名字段

18:50:25,788 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 已创建PDSignature

18:50:25,788 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 已创建AcroForm字典

18:50:25,789 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - Matrix已添加

18:50:25,792 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 已创建签名矩形

18:50:25,793 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 格式化矩形已创建

18:50:25,815 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 可见签名图像已创建

18:50:25,815 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 已创建Holder表单流

18:50:25,816 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 已创建Holder表单资源

18:50:25,816 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 已创建Holder表单

18:50:25,816 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 已创建PDF外观词典

18:50:25,817 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 另一种形式的流(内部形式 - 它将在持有者形式内)已创建

18:50:25,817 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 已创建另一种形式的资源(内部形式 - 它将在持有者形式内)

18:50:25,817 INFO [默认任务-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 另一种形式(内部形式 - 它将在持有者形式内)已创建

18:50:25,817 INFO [默认任务-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 现在在持有人表单中插入内部表单

18:50:25,817 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 创建的图像表单流

18:50:25,817 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 创建的图像表单资源

18:50:25,818 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 创建的图像表单

18:50:25,818 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 创建的背景图层表单

18:50:25,818 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 将ProcSet插入PDF

18:50:25,818 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 注入的外观流到pdf

18:50:25,818 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - 已创建可见签名

18:50:25,819 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDVisibleSigBuilder - WidgetDictionary已创建

18:50:25,825 DEBUG [default task-14] org.apache.pdfbox.cos.COSStream - 创建InputStream,调用之前没有数据写入流。

18:50:25,825 INFO [default task-14] org.apache.pdfbox.pdmodel.interactive.digitalsignature.visible.PDFTemplateCreator - stream returns started,size = 21301

18:50:25,825 INFO [默认任务-14]标准输出 - SignerName测试

18:50:25,857 INFO [默认任务-14]标准输出 - 内容长度:>>>>>>>>>>>>>>>>>>> 0

有谁知道这里发生了什么?

答案

在您的代码中,您没有为文档提供可用于签名的SignatureInterface实现。在此用例中没有提供使PDFBox假设您将在外部创建签名(例如,查看原始CreateVisibleSignature方法sign)。在这种情况下,使用ExternalSigningSupport方法setSignature设置签名后,结果将写入输出流。如果你不这样做,你的ByteArrayOutputStream baos仍然是空的。

但是你确实有一个SignatureInterface实例,你已经用证书和密钥材料初始化了:你的VisibleSignature vs。因此,我认为你没有提供SignatureInterface不是故意的。

因此,要提供SignatureInterface,您必须使用带有PDDocument.addSignature参数的SignatureInterface重载。例如。替换你的电话

doc.addSignature(signature, vs.signatureOptions);

通过

doc.addSignature(signature, vs, vs.signatureOptions);

使您的代码按需运行。

以上是关于签署文档时PDFBox 2.0.8问题的主要内容,如果未能解决你的问题,请参考以下文章

pdfbox 操作 pdf 文档 - android

使用 PDFbox 确定文档中单词的坐标

PDFBOX 从我的 html 字段中填写文档

在 pdfbox 2.00 中使用 PDFMergerUtility 合并文档

使用 PDFBox 从 PDF 文档中读取特定页面

获取PDFBox中字符的字体高度