2

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

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

如何确定签名的位置?

以下是创建签名矩形的方法:

new Rectangle(float llx, float lly, float urx, float ury); 

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

有什么帮助吗?

提前致谢 。

4

2 回答 2

10

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

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

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

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); 
} 

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

并想Rectangle在行中调整

    appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig"); 

以满足您的要求。

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

    Rectangle cropBox = reader.getCropBox(1);

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

    float width = 108;
    float height = 32;

使用这些数据,您可以计算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));

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

    appearance.setVisibleSignature(rectangle, 1, "sig");
于 2013-11-11T09:43:45.483 回答
0

您的要求不是很清楚,但我认为以下代码可能适合您....

public static void main(String[] args) throws IOException {
    String pdfFile = args[0];
    PdfReader reader = new PdfReader(pdfFile);

    AcroFields fields = reader.getAcroFields();

    for(String signame : fields.getBlankSignatureNames()) {
      List<AcroFields.FieldPosition> positions = fields.getFieldPositions(signame);
      Rectangle rect = positions.get(0).position; // In points:
      float left   = rect.getLeft();
      float bTop   = rect.getTop();
      float width  = rect.getWidth();
      float height = rect.getHeight();

      int page = positions.get(0).page;
      Rectangle pageSize = reader.getPageSize(page);
      float pageHeight = pageSize.getTop();
      float top = pageHeight - bTop;

      System.out.print(signame + "::" + page + "::" + left + "::" + top + "::" + width + "::" + height + "\n");
    }
  }
于 2013-11-11T08:24:11.117 回答