Class PdfSigner

java.lang.Object
cloud.opencode.base.pdf.signature.PdfSigner

public final class PdfSigner extends Object
PDF Digital Signer PDF 数字签名器

Signs PDF documents with digital signatures using PKCS#7.

使用 PKCS#7 对 PDF 文档进行数字签名。

Features | 主要功能:

  • Digital signature with certificates - 使用证书进行数字签名
  • Visible signature appearance - 可见的签名外观
  • Timestamp support - 时间戳支持
  • Multiple signature fields - 多签名字段

Usage Examples | 使用示例:

// Sign a PDF
PdfSigner.create()
    .keyStore(Path.of("keystore.p12"), "password".toCharArray(), "PKCS12")
    .alias("mycert")
    .reason("Contract Approval")
    .location("New York")
    .sign(Path.of("document.pdf"), Path.of("signed.pdf"));

Security | 安全性:

  • Thread-safe: No — not designed for concurrent use - 线程安全: 否 — 非并发设计
  • Null-safe: Yes — parameters are validated - 空值安全: 是 — 参数已验证
  • Sensitive data: Key store passwords are cloned defensively - 敏感数据: 密钥库密码进行了防御性克隆
Since:
JDK 25, opencode-base-pdf V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • keyStore

      public PdfSigner keyStore(Path keyStorePath, char[] password, String type)
      Sets the key store for signing. 设置签名用的密钥库。
      Parameters:
      keyStorePath - key store file path | 密钥库文件路径
      password - key store password | 密钥库密码
      type - key store type (e.g., "PKCS12", "JKS") | 密钥库类型
      Returns:
      this signer | 当前签名器
    • keyStore

      public PdfSigner keyStore(InputStream keyStoreStream, char[] password, String type)
      Sets the key store from input stream. 从输入流设置密钥库。
      Parameters:
      keyStoreStream - key store stream | 密钥库流
      password - key store password | 密钥库密码
      type - key store type | 密钥库类型
      Returns:
      this signer | 当前签名器
    • alias

      public PdfSigner alias(String alias)
      Sets the certificate alias to use. 设置要使用的证书别名。
      Parameters:
      alias - certificate alias | 证书别名
      Returns:
      this signer | 当前签名器
    • keyPassword

      public PdfSigner keyPassword(char[] password)
      Sets private key password (if different from keystore). 设置私钥密码(如果与密钥库不同)。
      Parameters:
      password - private key password | 私钥密码
      Returns:
      this signer | 当前签名器
    • appearance

      public PdfSigner appearance(SignatureAppearance appearance)
      Sets signature appearance. 设置签名外观。
      Parameters:
      appearance - signature appearance | 签名外观
      Returns:
      this signer | 当前签名器
    • reason

      public PdfSigner reason(String reason)
      Sets signature reason. 设置签名原因。
      Parameters:
      reason - signing reason | 签名原因
      Returns:
      this signer | 当前签名器
    • location

      public PdfSigner location(String location)
      Sets signature location. 设置签名位置。
      Parameters:
      location - signing location | 签名位置
      Returns:
      this signer | 当前签名器
    • contact

      public PdfSigner contact(String contact)
      Sets signature contact info. 设置签名联系信息。
      Parameters:
      contact - contact information | 联系信息
      Returns:
      this signer | 当前签名器
    • fieldName

      public PdfSigner fieldName(String fieldName)
      Sets signature field name. 设置签名字段名。
      Parameters:
      fieldName - signature field name | 签名字段名
      Returns:
      this signer | 当前签名器
    • rectangle

      public PdfSigner rectangle(int pageNumber, float x, float y, float width, float height)
      Sets signature rectangle position. 设置签名矩形位置。
      Parameters:
      pageNumber - page number | 页码
      x - x coordinate | x 坐标
      y - y coordinate | y 坐标
      width - width | 宽度
      height - height | 高度
      Returns:
      this signer | 当前签名器
    • timestamp

      public PdfSigner timestamp(String tsaUrl)
      Enables timestamp. 启用时间戳。
      Parameters:
      tsaUrl - TSA server URL | TSA 服务器 URL
      Returns:
      this signer | 当前签名器
    • timestamp

      public PdfSigner timestamp(String tsaUrl, String username, String password)
      Enables timestamp with authentication. 启用带认证的时间戳。
      Parameters:
      tsaUrl - TSA server URL | TSA 服务器 URL
      username - TSA username | TSA 用户名
      password - TSA password | TSA 密码
      Returns:
      this signer | 当前签名器
    • sign

      public PdfDocument sign(Path source)
      Signs a PDF document. 对 PDF 文档签名。
      Parameters:
      source - source PDF | 源 PDF
      Returns:
      signed PDF document | 签名后的 PDF 文档
      Throws:
      OpenPdfException - if signing fails | 签名失败时抛出异常
    • sign

      public void sign(Path source, Path target)
      Signs and saves to file. 签名并保存到文件。
      Parameters:
      source - source PDF | 源 PDF
      target - target file | 目标文件
      Throws:
      OpenPdfException - if signing fails | 签名失败时抛出异常
    • sign

      public PdfDocument sign(PdfDocument document)
      Signs a PDF document object. 对 PDF 文档对象签名。
      Parameters:
      document - PDF document | PDF 文档
      Returns:
      signed PDF document | 签名后的 PDF 文档
      Throws:
      OpenPdfException - if signing fails | 签名失败时抛出异常
    • getKeyStorePath

      public Path getKeyStorePath()
    • getKeyStoreType

      public String getKeyStoreType()
    • getAlias

      public String getAlias()
    • getAppearance

      public SignatureAppearance getAppearance()
    • getReason

      public String getReason()
    • getLocation

      public String getLocation()
    • getContact

      public String getContact()
    • getFieldName

      public String getFieldName()
    • getPageNumber

      public int getPageNumber()
    • getX

      public float getX()
    • getY

      public float getY()
    • getWidth

      public float getWidth()
    • getHeight

      public float getHeight()
    • getTsaUrl

      public String getTsaUrl()
    • create

      public static PdfSigner create()
      Creates a new signer. 创建新的签名器。
      Returns:
      PDF signer | PDF 签名器