When working with PDFs, especially in business or legal scenarios, adding multiple signatures is often essential. Using Apache PDFBox, a popular Java library, makes signing PDFs manageable—but incorporating multiple signatures into one document brings unique challenges. I recently explored a similar scenario and stumbled upon some helpful test code shared by mkl on Stack Overflow. Here, I’ll share my journey and what I learned along the way.
My First Attempt to Add Multiple Signatures in PDFBox
Starting out, I tried writing the following simplified Java code. It illustrates my initial approach:
// Load the PDF document
PDDocument doc = PDDocument.load(new File("sample.pdf"));
// Initialize the first signature
CreateVisibleSignature signing = new CreateVisibleSignature(keyStore, password.toCharArray());
signing.setVisibleSignDesigner("signature-image.png", 0, 0, -50, document);
signing.setVisibleSignatureProperties("Signature1", 1, 100, 100, 150, 50);
signing.signPDF(new File("sample_signed_once.pdf"));
// Add second signature
CreateVisibleSignature signing2 = new CreateVisibleSignature(keyStore, password.toCharArray());
signing2.setVisibleSignatureProperties("Signature2", 1, 100, 200, 150, 50);
signing2.signPDF(new File("sample_signed_once.pdf"), new File("sample_signed_twice.pdf"));
Here’s what’s happening step-by-step:
- First, I load my PDF document (sample.pdf).
- I create a visible signature (“Signature1”) and place it on the first page at specified coordinates.
- Then, output a new PDF file (“sample_signed_once.pdf”).
- Next, load the signed PDF and add another visible signature (“Signature2”), aiming to output a second signed file (“sample_signed_twice.pdf”).
This seemed logical enough and aligned with typical programming workflows. However, when running this code, an error appeared related to insufficient reserved space in the PDF for multiple signatures. After some digging on Stack Overflow, I pinpointed the issue: I hadn’t considered setting enough “preferred signature size.”
Adjusting Preferred Signature Size to Fix the Error
To correctly add multiple signatures, each signature needs sufficient reserved space in the document. Luckily, PDFBox provides a SignatureOptions class that allows setting the preferred signature size to prevent space-related errors.
My revised Java code now looked like this:
// Load document to sign
PDDocument document = PDDocument.load(new File("sample.pdf"));
// Initialize SignatureOptions with preferred size
SignatureOptions options = new SignatureOptions();
options.setPreferredSignatureSize(SignatureOptions.DEFAULT_SIGNATURE_SIZE * 2); // increased size
CreateVisibleSignature signing = new CreateVisibleSignature(keyStore, password.toCharArray());
signing.setVisibleSignDesigner("signature-image.png", 0, 0, -50, document);
signing.setVisibleSignatureProperties("Signature1", 1, 100, 100, 150, 50);
signing.setSignatureOptions(options);
signing.signPDF(new File("sample_signed_once.pdf"));
// Repeat for second signature with new options
SignatureOptions options2 = new SignatureOptions();
options2.setPreferredSignatureSize(SignatureOptions.DEFAULT_SIGNATURE_SIZE * 2);
CreateVisibleSignature signing2 = new CreateVisibleSignature(keyStore, password.toCharArray());
signing2.setVisibleSignatureProperties("Signature2", 1, 100, 200, 150, 50);
signing2.setSignatureOptions(options2);
signing2.signPDF(new File("sample_signed_once.pdf"), new File("sample_signed_twice.pdf"));
This approach ensures that enough space is allocated within the PDF file to accommodate both signatures. Now, my PDF signing process ran without any space-related errors.
Displaying Multiple Signatures — New Challenge Arises
After successfully adding multiple signatures and checking the PDF document, another puzzling issue emerged: although both signatures were technically added, only the last digital signature was clearly visible on the document. This meant if someone viewed the PDF, they’d only see the second signature displayed, and the previous one seemed to vanish visually.
To clarify the problem, I uploaded a sample signed PDF illustrating the problem. You can see this behavior clearly in this signed document example. Notice that only one signature visibly appears, even though multiple signatures are embedded.
Seeking Community & Expert Guidance for Display Issues
After exhausting common troubleshooting steps and reviewing documentation, I realized some subtle yet crucial implementation detail was probably missing. I’m currently using PDFBox version 2.0.24. Given my current setup, I suspect the issue might be related to either:
- Improper updating or incremental signing method
- Mistakes in configuration such as overlapping fields or incorrect annotation settings
- Specific compatibility issues between my PDF viewer software and PDFBox’s signing method
Have you encountered similar issues when working with visible signatures in PDFBox? Could you suggest anything about the visibility of multiple signatures—perhaps I’ve overlooked certain crucial parameters or options? I’d appreciate any insights you might have into fixing this visibility issue. I’m genuinely curious to know if you’ve faced and solved this scenario before.
For transparency, here’s an example test code I reference often, provided by expert PDF contributor mkl. This may offer some hints as you review or try solving the problem.
In my efforts to make this clearer, I’ve already uploaded the problematic signed PDF document: you can find it linked above. Feel free to download, inspect, and let me know if you spot the problem.
Also, for more practical examples and common solutions related to document handling, check the detailed JavaScript guides in our JavaScript articles section. Occasionally, combining Java with frontend scripting like JavaScript can offer creative ways of solving compound PDF display and signature problems.
Additional Resources for Your Reference
Here are quick links for convenience:
- Test signature code from GitHub
- Example PDF document signed twice demonstrating the mentioned issue
- Apache PDFBox Official Documentation
Thanks so much for your time—any help or suggestions would truly improve the PDF signing experience for me and others who might face this challenge. Maybe together, we can crack this puzzle and create clearer instructions for everyone using PDFBox. Have you experienced similar challenges with PDF signatures? How did you resolve them? Share your thoughts or suggestions, I’d be glad to hear them!
0 Comments