Class OpenPdf

java.lang.Object
cloud.opencode.base.pdf.OpenPdf

public final class OpenPdf extends Object
PDF Utility Entry Class PDF 工具入口类

Provides factory methods for all PDF operations.

提供所有 PDF 操作的工厂方法。

Features | 主要功能:

  • Document creation with fluent API - 使用流畅 API 创建文档
  • Document reading and parsing - 文档读取和解析
  • PDF merging and splitting - PDF 合并和拆分
  • Form filling and extraction - 表单填充和提取
  • Digital signatures - 数字签名
  • Content extraction - 内容提取

Usage Examples | 使用示例:

// Create a new PDF
OpenPdf.create()
    .title("My Document")
    .author("John Doe")
    .addPage()
        .text("Hello, World!", 100, 700)
    .endPage()
    .save(Path.of("hello.pdf"));

// Open an existing PDF
try (PdfDocument doc = OpenPdf.open(Path.of("document.pdf"))) {
    System.out.println("Pages: " + doc.getPageCount());
}

// Merge PDFs
OpenPdf.merge(
    List.of(Path.of("doc1.pdf"), Path.of("doc2.pdf")),
    Path.of("merged.pdf")
);

// Extract text
String text = OpenPdf.extractText(Path.of("document.pdf"));

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-pdf V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static DocumentBuilder create()
      Creates a new PDF document builder. 创建新的 PDF 文档构建器。
      Returns:
      document builder | 文档构建器
    • create

      public static DocumentBuilder create(PageSize pageSize)
      Creates a new PDF document builder with specified page size. 创建指定页面大小的 PDF 文档构建器。
      Parameters:
      pageSize - page size | 页面大小
      Returns:
      document builder | 文档构建器
    • open

      public static PdfDocument open(Path path)
      Opens an existing PDF document from file path. 从文件路径打开已有 PDF 文档。
      Parameters:
      path - file path | 文件路径
      Returns:
      PDF document | PDF 文档
      Throws:
      OpenPdfException - if reading fails | 读取失败时抛出异常
    • open

      public static PdfDocument open(InputStream inputStream)
      Opens an existing PDF document from input stream. 从输入流打开已有 PDF 文档。
      Parameters:
      inputStream - input stream | 输入流
      Returns:
      PDF document | PDF 文档
      Throws:
      OpenPdfException - if reading fails | 读取失败时抛出异常
    • open

      public static PdfDocument open(byte[] bytes)
      Opens an existing PDF document from byte array. 从字节数组打开已有 PDF 文档。
      Parameters:
      bytes - PDF bytes | PDF 字节数组
      Returns:
      PDF document | PDF 文档
      Throws:
      OpenPdfException - if reading fails | 读取失败时抛出异常
    • open

      public static PdfDocument open(Path path, String password)
      Opens a password-protected PDF document. 打开受密码保护的 PDF 文档。
      Parameters:
      path - file path | 文件路径
      password - document password | 文档密码
      Returns:
      PDF document | PDF 文档
      Throws:
      OpenPdfException - if reading fails or password incorrect | 读取失败或密码错误时抛出异常
    • merger

      public static PdfMerger merger()
      Creates a PDF merger. 创建 PDF 合并器。
      Returns:
      PDF merger | PDF 合并器
    • merge

      public static void merge(List<Path> sources, Path target)
      Merges multiple PDF files into one. 将多个 PDF 文件合并为一个。
      Parameters:
      sources - source files | 源文件列表
      target - target file | 目标文件
      Throws:
      OpenPdfException - if merging fails | 合并失败时抛出异常
    • merge

      public static PdfDocument merge(List<PdfDocument> documents)
      Merges multiple PDF documents into one. 将多个 PDF 文档合并为一个。
      Parameters:
      documents - source documents | 源文档列表
      Returns:
      merged document | 合并后的文档
      Throws:
      OpenPdfException - if merging fails | 合并失败时抛出异常
    • splitter

      public static PdfSplitter splitter()
      Creates a PDF splitter. 创建 PDF 拆分器。
      Returns:
      PDF splitter | PDF 拆分器
    • split

      public static List<PdfDocument> split(Path source, String... ranges)
      Splits a PDF by page ranges. 按页面范围拆分 PDF。
      Parameters:
      source - source file | 源文件
      ranges - page ranges (e.g., "1-3", "5", "7-10") | 页面范围
      Returns:
      split documents | 拆分后的文档列表
      Throws:
      OpenPdfException - if splitting fails | 拆分失败时抛出异常
    • splitToPages

      public static List<PdfDocument> splitToPages(Path source)
      Splits a PDF into single pages. 将 PDF 拆分为单页文档。
      Parameters:
      source - source file | 源文件
      Returns:
      single page documents | 单页文档列表
      Throws:
      OpenPdfException - if splitting fails | 拆分失败时抛出异常
    • fillForm

      public static PdfDocument fillForm(Path source, Map<String,String> fields)
      Fills form fields in a PDF. 填充 PDF 表单字段。
      Parameters:
      source - source PDF with form | 带表单的源 PDF
      fields - field name to value mapping | 字段名到值的映射
      Returns:
      PDF with filled form | 填充后的 PDF
      Throws:
      OpenPdfException - if filling fails | 填充失败时抛出异常
    • fillAndFlatten

      public static PdfDocument fillAndFlatten(Path source, Map<String,String> fields)
      Fills form and flattens (makes non-editable). 填充表单并扁平化(使其不可编辑)。
      Parameters:
      source - source PDF with form | 带表单的源 PDF
      fields - field name to value mapping | 字段名到值的映射
      Returns:
      flattened PDF | 扁平化后的 PDF
      Throws:
      OpenPdfException - if operation fails | 操作失败时抛出异常
    • extractFormFields

      public static Map<String,String> extractFormFields(Path source)
      Extracts form fields from a PDF. 从 PDF 提取表单字段。
      Parameters:
      source - source PDF | 源 PDF
      Returns:
      form field names and current values | 表单字段名和当前值
      Throws:
      OpenPdfException - if extraction fails | 提取失败时抛出异常
    • signer

      public static PdfSigner signer()
      Creates a PDF signer. 创建 PDF 签名器。
      Returns:
      PDF signer | PDF 签名器
    • sign

      public static PdfDocument sign(Path source, Path keyStore, char[] password, String alias)
      Signs a PDF document. 对 PDF 文档进行签名。
      Parameters:
      source - source PDF | 源 PDF
      keyStore - key store path | 密钥库路径
      password - key store password | 密钥库密码
      alias - certificate alias | 证书别名
      Returns:
      signed PDF | 签名后的 PDF
      Throws:
      OpenPdfException - if signing fails | 签名失败时抛出异常
    • verifySignatures

      public static List<SignatureInfo> verifySignatures(Path source)
      Verifies PDF signatures. 验证 PDF 签名。
      Parameters:
      source - signed PDF | 签名的 PDF
      Returns:
      signature validation results | 签名验证结果
      Throws:
      OpenPdfException - if verification fails | 验证失败时抛出异常
    • extractor

      public static PdfExtractor extractor()
      Creates a PDF extractor. 创建 PDF 提取器。
      Returns:
      PDF extractor | PDF 提取器
    • extractText

      public static String extractText(Path source)
      Extracts all text from a PDF. 从 PDF 提取所有文本。
      Parameters:
      source - source PDF | 源 PDF
      Returns:
      extracted text | 提取的文本
      Throws:
      OpenPdfException - if extraction fails | 提取失败时抛出异常
    • extractText

      public static String extractText(Path source, int... pageNumbers)
      Extracts text from specific pages. 从指定页面提取文本。
      Parameters:
      source - source PDF | 源 PDF
      pageNumbers - page numbers (1-based) | 页码(从1开始)
      Returns:
      extracted text | 提取的文本
      Throws:
      OpenPdfException - if extraction fails | 提取失败时抛出异常
    • extractImages

      public static List<byte[]> extractImages(Path source)
      Extracts images from a PDF. 从 PDF 提取图像。
      Parameters:
      source - source PDF | 源 PDF
      Returns:
      extracted images as byte arrays | 提取的图像字节数组
      Throws:
      OpenPdfException - if extraction fails | 提取失败时抛出异常
    • getPageCount

      public static int getPageCount(Path source)
      Gets the page count of a PDF. 获取 PDF 页数。
      Parameters:
      source - source PDF | 源 PDF
      Returns:
      page count | 页数
      Throws:
      OpenPdfException - if reading fails | 读取失败时抛出异常
    • getMetadata

      public static Metadata getMetadata(Path source)
      Gets PDF metadata. 获取 PDF 元数据。
      Parameters:
      source - source PDF | 源 PDF
      Returns:
      document metadata | 文档元数据
      Throws:
      OpenPdfException - if reading fails | 读取失败时抛出异常
    • isEncrypted

      public static boolean isEncrypted(Path source)
      Checks if a PDF is encrypted. 检查 PDF 是否加密。
      Parameters:
      source - source PDF | 源 PDF
      Returns:
      true if encrypted | 如果加密返回 true
      Throws:
      OpenPdfException - if reading fails | 读取失败时抛出异常
    • hasForm

      public static boolean hasForm(Path source)
      Checks if a PDF contains forms. 检查 PDF 是否包含表单。
      Parameters:
      source - source PDF | 源 PDF
      Returns:
      true if contains forms | 如果包含表单返回 true
      Throws:
      OpenPdfException - if reading fails | 读取失败时抛出异常
    • isSigned

      public static boolean isSigned(Path source)
      Checks if a PDF is signed. 检查 PDF 是否已签名。
      Parameters:
      source - source PDF | 源 PDF
      Returns:
      true if signed | 如果已签名返回 true
      Throws:
      OpenPdfException - if reading fails | 读取失败时抛出异常