将附件添加到 PDF 文档

Contents
[ ]

附件可以包含各种信息,并且可以是多种文件类型。本文解释了如何将附件添加到 PDF 文件。

下一个代码片段也适用于 Aspose.Drawing 库。

  1. 创建一个新的 C# 项目。
  2. 添加对 Aspose.PDF DLL 的引用。
  3. 创建一个 Document 对象。
  4. 创建一个 FileSpecification 对象,包含您要添加的文件和文件描述。
  5. 使用集合的 Add 方法将 FileSpecification 对象添加到 Document 对象的 EmbeddedFiles 集合中。

EmbeddedFiles 集合包含 PDF 文件中的所有附件。以下代码片段演示了如何在 PDF 文档中添加附件。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddEmbeddedFile()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "AddAttachment.pdf"))
    {
        // Setup new file to be added as attachment
        Aspose.Pdf.FileSpecification fileSpecification = new Aspose.Pdf.FileSpecification(dataDir + "test.txt", "Sample text file");

        // Add attachment to document's attachment collection
        document.EmbeddedFiles.Add(fileSpecification);

        // Save PDF document
        document.Save(dataDir + "AddAnnotations_out.pdf");
    }
}