PDFドキュメントへの添付ファイルの追加

Contents
[ ]

添付ファイルにはさまざまな情報を含めることができ、さまざまなファイルタイプが使用できます。この記事では、PDFファイルに添付ファイルを追加する方法について説明します。

次のコードスニペットは、Aspose.Drawingライブラリでも機能します。

  1. 新しいC#プロジェクトを作成します。
  2. Aspose.PDF DLLへの参照を追加します。
  3. Documentオブジェクトを作成します。
  4. 追加するファイルとファイルの説明を持つFileSpecificationオブジェクトを作成します。
  5. FileSpecificationオブジェクトをDocumentオブジェクトのEmbeddedFilesコレクションに、コレクションのAddメソッドを使用して追加します。

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