Java 可见签名的错误位置

Java 可见签名的错误位置,java,pdf,itext,Java,Pdf,Itext,我在将可见签名放置到现有PDF时遇到问题。只有某些PDF文件才会出现问题。用于计算它的代码似乎很好。对于我尝试过的页面旋转,没有任何帮助。正在尝试java iText版本5.5.5 显示错误输出的缩略图 Pdf文件是 错误输出日志,文件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 = 167

我在将可见签名放置到现有PDF时遇到问题。只有某些PDF文件才会出现问题。用于计算它的代码似乎很好。对于我尝试过的页面旋转,没有任何帮助。正在尝试java iText版本5.5.5

显示错误输出的缩略图

Pdf文件是

错误输出日志,文件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代码中添加一些东西来防止这种情况吗?或者是它的下一个错误? 谢谢你的提示

源代码:

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的引擎盖下查看:

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

现在让我们看一下源代码。pdf:

在此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;
float llx = pagesize.getRight() - image.getWidth() - 10;
float lly = pageSize.getBottom() + 10;
float urx = pagesize.getRight() - 10;
float ury = pageSize.getBottom() + image.getHeight() + 10;
然后假设左下角总是
x=0;y=0
且右上角始终为
x=宽度;y=高度
。这不一定是真的

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

您需要对代码进行如下调整:

float llx = pagesize.getWidth() - image.getWidth() - 10;
float lly = 10;
float urx = pagesize.getWidth() - 10;
float ury = image.getHeight() + 10;
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
条目。)

此外,应该使用裁剪框而不是媒体框;对于示例文档(裁剪框与媒体框重合),这不是问题,但对于其他文档,这是肯定的。Bruno,非常感谢您的快速响应和详细解释。你的代码工作得很好。再次感谢你。