Class OpenPdf
java.lang.Object
cloud.opencode.base.pdf.OpenPdf
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 Summary
Modifier and TypeMethodDescriptionstatic DocumentBuildercreate()Creates a new PDF document builder.static DocumentBuilderCreates a new PDF document builder with specified page size.extractFormFields(Path source) Extracts form fields from a PDF.static List<byte[]> extractImages(Path source) Extracts images from a PDF.static PdfExtractorCreates a PDF extractor.static StringextractText(Path source) Extracts all text from a PDF.static StringextractText(Path source, int... pageNumbers) Extracts text from specific pages.static PdfDocumentfillAndFlatten(Path source, Map<String, String> fields) Fills form and flattens (makes non-editable).static PdfDocumentFills form fields in a PDF.static MetadatagetMetadata(Path source) Gets PDF metadata.static intgetPageCount(Path source) Gets the page count of a PDF.static booleanChecks if a PDF contains forms.static booleanisEncrypted(Path source) Checks if a PDF is encrypted.static booleanChecks if a PDF is signed.static PdfDocumentmerge(List<PdfDocument> documents) Merges multiple PDF documents into one.static voidMerges multiple PDF files into one.static PdfMergermerger()Creates a PDF merger.static PdfDocumentopen(byte[] bytes) Opens an existing PDF document from byte array.static PdfDocumentopen(InputStream inputStream) Opens an existing PDF document from input stream.static PdfDocumentOpens an existing PDF document from file path.static PdfDocumentOpens a password-protected PDF document.static PdfDocumentSigns a PDF document.static PdfSignersigner()Creates a PDF signer.static List<PdfDocument> Splits a PDF by page ranges.static PdfSplittersplitter()Creates a PDF splitter.static List<PdfDocument> splitToPages(Path source) Splits a PDF into single pages.static List<SignatureInfo> verifySignatures(Path source) Verifies PDF signatures.
-
Method Details
-
create
Creates a new PDF document builder. 创建新的 PDF 文档构建器。- Returns:
- document builder | 文档构建器
-
create
Creates a new PDF document builder with specified page size. 创建指定页面大小的 PDF 文档构建器。- Parameters:
pageSize- page size | 页面大小- Returns:
- document builder | 文档构建器
-
open
Opens an existing PDF document from file path. 从文件路径打开已有 PDF 文档。- Parameters:
path- file path | 文件路径- Returns:
- PDF document | PDF 文档
- Throws:
OpenPdfException- if reading fails | 读取失败时抛出异常
-
open
Opens an existing PDF document from input stream. 从输入流打开已有 PDF 文档。- Parameters:
inputStream- input stream | 输入流- Returns:
- PDF document | PDF 文档
- Throws:
OpenPdfException- if reading fails | 读取失败时抛出异常
-
open
Opens an existing PDF document from byte array. 从字节数组打开已有 PDF 文档。- Parameters:
bytes- PDF bytes | PDF 字节数组- Returns:
- PDF document | PDF 文档
- Throws:
OpenPdfException- if reading fails | 读取失败时抛出异常
-
open
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
-
merge
Merges multiple PDF files into one. 将多个 PDF 文件合并为一个。- Parameters:
sources- source files | 源文件列表target- target file | 目标文件- Throws:
OpenPdfException- if merging fails | 合并失败时抛出异常
-
merge
Merges multiple PDF documents into one. 将多个 PDF 文档合并为一个。- Parameters:
documents- source documents | 源文档列表- Returns:
- merged document | 合并后的文档
- Throws:
OpenPdfException- if merging fails | 合并失败时抛出异常
-
splitter
Creates a PDF splitter. 创建 PDF 拆分器。- Returns:
- PDF splitter | PDF 拆分器
-
split
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
Splits a PDF into single pages. 将 PDF 拆分为单页文档。- Parameters:
source- source file | 源文件- Returns:
- single page documents | 单页文档列表
- Throws:
OpenPdfException- if splitting fails | 拆分失败时抛出异常
-
fillForm
Fills form fields in a PDF. 填充 PDF 表单字段。- Parameters:
source- source PDF with form | 带表单的源 PDFfields- field name to value mapping | 字段名到值的映射- Returns:
- PDF with filled form | 填充后的 PDF
- Throws:
OpenPdfException- if filling fails | 填充失败时抛出异常
-
fillAndFlatten
Fills form and flattens (makes non-editable). 填充表单并扁平化(使其不可编辑)。- Parameters:
source- source PDF with form | 带表单的源 PDFfields- field name to value mapping | 字段名到值的映射- Returns:
- flattened PDF | 扁平化后的 PDF
- Throws:
OpenPdfException- if operation fails | 操作失败时抛出异常
-
extractFormFields
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
-
sign
Signs a PDF document. 对 PDF 文档进行签名。- Parameters:
source- source PDF | 源 PDFkeyStore- key store path | 密钥库路径password- key store password | 密钥库密码alias- certificate alias | 证书别名- Returns:
- signed PDF | 签名后的 PDF
- Throws:
OpenPdfException- if signing fails | 签名失败时抛出异常
-
verifySignatures
Verifies PDF signatures. 验证 PDF 签名。- Parameters:
source- signed PDF | 签名的 PDF- Returns:
- signature validation results | 签名验证结果
- Throws:
OpenPdfException- if verification fails | 验证失败时抛出异常
-
extractor
Creates a PDF extractor. 创建 PDF 提取器。- Returns:
- PDF extractor | PDF 提取器
-
extractText
Extracts all text from a PDF. 从 PDF 提取所有文本。- Parameters:
source- source PDF | 源 PDF- Returns:
- extracted text | 提取的文本
- Throws:
OpenPdfException- if extraction fails | 提取失败时抛出异常
-
extractText
Extracts text from specific pages. 从指定页面提取文本。- Parameters:
source- source PDF | 源 PDFpageNumbers- page numbers (1-based) | 页码(从1开始)- Returns:
- extracted text | 提取的文本
- Throws:
OpenPdfException- if extraction fails | 提取失败时抛出异常
-
extractImages
Extracts images from a PDF. 从 PDF 提取图像。- Parameters:
source- source PDF | 源 PDF- Returns:
- extracted images as byte arrays | 提取的图像字节数组
- Throws:
OpenPdfException- if extraction fails | 提取失败时抛出异常
-
getPageCount
Gets the page count of a PDF. 获取 PDF 页数。- Parameters:
source- source PDF | 源 PDF- Returns:
- page count | 页数
- Throws:
OpenPdfException- if reading fails | 读取失败时抛出异常
-
getMetadata
Gets PDF metadata. 获取 PDF 元数据。- Parameters:
source- source PDF | 源 PDF- Returns:
- document metadata | 文档元数据
- Throws:
OpenPdfException- if reading fails | 读取失败时抛出异常
-
isEncrypted
Checks if a PDF is encrypted. 检查 PDF 是否加密。- Parameters:
source- source PDF | 源 PDF- Returns:
- true if encrypted | 如果加密返回 true
- Throws:
OpenPdfException- if reading fails | 读取失败时抛出异常
-
hasForm
Checks if a PDF contains forms. 检查 PDF 是否包含表单。- Parameters:
source- source PDF | 源 PDF- Returns:
- true if contains forms | 如果包含表单返回 true
- Throws:
OpenPdfException- if reading fails | 读取失败时抛出异常
-
isSigned
Checks if a PDF is signed. 检查 PDF 是否已签名。- Parameters:
source- source PDF | 源 PDF- Returns:
- true if signed | 如果已签名返回 true
- Throws:
OpenPdfException- if reading fails | 读取失败时抛出异常
-