【问题标题】:PDFBox - sign PDF have existing signed by anotherPDFBox - 签署 PDF 已由另一个人签署
【发布时间】:2021-09-13 08:25:41
【问题描述】:

正在尝试对已使用 PDFBox 签名的 PDF 进行签名

使用此测试功能:

/**
     * Test creating visual signature with the modernized example.
     *
     * @throws IOException
     * @throws CMSException
     * @throws OperatorCreationException
     * @throws GeneralSecurityException
     * @throws TSPException
     * @throws CertificateVerificationException
     */
    @ParameterizedTest
    @MethodSource("signingTypes")
    void testCreateVisibleSignature2(boolean externallySign)
            throws IOException, CMSException, OperatorCreationException, GeneralSecurityException,
                   TSPException, CertificateVerificationException
    {
        // sign PDF
        String inPath = IN_DIR + "pdf.pdf";
        File destFile;

        CreateVisibleSignature2 signing = new CreateVisibleSignature2(keyStore, PASSWORD.toCharArray());
        Rectangle2D humanRect = new Rectangle2D.Float(200, 300, 200, 100);
        signing.setImageFile(new File(JPEG_PATH));

        signing.setExternalSigning(externallySign);
        destFile = new File(OUT_DIR + getOutputFileName("Signed.pdf", externallySign));
        signing.signPDF(new File(inPath), destFile, humanRect, tsa);

        checkSignature(new File(inPath), destFile, false);
    }

当尝试设置MDPPermission进程时:

if (doc.getVersion() >= 1.5f && accessPermissions == 0)
            {
                SigUtils.setMDPPermission(doc, signature, 2);
            }



 if (sig.getCOSObject().containsKey(COSName.CONTENTS))
            {
                throw new IOException("DocMDP transform method not allowed if an approval signature exists");
            }

但是在这个函数上 SigUtils 出错了:

java.io.IOException: DocMDP 转换方法不允许,如果 批准签名存在

我怎样才能通过文件签名?

【问题讨论】:

    标签: pdfbox


    【解决方案1】:

    错误信息“如果存在批准签名,则不允许使用DocMDP转换方法”是正确的。

    如果您想为已签名的 PDF 添加签名,只需删除

    if (doc.getVersion() >= 1.5f && accessPermissions == 0)
    {
        SigUtils.setMDPPermission(doc, signature, 2);
    }
    

    阻止。

    【讨论】:

    • 对不起,签名现在成功了,但是得到了错误 org.opentest4j.AssertionFailedError: array lengths different, expected: but was: in function checksignature(new File(inPath) , 目标文件, 假);为什么会这样?
    • @Lazar “为什么会发生这种情况” - 嗯,很难说,你在示例中是否更改了其他内容?
    • 没什么变化,我今天克隆了 github
    • 特别是checksignature中有多个数组相等的断言...你能确定哪一个失败并分享签名的PDF吗?
    • CheckSignature 是我们构建测试的一部分,您应该找出不同的数组以及这是否与您的应用程序相关。
    猜你喜欢
    • 2015-08-04
    • 1970-01-01
    • 2019-12-17
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多