可见签名的错误位置

时间:2016-01-08 14:15:00

标签: java pdf itext

我在向现有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 + "'");

}

1 个答案:

答案 0 :(得分:1)

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)。如果它存在,您应该使用该页面边界来定义llxllyurxury,因为/CropBox定义了页面的可见区域。 (在source.pdf中,/CropBox/MediaBox是相同的,因此在这种情况下并不重要,但您应该始终先检查是否存在/CropBox条目。)