使用 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 文件)汇集到一个统一的容器中。原始文件保留其各自的身份,但被组装成一个 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");
    }
}