gpt4 book ai didi

java - 可见签名位置错误

转载 作者:行者123 更新时间:2023-12-01 10:36:37 27 4
gpt4 key购买 nike

我在向现有 PDF 放置可见签名时遇到问题。仅某些 PDF 会出现此问题。计算它的代码似乎没问题。我尝试过页面旋转,但没有帮助。正在尝试 java iText 版本 5.5.5。

错误输出的缩略图是here .

PDF 文件为 here .

错误输出的日志,文件source.pdf和target.pdf:

page width = 1683.6
page height = 1205.52
image width = 240.0
image height = 160.0
ll = 1433.6, 10.0
ur = 1673.6, 170.0

正确输出的日志,文件source2.pdf和target2.pdf:

page width = 1190.52
page height = 842.04
image width = 240.0
image height = 160.0
ll = 940.52, 10.0
ur = 1180.52, 170.0

第一个 pdf 文件有什么问题?我可以在java代码中添加一些东西来防止这种情况发生吗?或者是 iText 错误?感谢您的提示。

源代码:

public static void main(String[] args) throws IOException, DocumentException, GeneralSecurityException {

String inputfilepath = "D:/temp/itext/source2.pdf";
String outputfilepath = "D:/temp/itext/target2.pdf";
String imagefilepath = "D:/temp/itext/signature.png";
String ksfilepath = "D:/temp/itext/keystore.ks";
String kspass = "kspass ";
String keyalias = "keyalias";
String keypass = "keypass";

//get input pdf file
PdfReader reader = new PdfReader(inputfilepath);

//get keystore
KeyStore ks = null;
try {
ks = KeyStore.getInstance(KeyStore.getDefaultType());
} catch (KeyStoreException e) {
System.out.println("KeyStoreException exception: \n");
e.printStackTrace();
}
try {
ks.load(new FileInputStream(ksfilepath), kspass.toCharArray());
} catch (CertificateException e) {
System.out.println("Certificate exception: \n");
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException: \n");
e.printStackTrace();
}

//get key and certificate
PrivateKey key = null;
try {
key = (PrivateKey)ks.getKey(keyalias, keypass.toCharArray());
} catch (UnrecoverableKeyException e) {
System.out.println("Bad password for keystore given");
} catch (KeyStoreException e) {
System.out.println("KeyStoreException exception: \n");
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException: \n");
e.printStackTrace();
}
Certificate[] chain = null;
try {
chain = ks.getCertificateChain(keyalias);
} catch (KeyStoreException e) {
System.out.println("KeyStoreException exception: \n");
e.printStackTrace();
}

//set output pdf file
FileOutputStream fout = new FileOutputStream(outputfilepath);

//get iText pdf stamper
PdfStamper stamper = PdfStamper.createSignature(reader, fout, '\0', null, true);

//set appearance of stamp
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason("sign test");
appearance.setLocation("");

//compute coordinates, margin 10 pt, position right down
Rectangle pagesize;
if (reader.getPageRotation(1) == 90 || reader.getPageRotation(1) == 270) {
pagesize = reader.getPageSizeWithRotation(1);
} else {
pagesize = reader.getPageSize(1);
}
Image image = Image.getInstance(imagefilepath);
float llx = pagesize.getWidth() - image.getWidth() - 10;
float lly = 10;
float urx = pagesize.getWidth() - 10;
float ury = image.getHeight() + 10;
Rectangle rect = new Rectangle(llx, lly, urx, ury);
System.out.println("page width = " + pagesize.getWidth());
System.out.println("page height = " + pagesize.getHeight());
System.out.println("image width = " + image.getWidth());
System.out.println("image height = " + image.getHeight());
System.out.println("ll = " + llx + ", " + lly);
System.out.println("ur = " + urx + ", " + ury);

//graphic
appearance.setSignatureGraphic(Image.getInstance(imagefilepath));
appearance.setRenderingMode(PdfSignatureAppearance.RenderingMode.GRAPHIC);
appearance.setVisibleSignature(rect, 1, null);

//signature
ExternalDigest digest = new BouncyCastleDigest();
BouncyCastleProvider provider = new BouncyCastleProvider();
Security.addProvider(provider);
String digestAlgorithm = DigestAlgorithms.SHA256;
CryptoStandard subfilter = null;
ExternalSignature signature = new PrivateKeySignature(key, digestAlgorithm, provider.getName());
MakeSignature.signDetached(appearance, digest, signature, chain, null, null, null, 0, subfilter);

//write pdf and close streams
stamper.close();
reader.close();
fout.close();

System.out.println("File '" + inputfilepath + "' was succesfully signed and saved to '" + outputfilepath + "'");

}

最佳答案

iText 在正确的位置添加签名;即:在您选择的位置。但是,您并没有明智地选择该位置。

请看一下 source2.pdf 的内部结构:

enter image description here

我们看到页面的可见区域是使用 /MediaBox 定义的页词典的条目。页面左下角坐标为x = 0; y = 0页面右上角坐标为 x = 842.04; y = 1190.52 。因为左下坐标是0, 0 ;右上角的坐标对应x = width; y = height .

现在让我们看一下source.pdf:

enter image description here

在此 PDF 中,页面左下角的坐标为 x = 0; y = -1205.52002 (或 x = 0; y = -height )并且页面右上角的坐标为 x = 1683.59998; y = 0 (或x = width; y = 0)。

如果您像这样定义矩形:

float llx = pagesize.getWidth() - image.getWidth() - 10;
float lly = 10;
float urx = pagesize.getWidth() - 10;
float ury = image.getHeight() + 10;

那么你假设左下角始终是 x = 0; y = 0并且右上角始终是 x = width; y = height 。这不一定是真的。

官方文档对此进行了解释,例如:

您需要像这样调整您的代码:

float llx = pagesize.getRight() - image.getWidth() - 10;
float lly = pageSize.getBottom() + 10;
float urx = pagesize.getRight() - 10;
float ury = pageSize.getBottom() + image.getHeight() + 10;

如果您仔细检查 source.pdf 的屏幕截图,您还会看到有一个 /CropBox 。这是一个可选的页面边界(您在 source2.pdf 中看不到 /CropBox)。如果存在,您应该使用该页面边界来定义 llx , lly , urx ,和ury因为/CropBox定义页面的可见区域。 (在 source.pdf 中,/CropBox/MediaBox 是相同的,因此在这种情况下并不重要,但您应该始终首先检查是否存在 /CropBox 条目。)

关于java - 可见签名位置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34679136/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com