1 回答

TA貢獻2011條經驗 獲得超2個贊
根據問題評論中的說明,您嘗試將簽名放置在最后一個文檔頁面現有內容的邊界框下方。
但正如您在對此評論的回應中發現的那樣,您不能簡單地使用其結果作為輸入,因為CreateVisibleSignature.setVisibleSignDesigner
假定了不同的坐標系:
使用相關頁面的 PDF 默認用戶空間坐標:它們由相關頁面的MediaBox
BoundingBoxFinder
給出,并且y坐標向上增加。通常原點位于頁面的左下角。CreateVisibleSignature
另一方面,使用單位長度相同但原點位于頁面左上角且 y坐標向下增加的坐標系。
因此,必須轉換坐標,例如:
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);
評論
將上面的代碼應用到該文件,人們會發現最后可見的文本行和圖像之間有一個小間隙。此間隙是由“請訪問我們的網站”行下方的一行中的一些空格字符引起的。它BoundingBoxFinder
不會檢查繪圖指令最終是否會產生可見的結果,它總是將有問題的區域添加到邊界框。
一般來說,您可能需要從上面代碼計算出的y坐標中減去一點點,以在以前的頁面內容和新的簽名小部件之間創建視覺間隙。
查看源代碼CreateVisibleSignature
會發現,實際上y坐標是通過從MediaBox 的高度中減去它們來轉換的,而不是從其上邊框值中減去它們。最終這些坐標被復制到目標文檔中。因此,可能需要在上面的代碼中使用而不是。mediaBox.getHeight()
mediaBox.getUpperRightY()
查看源代碼后CreateVisibleSignature2
發現,實際上使用了CropBox而不是MediaBox。如果您的代碼源自該示例,您可能必須在上面的代碼中替換pdPage.getMediaBox()
為。pdPage.getCropBox()
一般來說,任意使用不同的坐標系是使用 PDFBox 時相當少的令人煩惱的來源之一。
添加回答
舉報