Aspose.PDF FOSS for .NET 是一个 MIT 许可证的库,用于在 C# 和 .NET 中创建和处理 PDF 文档。核心入口点是 Document 类,它支持从文件路径、字节数组、流或受密码保护的来源构造,并公开 Open、Save、Merge、Encrypt、Decrypt 和 Convert 操作,覆盖完整的 PDF 规范。
该库公开了 805 个类,涵盖通过 Document、Page 和 PageCollection 的文档结构;通过 AnnotationCollection(包括 AddTextAnnotation、AddLinkAnnotation、AddHighlightAnnotation、AddWatermarkAnnotation 和 AddRedactAnnotation)实现丰富的批注;使用 Form、ButtonField、CheckboxField、RadioButtonField 和 ChoiceField 的交互式表单;通过 TextFragmentAbsorber 和 TextFragment 进行文本提取和搜索;使用 AbsorbedTable、AbsorbedRow 和 AbsorbedCell 检测表格;以及通过 Document.Encrypt 和 Document.Decrypt 实现文档安全。格式转换支持 HTML、SVG 和 Markdown 导入,同时支持 PDF 导出。
从 NuGet 安装 Aspose.PDF FOSS 用于 .NET: dotnet add package Aspose.Pdf.Foss --version 0.1.0-alpha。该库完全采用 MIT 许可证,无运行时费用或使用限制。有关企业功能和支持,请参阅 Aspose.PDF for .NET — Enterprise Product。
Document() 创建新 PDF,或通过 Document.Open(path)、Document.Open(data) 或 Document.Open(stream) 打开现有文件。PageCollection 添加、访问和重新排序页面。每个 Page 都公开 Width、Height、MediaBox、旋转和 Annotations。Document.Merge(documents) 或 Document.MergeDocuments(files) 合并多个文件。使用 Document.ImportPage 和 Document.ImportPages 导入特定页面。DocumentInfo 和 Document.GetOrCreateMetadata() 读取和写入文档元数据。Document.Save(filename)、Document.Save(stream, format) 或 Document.ToArray() 持久化文档。Document 实例并向页面填充内容,以编程方式构建文档。Document.ImportPage 和 Page API 调整大小、旋转或重新排序页面。Document.MergeDocuments 将报告章节或发票页面合并为单个 PDF。AnnotationCollection 提供用于文本、链接、突出显示、下划线、删除线、方形、圆形、线条、墨迹、印章、插入符号、编辑遮蔽、水印、多边形和折线注释的方法。AddLinkAnnotation(rect, uri) 添加 URI 链接,或使用 AddLinkAnnotation(rect, destinationPage, destRect) 添加内部页面跳转链接。Document.Form 访问文档表单,以枚举或创建 ButtonField、CheckboxField、RadioButtonField 和 ChoiceField 实例。AnnotationSelector 在跨页面中按类型过滤注释,并使用每种类型的 Visit 重载。AnnotationFlags 控制可见性、打印和锁定行为。AddRedactAnnotation 的敏感内容区域。AddWatermarkAnnotation 和 AddStampAnnotation 在页面上添加 CONFIDENTIAL 或 APPROVED 文本。TextFragmentAbsorber 在页面上或跨文档定位所有文本片段,可选短语匹配。TextFragment 暴露 Text、Position、TextState(字体、大小、颜色),以及边界矩形。TextFragmentState 提供对 Font、FontSize、ForegroundColor、BackgroundColor 以及渲染模式的访问。AbsorbedTable、AbsorbedRow 和 AbsorbedCell 识别并提取表格内容,每个提供 Rect 和 TextFragments。AbsorbedTable 从财务 PDF 中解析表格行列数据。Algorithm.AESx128、AESx256 或 RC4x128 对 Document.Encrypt(userPassword, ownerPassword, permissions, algorithm) 加密文档。DocumentPrivilege 限制打印、复制和可访问性。Document.ChangePasswords 更改用户和所有者密码。Document.Decrypt() 移除保护。Signature 和 Algorithm 类型访问签名字段和 PKCS#7 数据。DocumentPrivilege 以防止复制。打开现有的 PDF,在第 1 页附加 URI 链接注释,并持久化结果。
using Aspose.Pdf;
var data = System.IO.File.ReadAllBytes("input.pdf");
using var doc = Document.Open(data);
var page = doc.Pages[1];
var action = PdfAction.CreateUri("https://aspose.com");
page.Annotations.AddLinkAnnotation(
new Rectangle(50, 700, 200, 720), action);
using var ms = new System.IO.MemoryStream();
doc.Save(ms);
System.IO.File.WriteAllBytes("output.pdf", ms.ToArray());
在现有文档的第 1 页上加上水印,并通过序列化进行往返。
using Aspose.Pdf;
var input = System.IO.File.ReadAllBytes("report.pdf");
using var doc = Document.Open(input);
doc.Pages[1].Annotations.AddWatermarkAnnotation(
new Rectangle(0, 0, 612, 792), "CONFIDENTIAL");
var saved = doc.ToArray();
System.IO.File.WriteAllBytes("watermarked.pdf", saved);
使用 TextFragmentAbsorber 来枚举第一页上的所有文本片段。
using Aspose.Pdf;
using Aspose.Pdf.Text;
var data = System.IO.File.ReadAllBytes("document.pdf");
using var doc = Document.Open(data);
var absorber = new TextFragmentAbsorber();
absorber.Visit(doc.Pages[1]);
foreach (var fragment in absorber.TextFragments)
{
Console.WriteLine(fragment.Text);
}
Aspose.PDF FOSS for .NET 在 MIT 许可证下发布,允许在商业产品中免费使用且无需运行时费用。源代码已在 GitHub 上公开。
使用 dotnet add package Aspose.Pdf.Foss --version 0.1.0-alpha 从 NuGet 安装。该库面向 .NET 8 及更高版本,无原生依赖。
该库支持读取和写入 PDF,并支持导入 HTML、SVG 和 Markdown。还提供光栅导出——PDF 页面可渲染为 BMP、JPEG、PNG 和 TIFF。
使用 Document.Create() 创建空文档,然后添加页面,并使用 Document.Save(stream) 或 Document.ToArray() 保存。
无需许可证密钥或激活步骤。该库在 MIT 许可证下免费运行,无需任何费用。