Wie füge ich ein Dokument ordnungsgemäß mehrere Signaturen hinzu und unterschreibe es extern in einer UnterschriftensitzJava

Java-Forum
Anonymous
 Wie füge ich ein Dokument ordnungsgemäß mehrere Signaturen hinzu und unterschreibe es extern in einer Unterschriftensitz

Post by Anonymous »

Ich habe die Anforderung, in einer Unterzeichnung mehrere Signaturen zu einem Dokument für einen Unterzeichner hinzuzufügen. Following one of the suggestions from @mkl, I tried to apply his test code [testCreateSignatureWithMultipleVisualizations] (https://github.com/mkl-public/testarea- ... tions.java) to my case Ersetzen Sie die Schleife durch Dokumentseiten durch meine Liste der Signaturmetadaten ( SignaturedDinitions < /em>).PDAcroForm acroForm = this.pdDocument.getDocumentCatalog().getAcroForm();
if (acroForm == null) {
this.pdDocument.getDocumentCatalog().setAcroForm(acroForm = new PDAcroForm(pdDocument));
}
acroForm.setSignaturesExist(true);
acroForm.setAppendOnly(true);
acroForm.getCOSObject().setDirect(true);
PDSignature pdSignature = new PDSignature();
pdSignature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
pdSignature.setSubFilter(PDSignature.SUBFILTER_ETSI_CADES_DETACHED);
Calendar signDateCalendar = Calendar.getInstance();
signDateCalendar.setTime(Date.from(signingTimestamp.toInstant()));
pdSignature.setSignDate(signDateCalendar);
pdSignature.setName(userData.getSignatureName());
pdSignature.setReason(userData.getSignatureReason());
pdSignature.setLocation(userData.getSignatureLocation());
pdSignature.setContactInfo(userData.getSignatureContactInfo());
for (SignatureDefinition signatureDefinition : this.signatureDefinitions) {
Rectangle2D humanRect =
new Rectangle2D.Float(
signatureDefinition.getX(),
signatureDefinition.getY(),
signatureDefinition.getWidth(),
signatureDefinition.getHeight());
PDRectangle rect =
SigningPdfDocumentHelper.createSignatureRectangle(
this.pdDocument, humanRect, signatureDefinition.getPage());

PDPage pdPage = this.pdDocument.getPage(signatureDefinition.getPage());
addSignatureField(this.pdDocument, pdPage, rect, pdSignature);
}
this.pdDocument.addSignature(pdSignature);
this.pbSigningSupport = this.pdDocument.saveIncrementalForExternalSigning(this.inMemoryStream);
MessageDigest digest = MessageDigest.getInstance(digestAlgorithm.getDigestAlgorithm());
byte[] contentToSign = IOUtils.toByteArray(this.pbSigningSupport.getContent());

byte[] hashToSign = digest.digest(contentToSign);
this.base64HashToSign = Base64.getEncoder().encodeToString(hashToSign);
}
< /code>
private void addSignatureField(
PDDocument pdDocument, PDPage pdPage, PDRectangle rectangle, PDSignature signature)
throws IOException {
PDAcroForm acroForm = pdDocument.getDocumentCatalog().getAcroForm();
List
acroFormFields = acroForm.getFields();

PDSignatureField signatureField = new PDSignatureField(acroForm);
signatureField.setValue(signature);
PDAnnotationWidget widget = signatureField.getWidgets().get(0);
acroFormFields.add(signatureField);

widget.setRectangle(rectangle);
widget.setPage(pdPage);

// from PDVisualSigBuilder.createHolderForm()
PDStream stream = new PDStream(pdDocument);
PDFormXObject form = new PDFormXObject(stream);
PDResources res = new PDResources();
form.setResources(res);
form.setFormType(1);
PDRectangle bbox = new PDRectangle(rectangle.getWidth(), rectangle.getHeight());
float height = bbox.getHeight();

form.setBBox(bbox);
PDFont font = PDType1Font.HELVETICA_BOLD;

// from PDVisualSigBuilder.createAppearanceDictionary()
PDAppearanceDictionary appearance = new PDAppearanceDictionary();
appearance.getCOSObject().setDirect(true);
PDAppearanceStream appearanceStream = new PDAppearanceStream(form.getCOSObject());
appearance.setNormalAppearance(appearanceStream);
widget.setAppearance(appearance);

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();
}

pdPage.getAnnotations().add(widget);

COSDictionary pageTreeObject = pdPage.getCOSObject();
while (pageTreeObject != null) {
pageTreeObject.setNeedToBeUpdated(true);
pageTreeObject = (COSDictionary)
pageTreeObject.getDictionaryObject(COSName.PARENT);
}
}
< /code>
Since I did not set the preferred signature size, I got the IOException: "Can't write signature, not enough space".
In the second try, I made a change on method this.pdDocument.addSignature(pdSignature) to pass SignatureOptions parameter to have a chance setting preferred signature size like below:
SignatureOptions options = new SignatureOptions();
options.setPreferredSignatureSize(30000);
this.pdDocument.addSignature(pdSignature, options);
< /code>
This time I could sign successfully but only the last signture was displayed, the others remained invisible.
signed document here
I would be very grateful if anybody can point the missing part in my implementation to get it completed as all signtures will be visible and valid.
Thank you so much for your time reading this topic of mine!
Updated: Attach example PDF document signed by my code
https://github.com/tai-nguyen-mesoneer/ ... 0(003).pdf
I am using PDFBox 2.0.24.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post