According to the clarifications in comments to the question, you try to position the signature right underneath the bounding box of existing contents of the last document page.
To determine that bounding box you can use the BoundingBoxFinder
presented in this answer.
But as you found out in response to a comment to that effect, you cannot simply use its result as input for CreateVisibleSignature.setVisibleSignDesigner
as different coordinate systems are assumed:
- The
BoundingBoxFinder
uses the PDF default user space coordinates of the page in question: They are given by the MediaBox of the page in question and have their y coordinates increase upwards. Usually the origin is in the lower left corner of the page.
CreateVisibleSignature
on the other hand uses a coordinate system with the same unit length but having the origin in the upper left corner of the page and the y coordinates increasing downwards.
Thus, the coordinates have to be transformed, e.g. like this:
File documentFile = new File(SOURCE);
File signedDocumentFile = new File(RESULT);
Rectangle2D boundingBox;
PDRectangle mediaBox;
try ( PDDocument document = PDDocument.load(documentFile) ) {
PDPage pdPage = document.getPage(0);
BoundingBoxFinder boundingBoxFinder = new BoundingBoxFinder(pdPage);
boundingBoxFinder.processPage(pdPage);
boundingBox = boundingBoxFinder.getBoundingBox();
mediaBox = pdPage.getMediaBox();
}
CreateVisibleSignature signing = new CreateVisibleSignature(ks, PASSWORD.clone());
try ( InputStream imageStream = IMAGE_STREAM) {
signing.setVisibleSignDesigner(documentFile.getPath(), (int)boundingBox.getX(), (int)(mediaBox.getUpperRightY() - boundingBox.getY()), -50, imageStream, 1);
}
signing.setVisibleSignatureProperties("name", "location", "Security", 0, 1, true);
signing.setExternalSigning(false);
signing.signPDF(documentFile, signedDocumentFile, null);
(CreateSignature test signLikeHemantPdfTest
)
Remarks
I found a document looking like your Yukon Education PDF Test File here. Applying the code above to that file, one observes that there is a small gap between the last visible line of text and the image. This gap is caused by some space characters in a line below the "Please visit our website" line. The BoundingBoxFinder
does not check whether a drawing instruction eventually results in something visible, it always adds the area in question to the bounding box.
In general you may want to subtract a small bit from the y coordinate calculated by the code above to create a visual gap between former page content and the new signature widget.
Looking into the sources of the CreateVisibleSignature
one sees that there actually the y coordinates are transformed by subtracting them from the height of the MediaBox, not from its upper border value. Eventually these coordinates are copied into the target document. Thus, it may be necessary to use mediaBox.getHeight()
instead of mediaBox.getUpperRightY()
in the code above.
Looking into the sources of the CreateVisibleSignature2
one sees that there actually the CropBox is used instead of the MediaBox. If your code derives from that example, you may have to replace pdPage.getMediaBox()
by pdPage.getCropBox()
in the code above.
In general this arbitrary use of different coordinate systems is one of the fairly few sources of irritation when working with PDFBox.
VisibleSignature
and theVisibleSignDesigner
normalize that away? – mklPDVisibleSignDesigner.adjustForRotation()
which was inspired by an answer by you. – Tilman Hausherr