1. 产品
  2.   Aspose.PDF
  3.   Aspose.PDF FOSS for .NET

Aspose.PDF FOSS(适用于 .NET)

在 .NET 中创建和操作 PDF 文档 — 支持批注、表单字段、文本提取、页面管理、加密以及 HTML/SVG/Markdown 导入。MIT 许可证。

免费 MIT 许可证的 .NET 库,用于 PDF 创建和操作

Aspose.PDF FOSS for .NET 是一个基于 MIT 许可证的库,用于在 C# 和 .NET 中创建和处理 PDF 文档。核心入口点是 Document 类,它支持从文件路径、字节数组、流或受密码保护的来源构造,并提供 OpenSaveMergeEncryptDecryptConvert 等跨完整 PDF 规范的操作。

该库公开了 805 个类,涵盖通过 DocumentPagePageCollection 实现的文档结构;通过 AnnotationCollection 实现的丰富批注(包括 AddTextAnnotationAddLinkAnnotationAddHighlightAnnotationAddWatermarkAnnotationAddRedactAnnotation);使用 FormButtonFieldCheckboxFieldRadioButtonFieldChoiceField 的交互式表单;通过 TextFragmentAbsorberTextFragment 进行的文本提取和搜索;使用 AbsorbedTableAbsorbedRowAbsorbedCell 的表格检测;以及通过 Document.EncryptDocument.Decrypt 实现的文档安全。格式转换支持 HTML、SVG 和 Markdown 导入以及 PDF 导出。

从 NuGet 安装 Aspose.PDF FOSS for .NET:dotnet add package Aspose.Pdf.Foss --version 0.1.0-alpha。该库完全采用 MIT 许可证,无运行时费用或使用限制。有关企业功能和支持,请参阅 Aspose.PDF for .NET — Enterprise Product

Document Structure and Page Management

  • Document API: Create new PDFs with Document() or open existing files via Document.Open(path), Document.Open(data), or Document.Open(stream).
  • Page management: Add, access, and reorder pages via PageCollection. Each Page exposes Width, Height, MediaBox, rotation, and Annotations.
  • Merge and split: Combine multiple files with Document.Merge(documents) or Document.MergeDocuments(files). Import specific pages with Document.ImportPage and Document.ImportPages.
  • Metadata: Read and write document metadata via DocumentInfo and Document.GetOrCreateMetadata().
  • Serialization: Persist documents with Document.Save(filename), Document.Save(stream, format), or Document.ToArray().

Common Document Processing Scenarios

  • PDF generation: Build documents programmatically by creating a Document instance and populating pages with content.
  • Document analysis: Open existing files for metadata inspection, page dimension measurement, or form field enumeration.
  • Page manipulation: Resize, rotate, or reorder pages using Document.ImportPage and the Page API.
  • Batch merging: Assemble report sections or invoice pages into a single PDF using Document.MergeDocuments.

Annotations and Form Fields

  • Rich annotation API: AnnotationCollection exposes methods for text, link, highlight, underline, strikeout, square, circle, line, ink, stamp, caret, redact, watermark, polygon, and polyline annotations.
  • Link actions: Add URI links with AddLinkAnnotation(rect, uri) or internal page-jump links with AddLinkAnnotation(rect, destinationPage, destRect).
  • Interactive forms: Access the document form via Document.Form to enumerate or create ButtonField, CheckboxField, RadioButtonField, and ChoiceField instances.
  • Annotation selection: Filter annotations by type across pages using AnnotationSelector with per-type Visit overloads.
  • Annotation flags: Control visibility, print, and lock behaviour via AnnotationFlags.

Document Review and Collaboration Scenarios

  • Markup review: Add highlight, underline, or free-text annotations for document review workflows.
  • Redaction: Permanently remove sensitive content regions using AddRedactAnnotation.
  • Watermarking: Stamp CONFIDENTIAL or APPROVED text onto pages with AddWatermarkAnnotation and AddStampAnnotation.
  • Interactive forms: Build fillable documents with radio buttons, checkboxes, and choice lists for data capture pipelines.

Text Extraction and Search

  • Text search: Use TextFragmentAbsorber to locate all text fragments on a page or across a document, with optional phrase matching.
  • Fragment properties: Each TextFragment exposes Text, Position, TextState (font, size, colour), and bounding rectangle.
  • Text state control: TextFragmentState provides access to Font, FontSize, ForegroundColor, BackgroundColor, and rendering mode.
  • Table detection: Identify and extract tabular content via AbsorbedTable, AbsorbedRow, and AbsorbedCell, each providing Rect and TextFragments.

Text and Data Extraction Scenarios

  • Content indexing: Extract all page text for full-text search pipelines.
  • Form data extraction: Read submitted field values for server-side processing.
  • Invoice parsing: Use AbsorbedTable to parse tabular row-and-column data from financial PDFs.
  • Redline comparison: Locate specific text fragments for precise position-based annotation or replacement.

Security and Encryption

  • AES and RC4 encryption: Encrypt documents with Document.Encrypt(userPassword, ownerPassword, permissions, algorithm) using Algorithm.AESx128, AESx256, or RC4x128.
  • Permission control: Restrict print, copy, and accessibility via DocumentPrivilege.
  • Password management: Change user and owner passwords with Document.ChangePasswords.
  • Decryption: Remove protection with Document.Decrypt().
  • Digital signatures: Access signature fields and PKCS#7 data through the Signature and Algorithm types.

Document Security Scenarios

  • Confidential distribution: Encrypt PDFs with per-recipient passwords before delivery.
  • Print-only contracts: Apply owner passwords with print-only DocumentPrivilege to prevent copying.
  • Compliance archiving: Strip encryption from internal archives after security review.
  • Signature verification: Inspect embedded digital signatures in received PDFs for authenticity checks.

Open a PDF, Add a Link Annotation, and Save

Open an existing PDF, attach a URI link annotation to page 1, and persist the result.

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());

Add a Watermark Annotation

Stamp a watermark onto page 1 of an existing document and round-trip through serialization.

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);

Extract Text Fragments from a Page

Use TextFragmentAbsorber to enumerate all text fragments on the first page.

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);
}

常见问题

What is the licensing model for Aspose.PDF FOSS for .NET?

Aspose.PDF FOSS for .NET is released under the MIT License, allowing free use in commercial products with no runtime fees. The source code is publicly available on GitHub.

How do I install Aspose.PDF FOSS for .NET?

Install from NuGet using dotnet add package Aspose.Pdf.Foss --version 0.1.0-alpha. The library targets .NET 8 and later with no native dependencies.

Which file formats does Aspose.PDF FOSS for .NET support?

The library reads and writes PDF and supports import of HTML, SVG, and Markdown. Raster export is also available — PDF pages can be rendered to BMP, JPEG, PNG, and TIFF.

How do I create a new PDF document?

Use Document.Create() to produce an empty document, then add pages and save with Document.Save(stream) or Document.ToArray().

Is a license key required to use Aspose.PDF FOSS for .NET?

No license key or activation step is required. The library runs fully unlicensed at no cost under the MIT License.

  

支持和学习资源

 中文