0

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.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • 1
    Does [this answer](https://stackoverflow.com/a/52834362/1729265) help? In short, there theoretically are multiple ways to do (and which are actually used by some software). Most of them are explicitly forbidden, at least in the current PDF specification. One option is not explicitly forbidden and that answer contains a proof-of-concept for using it. Be aware, though, that even though it is not explicitly forbidden, the intention of disallowing the other options was to disallow signatures with multiple appearances in general. So any signature with multiple appearances may be considered suspect. – mkl Nov 04 '21 at 09:09
  • Thanks,this answer is very useful for me,but I have a little doubt,i updated my question. – Serendipity Dec 14 '21 at 02:49
  • Please share an example pdf signed with your code for analysis. – mkl Dec 14 '21 at 06:10
  • Adobe Acrobat here appears to have a bug, there is no missing dictionary object to expect. (Strictly speaking your signatures are invalid because the same annotation is referenced from different pages, but Adobe Acrobat usually ignores this). Acrobat sees the appearance XObject just reference and draw one other XObject, and I assume it takes this as a sign that the appearance is constructed like the old Adobe style appearances with changing appearances based on the validation state. Deeper in, though, your appearance structure differs, so maybe here Adobe Acrobat misses a dictionary... – mkl Dec 14 '21 at 18:18
  • 1
    If I find some time later this week or next week, I'll test a bit. Currently the log4j issue takes up most of the time... – mkl Dec 14 '21 at 18:20
  • Thanks, is there any other way to add custom picture stamp data (wait for you to solve the problem before answering) – Serendipity Dec 20 '21 at 06:35
  • Theoretically you could add images inside arbitrary annotations if the certification levels allows. I assume, though, that it suffices to deviate far enough from the Adobe pattern of creating their special signature visualizations. – mkl Dec 20 '21 at 08:31

1 Answers1

1

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, i.e. 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.

mkl
  • 90,588
  • 15
  • 125
  • 265