简体   繁体   中英

Java uses pdfbox to add digital signature to pdf

I want to ask a question. If I want to add digital signature to a multi-page pdf, each page has the same seal, can I only add the digital signature once on the first page, and then the other pages only need to quote the appearance of the first seal. Because using this method can reduce the time to add stamps.

I used the code given by mkl, but I have a question. I replaced the following code with other codes.

original:

try (PDPageContentStream cs = new PDPageContentStream(pdDocument, appearanceStream))
{
    // show background (just for debugging, to see the rect size + position)
    cs.setNonStrokingColor(Color.yellow);
    cs.addRect(-5000, -5000, 10000, 10000);
    cs.fill();

    float fontSize = 10;
    float leading = fontSize * 1.5f;
    cs.beginText();
    cs.setFont(font, fontSize);
    cs.setNonStrokingColor(Color.black);
    cs.newLineAtOffset(fontSize, height - leading);
    cs.setLeading(leading);
    cs.showText("Signature text");
    cs.newLine();
    cs.showText("some additional Information");
    cs.newLine();
    cs.showText("let's keep talking");
    cs.endText();
}

now:

PDImageXObject Sign0 = PDImageXObject.createFromByteArray(doc, imageByte, null);

try (PDPageContentStream cs = new PDPageContentStream(pdDocument, appearanceStream))
{
    cs.drawImage(Sign0,0, 0,rectangle.getWidth(),rectangle.getHeight());
}

The original code is valid at the time of stamping, but the modified code will invalidate the stamp. I use Adobe Acrobat Pro DC to open the signed document. This error is "An error occurred during signature verification. Adobe Acrobat error. Expected dictionary object.". I'm not sure what happened.

Your question is based upon the code presented in the "A proof of concept" section of this answer to the question Multiple esign using pdfbox 2.0.12 java . Thus, I tested again adding a new test case testCreateSignatureWithMultipleImageOnlyVisualizations to the old test class CreateMultipleVisualizations and indeed could reproduce the behavior observed by the OP.

But then a small change sufficed to make Adobe Reader happy again, I simply also added a comment to the signature visualization like this:

try (PDPageContentStream cs = new PDPageContentStream(pdDocument, appearanceStream))
{
    cs.addComment("This is a comment");
    cs.drawImage(Sign0,0, 0,rectangle.getWidth(),rectangle.getHeight());
}

and Adobe Reader didn't run into an error with the output PDF anymore!


As already mentioned in the comments, I assume that in case of a signature appearance that only draws another XObject, Adobe Acrobat considers itself to be confronted with a signature appearance as usually constructed Acrobat itself, ie a signature appearance only drawing a form XObject which in turn draws only other form XObjects, the "layers" n0 and n2 or (deprecated) even more, and only these layers eventually contain actual text and graphics.

Your image XObject contains no nested XObjects, so Adobe Acrobat then fails when trying to find those inner layer form XObjects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM