Interface PdfDocument

All Superinterfaces:
AutoCloseable

public interface PdfDocument extends AutoCloseable
PDF Document - Represents a PDF document with pages and content PDF 文档 - 表示包含页面和内容的 PDF 文档

This interface provides access to PDF document structure including pages, metadata, forms, and save operations.

此接口提供对 PDF 文档结构的访问,包括页面、元数据、表单和保存操作。

Features | 主要功能:

  • Page access and manipulation - 页面访问和操作
  • Metadata management - 元数据管理
  • Form access - 表单访问
  • Document saving - 文档保存

Usage Examples | 使用示例:

// Open and read a document
try (PdfDocument doc = OpenPdf.open(Path.of("document.pdf"))) {
    int pages = doc.getPageCount();
    Metadata meta = doc.getMetadata();
    doc.save(Path.of("copy.pdf"));
}

Security | 安全性:

  • Thread-safe: No — implementations are not thread-safe - 线程安全: 否 — 实现非线程安全
  • Null-safe: Yes — parameters are validated - 空值安全: 是 — 参数已验证
Since:
JDK 25, opencode-base-pdf V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Adds a new blank page 添加新的空白页
    addPage(PageSize pageSize)
    Adds a new page with specified size 添加指定大小的新页面
    void
    Closes the document and releases resources 关闭文档并释放资源
    Gets the interactive form 获取交互表单
    Gets document metadata 获取文档元数据
    getPage(int pageNumber)
    Gets a specific page (1-based index) 获取指定页面(从1开始)
    int
    Gets the total page count 获取总页数
    Gets all pages 获取所有页面
    boolean
    Checks if document has interactive form 检查文档是否有交互表单
    void
    insertPage(int pageNumber, PdfPage page)
    Inserts a page at specified position 在指定位置插入页面
    boolean
    Checks if document is encrypted 检查文档是否加密
    void
    removePage(int pageNumber)
    Removes a page 删除页面
    void
    save(OutputStream outputStream)
    Saves document to output stream 保存文档到输出流
    void
    save(Path path)
    Saves document to file 保存文档到文件
    void
    Sets document metadata 设置文档元数据
    void
    setPassword(String userPassword, String ownerPassword)
    Sets document password protection 设置文档密码保护
    byte[]
    Saves document to byte array 保存文档到字节数组
  • Method Details

    • getPageCount

      int getPageCount()
      Gets the total page count 获取总页数
      Returns:
      page count | 页数
    • getPage

      PdfPage getPage(int pageNumber)
      Gets a specific page (1-based index) 获取指定页面(从1开始)
      Parameters:
      pageNumber - page number (1-based) | 页码(从1开始)
      Returns:
      PDF page | PDF 页面
      Throws:
      OpenPdfException - if page number is invalid | 页码无效时抛出异常
    • getPages

      List<PdfPage> getPages()
      Gets all pages 获取所有页面
      Returns:
      list of pages | 页面列表
    • addPage

      PdfPage addPage()
      Adds a new blank page 添加新的空白页
      Returns:
      the new page | 新页面
    • addPage

      PdfPage addPage(PageSize pageSize)
      Adds a new page with specified size 添加指定大小的新页面
      Parameters:
      pageSize - page size | 页面大小
      Returns:
      the new page | 新页面
    • insertPage

      void insertPage(int pageNumber, PdfPage page)
      Inserts a page at specified position 在指定位置插入页面
      Parameters:
      pageNumber - position (1-based) | 位置(从1开始)
      page - page to insert | 要插入的页面
      Throws:
      OpenPdfException - if position is invalid | 位置无效时抛出异常
    • removePage

      void removePage(int pageNumber)
      Removes a page 删除页面
      Parameters:
      pageNumber - page number to remove (1-based) | 要删除的页码(从1开始)
      Throws:
      OpenPdfException - if page number is invalid | 页码无效时抛出异常
    • getMetadata

      Metadata getMetadata()
      Gets document metadata 获取文档元数据
      Returns:
      metadata | 元数据
    • setMetadata

      void setMetadata(Metadata metadata)
      Sets document metadata 设置文档元数据
      Parameters:
      metadata - metadata to set | 要设置的元数据
    • hasForm

      boolean hasForm()
      Checks if document has interactive form 检查文档是否有交互表单
      Returns:
      true if has form | 如果有表单返回 true
    • getForm

      PdfForm getForm()
      Gets the interactive form 获取交互表单
      Returns:
      PDF form, or null if none | PDF 表单,如果没有则返回 null
    • save

      void save(Path path)
      Saves document to file 保存文档到文件
      Parameters:
      path - target file path | 目标文件路径
      Throws:
      OpenPdfException - if saving fails | 保存失败时抛出异常
    • save

      void save(OutputStream outputStream)
      Saves document to output stream 保存文档到输出流
      Parameters:
      outputStream - target stream | 目标输出流
      Throws:
      OpenPdfException - if saving fails | 保存失败时抛出异常
    • toBytes

      byte[] toBytes()
      Saves document to byte array 保存文档到字节数组
      Returns:
      PDF bytes | PDF 字节数组
    • isEncrypted

      boolean isEncrypted()
      Checks if document is encrypted 检查文档是否加密
      Returns:
      true if encrypted | 如果加密返回 true
    • setPassword

      void setPassword(String userPassword, String ownerPassword)
      Sets document password protection 设置文档密码保护
      Parameters:
      userPassword - user password (for opening) | 用户密码(用于打开)
      ownerPassword - owner password (for permissions) | 所有者密码(用于权限)
    • close

      void close()
      Closes the document and releases resources 关闭文档并释放资源
      Specified by:
      close in interface AutoCloseable