Interface PdfDocument
- All Superinterfaces:
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 TypeMethodDescriptionaddPage()Adds a new blank page 添加新的空白页Adds a new page with specified size 添加指定大小的新页面voidclose()Closes the document and releases resources 关闭文档并释放资源getForm()Gets the interactive form 获取交互表单Gets document metadata 获取文档元数据getPage(int pageNumber) Gets a specific page (1-based index) 获取指定页面(从1开始)intGets the total page count 获取总页数getPages()Gets all pages 获取所有页面booleanhasForm()Checks if document has interactive form 检查文档是否有交互表单voidinsertPage(int pageNumber, PdfPage page) Inserts a page at specified position 在指定位置插入页面booleanChecks if document is encrypted 检查文档是否加密voidremovePage(int pageNumber) Removes a page 删除页面voidsave(OutputStream outputStream) Saves document to output stream 保存文档到输出流voidSaves document to file 保存文档到文件voidsetMetadata(Metadata metadata) Sets document metadata 设置文档元数据voidsetPassword(String userPassword, String ownerPassword) Sets document password protection 设置文档密码保护byte[]toBytes()Saves document to byte array 保存文档到字节数组
-
Method Details
-
getPageCount
int getPageCount()Gets the total page count 获取总页数- Returns:
- page count | 页数
-
getPage
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
-
addPage
-
addPage
-
insertPage
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
-
setMetadata
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
Saves document to file 保存文档到文件- Parameters:
path- target file path | 目标文件路径- Throws:
OpenPdfException- if saving fails | 保存失败时抛出异常
-
save
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
-
close
void close()Closes the document and releases resources 关闭文档并释放资源- Specified by:
closein interfaceAutoCloseable
-