PDF文件包(Portfolio)允许用户将多种不同类型的文件如Word、Excel、PDF、PowerPoint和图片等集合到一个PDF文件中,用户可以打开、更改PDF文件包中的单个文件,而不影响其他文件。本文将介绍如何使用Spire.PDF for .NET创建PDF文件包。
在创建PDF文件包时,Spire.PDF for .NET支持给包添加文件和文件夹。以下示例展示了如何使用Spire.PDF for .NET创建PDF文件包并添加文件。
C#
using System;
using Spire.Pdf;
namespace CreatePDFPortfolio
{
class Program
{
static void Main(string[] args)
{
String[] files = new String[] { "Instruction.pdf", "Sales Report.docx", "Sample.xlsx" };
//创建PdfDocument实例
using (PdfDocument doc = new PdfDocument())
{
//创建PDF文件包并添加文件
for (int i = 0; i < files.Length; i++)
{
doc.Collection.AddFile(files[i]);
}
//保存结果文档
doc.SaveToFile("PortfolioWithFiles.pdf", FileFormat.PDF);
doc.Dispose();
}
}
}
}
VB.NET
Imports System
Imports Spire.Pdf
Namespace CreatePDFPortfolio
Class Program
Private Shared Sub Main(ByVal args As String())
Dim files As String() = New String() {"Instruction.pdf", "Sales Report.docx", "Sample.xlsx"}
Using doc As PdfDocument = New PdfDocument()
For i As Integer = 0 To files.Length - 1
doc.Collection.AddFile(files(i))
Next
doc.SaveToFile("PortfolioWithFiles.pdf", FileFormat.PDF)
doc.Dispose()
End Using
End Sub
End Class
End Namespace
以下示例展示了如何使用Spire.PDF for .NET创建PDF文件包并添加文件夹。
C#
using System;
using Spire.Pdf;
using Spire.Pdf.Collections;
namespace CreatePDFPortfolio
{
class Program
{
static void Main(string[] args)
{
String[] files = new String[] { "Instruction.pdf", "Sales Report.docx", "Sample.xlsx" };
//创建PdfDocument实例
using (PdfDocument doc = new PdfDocument())
{
//创建PDF文件包,添加文件夹并向文件夹添加文件
for (int i = 0; i < files.Length; i++)
{
PdfFolder folder = doc.Collection.Folders.CreateSubfolder("Folder" + i);
folder.AddFile(files[i]);
}
//保存结果文档
doc.SaveToFile("PortfolioWithFolders.pdf", FileFormat.PDF);
doc.Dispose();
}
}
}
}
VB.NET
Imports System
Imports Spire.Pdf
Imports Spire.Pdf.Collections
Namespace CreatePDFPortfolio
Class Program
Private Shared Sub Main(ByVal args As String())
Dim files As String() = New String() {"Instruction.pdf", "Sales Report.docx", "Sample.xlsx"}
Using doc As PdfDocument = New PdfDocument()
For i As Integer = 0 To files.Length - 1
Dim folder As PdfFolder = doc.Collection.Folders.CreateSubfolder("Folder" & i)
folder.AddFile(files(i))
Next
doc.SaveToFile("PortfolioWithFolders.pdf", FileFormat.PDF)
doc.Dispose()
End Using
End Sub
End Class
End Namespace