PDFポートフォリオの操作

PDFポートフォリオの作成方法

Aspose.PDFを使用すると、Documentクラスを使用してPDFポートフォリオ文書を作成できます。FileSpecificationクラスを使用してファイルを取得した後、Document.Collectionオブジェクトにファイルを追加します。ファイルが追加されたら、DocumentクラスのSaveメソッドを使用してポートフォリオ文書を保存します。

以下の例では、Microsoft Excelファイル、Word文書、および画像ファイルを使用してPDFポートフォリオを作成します。

以下のコードは、次のポートフォリオを生成します。

以下のコードスニペットは、Aspose.PDF.Drawingライブラリでも動作します。

Aspose.PDFで作成されたPDFポートフォリオ

Aspose.PDF for .NETで作成されたPDFポートフォリオ

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

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        // Instantiate document Collection object
        document.Collection = new Aspose.Pdf.Collection();

        // Get Files to add to Portfolio
        var excel = new Aspose.Pdf.FileSpecification(dataDir + "HelloWorld.xlsx");
        var word = new Aspose.Pdf.FileSpecification(dataDir + "HelloWorld.docx");
        var image = new Aspose.Pdf.FileSpecification(dataDir + "aspose-logo.jpg");

        // Provide description of the files
        excel.Description = "Excel File";
        word.Description = "Word File";
        image.Description = "Image File";

        // Add files to document collection
        document.Collection.Add(excel);
        document.Collection.Add(word);
        document.Collection.Add(image);

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

PDFポートフォリオからファイルを抽出する

PDFポートフォリオを使用すると、さまざまなソース(たとえば、PDF、Word、Excel、JPEGファイル)からのコンテンツを1つの統一コンテナにまとめることができます。元のファイルはそれぞれのアイデンティティを保持しながら、PDFポートフォリオファイルに組み込まれます。ユーザーは、他のコンポーネントファイルとは独立して、各コンポーネントファイルを開いて、読み取り、編集、フォーマットできます。

Aspose.PDFを使用すると、Documentクラスを使用してPDFポートフォリオ文書を作成できます。また、PDFポートフォリオからファイルを抽出する機能も提供しています。

以下のコードスニペットは、PDFポートフォリオからファイルを抽出する手順を示しています。

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "PDFPortfolio.pdf"))
    {
        // Get collection of embedded files
        Aspose.Pdf.EmbeddedFileCollection embeddedFiles = document.EmbeddedFiles;
        // Iterate through individual file of Portfolio
        foreach (Aspose.Pdf.FileSpecification fileSpecification in embeddedFiles)
        {
            // Get the attachment and write to file or stream
            byte[] fileContent = new byte[fileSpecification.Contents.Length];
            fileSpecification.Contents.Read(fileContent, 0, fileContent.Length);
            string filename = Path.GetFileName(fileSpecification.Name);
            // Save the extracted file to some location
            using (FileStream fileStream = new FileStream(dataDir + filename + "_out", FileMode.Create))
            {
                fileStream.Write(fileContent, 0, fileContent.Length);
            }
        }
    }
}

PDFポートフォリオからファイルを抽出する

PDFポートフォリオからファイルを削除する

PDFポートフォリオからファイルを削除するには、以下のコード行を使用してみてください。

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "PDFPortfolio.pdf"))
    {
        document.Collection.Delete();
        // Save PDF document
        document.Save(dataDir + "NoPortFolio_out.pdf");
    }
}