如何在Itext PDF Java中确定签名的位置?

Yas*_*ing 2 java pdf sign pki itext

我正在做一个使用Itext Library对PDF文档进行数字签名的程序.

当我将其设置为可见签名时,问题是签名的位置

如何确定签名的位置?

这是如何创建签名的矩形:

new Rectangle(float llx, float lly, float urx, float ury); 
Run Code Online (Sandbox Code Playgroud)

当我玩这个时,我发现它与签名的大小有关,而不是它的位置.?

任何帮助?

提前致谢 .

mkl*_*mkl 9

根据您对原始问题的评论,您想知道

如何告诉程序将签名放在页面的顶部,左侧,右侧等.

我假设您使用这样的例程:

public void sign(String src, String dest, 
    Certificate[] chain, PrivateKey pk, String digestAlgorithm, String provider, 
    CryptoStandard subfilter, String reason, String location) 
    throws GeneralSecurityException, IOException, DocumentException { 
    // Creating the reader and the stamper 
    PdfReader reader = new PdfReader(src); 
    FileOutputStream os = new FileOutputStream(dest); 
    PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0'); 
    // Creating the appearance 
    PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); 
    appearance.setReason(reason); 
    appearance.setLocation(location); 
    appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig"); 
    // Creating the signature 
    ExternalDigest digest = new BouncyCastleDigest(); 
    ExternalSignature signature = 
    new PrivateKeySignature(pk, digestAlgorithm, provider); 
    MakeSignature.signDetached(appearance, digest, signature, chain, 
    null, null, null, 0, subfilter); 
} 
Run Code Online (Sandbox Code Playgroud)

(Bruno Lowagie(iText软件)的PDF文档白皮书数字签名中的代码示例2.1 )

并想要调整Rectangle在线

    appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig"); 
Run Code Online (Sandbox Code Playgroud)

满足您的要求.

此矩形中的坐标是相对于您签名的页面的坐标系给出的.您可以使用PdfReader方法检索定义此坐标系的数据getCropBox(int index)(index:页码.第一页是1).

    Rectangle cropBox = reader.getCropBox(1);
Run Code Online (Sandbox Code Playgroud)

此外,您需要知道签名的宽度和高度.例如

    float width = 108;
    float height = 32;
Run Code Online (Sandbox Code Playgroud)

使用这些数据,您可以计算Rectangle rectangle如下:

    // Top left
    rectangle = new Rectangle(cropBox.getLeft(), cropBox.getTop(height),
                              cropBox.getLeft(width), cropBox.getTop());

    // Top right
    rectangle = new Rectangle(cropBox.getRight(width), cropBox.getTop(height),
                              cropBox.getRight(), cropBox.getTop());

    // Bottom left
    rectangle = new Rectangle(cropBox.getLeft(), cropBox.getBottom(),
                              cropBox.getLeft(width), cropBox.getBottom(height));

    // Bottom right
    rectangle = new Rectangle(cropBox.getRight(width), cropBox.getBottom(),
                              cropBox.getRight(), cropBox.getBottom(height));
Run Code Online (Sandbox Code Playgroud)

并用它来定义可见的签名位置和大小:

    appearance.setVisibleSignature(rectangle, 1, "sig");
Run Code Online (Sandbox Code Playgroud)