LibrePDF / OpenPDF

OpenPDF is a free Java library for creating and editing PDF files, with a LGPL and MPL open source license. OpenPDF is based on a fork of iText. We welcome contributions from other developers. Please feel free to submit pull-requests and bugreports to this GitHub repository.
Other
3.47k stars 578 forks source link

Run test method: visibleExternalSignature to generate a pdf file with digital-signature #787

Open bobo325 opened 2 years ago

bobo325 commented 2 years ago

I try to use openPdf to generate a pdf file with digital-signature with test method: visibleExternalSignature, It did generate a pdf file, but when I open it with Adobe Acrobat Reader DC, It reported an error: At lease one signature is invalid. And when I extend the details, It appeared the follow errors:

图片

What should I do now ?

here is the codes(mostly like the official demo just replace some urls with myselves'): ` @Test void visibleExternalSignature() throws DocumentException { byte[] expectedDigestPreClose = null; byte[] expectedDigestClose = null;

    // These fields are provided to be able to generate the same content more than
    // once
    Calendar signDate = Calendar.getInstance();
    PdfObject overrideFileId = new PdfLiteral("<123><123>".getBytes());

    for (int i = 0; i < 10; i++) {
        try {
            File file = new File("xxxxxxx\\test.pdf");
            System.out.println(file.exists());
            InputStream is = new FileInputStream(file);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfReader reader = new PdfReader(is);
            PdfStamper stp = PdfStamper.createSignature(reader, baos, '\0',
                    null, true);
            stp.setEnforcedModificationDate(signDate);
            stp.setOverrideFileId(overrideFileId);

            PdfSignatureAppearance sap = stp.getSignatureAppearance();

            PdfDictionary dic = new PdfDictionary();
            dic.put(PdfName.FILTER, PdfName.ADOBE_PPKLITE);
            dic.put(PdfName.M, new PdfDate(signDate));

            sap.setCryptoDictionary(dic);
            sap.setSignDate(signDate);
            sap.setVisibleSignature(new Rectangle(100, 100), 1);
            sap.setLayer2Text("Hello world");
            sap.setLayer4Text("BOBO");
            sap.setImage(Image.getInstance("xxxxxx\\bobo.png"));

            Map<PdfName, Integer> exc = new HashMap<>();
            exc.put(PdfName.CONTENTS, 10);
            sap.preClose(exc);

            byte[] result = Utilities.toByteArray(sap.getRangeStream());
            byte[] sha256 = getSHA256(result);
            if (expectedDigestPreClose == null) {
                expectedDigestPreClose = sha256;
            } else {
                assertArrayEquals(expectedDigestPreClose, sha256);
            }

            PdfDictionary update = new PdfDictionary();
            update.put(PdfName.CONTENTS, new PdfString("aaaa").setHexWriting(true));
            sap.close(update);

            byte[] resultClose = Utilities.toByteArray(sap.getRangeStream());
            byte[] sha256Close = getSHA256(resultClose);
            if (expectedDigestClose == null) {
                expectedDigestClose = sha256Close;
            } else {
                assertArrayEquals(expectedDigestClose, sha256Close);
            }

            OutputStream outputStream = new FileOutputStream("xxxxxx\\sign.pdf");
            outputStream.write(baos.toByteArray());
            outputStream.flush();
            outputStream.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}`
bobo325 commented 2 years ago

Compared to other solutions, I found that there is no private key, certs and so on... progress...