PDF 库功能
PDF 库功能
此页面概述了 Aspose.PDF 针对 Go 的 FOSS 中的主要功能领域。每个章节都链接到专门的开发者指南页面,提供完整的代码示例。
文档管理
Document 是核心类型。它支持打开(Open、OpenWithPassword)、创建(NewDocument)、保存(Save),以及对页面集合进行操作(Split、Append、Extract、PageCount)。SetMetadata 和 Metadata 提供对 Info 字典字段的读写访问。
doc, _ := pdf.Open("input.pdf")
pages, _ := doc.Split()
fmt.Printf("Split into %d pages\n", len(pages))安全与加密
EncryptionOptions 通过一次调用 Document.SetEncryption 配置基于密码的加密。支持的算法有:AES-128(默认)、AES-256(ISO 32000-2 / PDF 2.0)以及 RC4-128。Permissions 可独立控制各项操作——打印、复制、可访问性。
doc, _ := pdf.Open("doc.pdf")
doc.SetEncryption(pdf.EncryptionOptions{
UserPassword: "user",
OwnerPassword: "owner",
Permissions: &pdf.Permissions{AllowPrint: true},
})
doc.Save("doc_secured.pdf")AcroForm 处理
Document.Form 返回一个 *Form,提供对所有字段的访问。类型断言暴露具体的字段类型:TextBoxField、CheckboxField、RadioButtonField、ComboBoxField、ListBoxField和ButtonField。字段值可以被写入;外观会由 PDF 查看器自动重新生成。
doc, _ := pdf.Open("form.pdf")
text := doc.Form().Field("name").(*pdf.TextBoxField)
text.SetValue("Jane Doe")
doc.Save("form_filled.pdf")表格布局
Table、Row 和 Cell 构建页面上的网格布局。BorderInfo 设置每个单元格或整张表的边框样式。MarginInfo 设置内边距。TextStyle、Font、Color、HAlign 和 VAlign 控制文本呈现。
书签与导航
OutlineItemCollection 表示单个书签。可配置标题、粗体/斜体样式、链接颜色以及目标(DestinationXYZ、DestinationFit、DestinationFitH)。嵌套书签构建章节/节层级结构。NamedDestinations 提供可通过名称访问的可复用页面目标。
批注
AnnotationCollection 包含页面上的所有批注。可用类型包括 TextAnnotation、FreeTextAnnotation、StampAnnotation、HighlightAnnotation、LinkAnnotation、RedactAnnotation、InkAnnotation、LineAnnotation、SquareAnnotation 和 CircleAnnotation。
图像处理
Image 表示嵌入的图像资源。ImageInfo 暴露尺寸和颜色空间。图像可以直接添加到页面或嵌入表格单元格中。OptimizeImageOptions 提供用于文件大小缩减的压缩参数。
渲染为图像
BmpDevice、PngDevice、JpegDevice、GifDevice 和 TiffDevice 将单个 PDF 页面渲染为光栅图像。每个设备使用 Resolution 值构建,并暴露一个 Render* 方法,将输出写入 io.Writer。RenderOptions 控制渲染参数——输出分辨率(DPI)和背景颜色。
doc, _ := pdf.Open("report.pdf")
page, _ := doc.Page(1)
dev := pdf.NewPngDevice(pdf.Resolution{DPI: 150})
f, _ := os.Create("page1.png")
defer f.Close()
dev.Process(page, f)文本搜索
SearchText 接受查询字符串并 SearchOptions,返回一个 TextMatch 结果切片。每个 TextMatch 暴露匹配的文本、其在页面上的边界框以及找到它的页面索引。
doc, _ := pdf.Open("report.pdf")
matches, _ := doc.SearchText("invoice", pdf.SearchOptions{CaseInsensitive: true})
for _, m := range matches {
fmt.Printf("Found %q on page %d\n", m.Text, m.PageNumber)
}表单扁平化
Document.Flatten 将所有交互式表单字段转换为静态内容。字段值被烘焙进页面图形中,表单字段被移除,从而生成一个不可编辑的 PDF。
doc, _ := pdf.Open("form_filled.pdf")
doc.Flatten()
doc.Save("form_flat.pdf")API Reference 摘要
| 类型 | Purpose |
|---|---|
Document | 核心文档容器 — 打开、创建、保存、拆分、合并 |
Page | 单页访问 — 大小、注释、内容 |
Form | AcroForm 容器 — 字段枚举和访问 |
Table | 网格布局元素 |
EncryptionOptions | 密码和算法配置 |
Permissions | 每次操作的访问控制标志 |
OutlineItemCollection | 书签树节点 |
AnnotationCollection | 页面批注容器 |
Metadata | 信息字典 读取/写入 |
Image | 嵌入式图像资源 |
BmpDevice | 将 PDF 页面渲染为 BMP |
PngDevice | 将 PDF 页面渲染为 PNG |
JpegDevice | 将 PDF 页面渲染为 JPEG |
TiffDevice | 将 PDF 页面渲染为 TIFF |
GifDevice | 将 PDF 页面渲染为 GIF |
RenderOptions | 渲染参数 — DPI 和背景颜色 |
SearchOptions | 文本搜索配置 |
TextMatch | 带边界框的单个文本搜索结果 |
LinearGradient | 矢量形状的线性渐变填充 |
RadialGradient | 矢量形状的径向渐变填充 |