Class PdfSplitter

java.lang.Object
cloud.opencode.base.pdf.operation.PdfSplitter

public final class PdfSplitter extends Object
PDF Splitter PDF 拆分器

Splits a PDF document into multiple documents.

将 PDF 文档拆分为多个文档。

Features | 主要功能:

  • Split into single pages - 拆分为单页
  • Split by page ranges - 按页面范围拆分
  • Split by page count - 按页数拆分
  • Split by file size - 按文件大小拆分
  • Split by bookmarks - 按书签拆分

Usage Examples | 使用示例:

// Split to single pages
List<PdfDocument> pages = PdfSplitter.of(Path.of("document.pdf"))
    .splitToPages();

// Split by ranges
List<PdfDocument> parts = PdfSplitter.create()
    .source(Path.of("document.pdf"))
    .splitByRanges("1-5", "6-10", "11-15");

// Split and save
PdfSplitter.of(Path.of("document.pdf"))
    .splitAndSave(Path.of("output"), "part_%d.pdf");

Security | 安全性:

  • Thread-safe: No — not designed for concurrent use - 线程安全: 否 — 非并发设计
  • Null-safe: Yes — parameters are validated - 空值安全: 是 — 参数已验证
Since:
JDK 25, opencode-base-pdf V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • source

      public PdfSplitter source(Path path)
      Sets source PDF file. 设置源 PDF 文件。
      Parameters:
      path - PDF file path | PDF 文件路径
      Returns:
      this splitter | 当前拆分器
    • source

      public PdfSplitter source(PdfDocument document)
      Sets source PDF document. 设置源 PDF 文档。
      Parameters:
      document - PDF document | PDF 文档
      Returns:
      this splitter | 当前拆分器
    • splitToPages

      public List<PdfDocument> splitToPages()
      Splits into single pages. 拆分为单页。
      Returns:
      list of single-page documents | 单页文档列表
      Throws:
      OpenPdfException - if splitting fails | 拆分失败时抛出异常
    • splitByRanges

      public List<PdfDocument> splitByRanges(String... ranges)
      Splits by page ranges. 按页面范围拆分。
      Parameters:
      ranges - page ranges (e.g., "1-3", "5", "7-10") | 页面范围
      Returns:
      list of documents | 文档列表
      Throws:
      OpenPdfException - if splitting fails | 拆分失败时抛出异常
    • splitByPageCount

      public List<PdfDocument> splitByPageCount(int pagesPerDocument)
      Splits by fixed page count per document. 按固定页数拆分。
      Parameters:
      pagesPerDocument - pages per split document | 每个文档的页数
      Returns:
      list of documents | 文档列表
      Throws:
      OpenPdfException - if splitting fails | 拆分失败时抛出异常
    • splitBySize

      public List<PdfDocument> splitBySize(long maxSizeBytes)
      Splits by file size limit. 按文件大小限制拆分。
      Parameters:
      maxSizeBytes - max size per document in bytes | 每个文档最大字节数
      Returns:
      list of documents | 文档列表
      Throws:
      OpenPdfException - if splitting fails | 拆分失败时抛出异常
    • splitByBookmarks

      public List<PdfDocument> splitByBookmarks(int level)
      Splits by bookmarks/outlines. 按书签/大纲拆分。
      Parameters:
      level - bookmark level (1 = top level) | 书签层级
      Returns:
      list of documents | 文档列表
      Throws:
      OpenPdfException - if splitting fails | 拆分失败时抛出异常
    • extractPages

      public PdfDocument extractPages(int... pageNumbers)
      Extracts specific pages. 提取指定页面。
      Parameters:
      pageNumbers - page numbers (1-based) | 页码(从1开始)
      Returns:
      document with extracted pages | 包含提取页面的文档
      Throws:
      OpenPdfException - if extraction fails | 提取失败时抛出异常
    • splitAndSave

      public List<Path> splitAndSave(Path directory, String nameFormat)
      Splits and saves to directory with naming. 拆分并使用命名保存到目录。
      Parameters:
      directory - target directory | 目标目录
      nameFormat - file name format (e.g., "doc_%d.pdf") | 文件名格式
      Returns:
      list of saved file paths | 保存的文件路径列表
      Throws:
      OpenPdfException - if splitting fails | 拆分失败时抛出异常
    • splitAndSave

      public List<Path> splitAndSave(Path directory, Function<Integer,String> nameFunction)
      Splits and saves with custom naming function. 拆分并使用自定义命名函数保存。
      Parameters:
      directory - target directory | 目标目录
      nameFunction - function to generate file name from index | 从索引生成文件名的函数
      Returns:
      list of saved file paths | 保存的文件路径列表
      Throws:
      OpenPdfException - if splitting fails | 拆分失败时抛出异常
    • getSourcePath

      public Path getSourcePath()
    • getSourceDocument

      public PdfDocument getSourceDocument()
    • create

      public static PdfSplitter create()
      Creates a new splitter. 创建新的拆分器。
      Returns:
      PDF splitter | PDF 拆分器
    • of

      public static PdfSplitter of(Path path)
      Creates splitter for file. 为文件创建拆分器。
      Parameters:
      path - PDF file path | PDF 文件路径
      Returns:
      PDF splitter | PDF 拆分器
    • of

      public static PdfSplitter of(PdfDocument document)
      Creates splitter for document. 为文档创建拆分器。
      Parameters:
      document - PDF document | PDF 文档
      Returns:
      PDF splitter | PDF 拆分器