PDF文件包(Portfolio)允许用户将多种不同类型的文件如Word、Excel、PDF、PowerPoint和图片等集合到一个PDF文件中,用户可以打开、更改PDF文件包中的单个文件。添加文件包时,可支持创建文件包时并直接将文件添加到文件包;或者创建文件包的同时创建文件夹,并将文件添加到文件夹。
环境准备:程序中需要使用到PDF类库工具(Spire.PDF for .NET hotfix 6.5.15及以上版本)下载后,解压,将Bin文件夹下的dll文件添加引用到VS程序。如下效果:
1. 创建文件包时直接添加文件
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
using Spire.Pdf; using System; namespace CreatePDFPortfolio_PDF { class Program { static void Main( string [] args) { //创建数组 String[] files = new String[] { "sample.pdf" , "sample.docx" , "sample.xlsx" , "sample.txt" , "sample.pptx" }; //创建PdfDocument实例 PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile( "test.pdf" ); //创建PDF文件包并添加文件 for ( int i = 0; i < files.Length; i++) { pdf.Collection.AddFile(files[i]); } //保存文档 pdf.SaveToFile( "PortfolioWithFiles.pdf" , FileFormat.PDF); System.Diagnostics.Process.Start( "PortfolioWithFiles.pdf" ); } } } |
VB.NET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
Imports Spire.Pdf Namespace CreatePDFPortfolio_PDF Class Program Private Shared Sub Main(args As String()) '创建数组 Dim files As [String]() = New [String]() { "sample.pdf" , "sample.docx" , "sample.xlsx" , "sample.txt" , "sample.pptx" } '创建PdfDocument实例 Dim pdf As New PdfDocument() pdf.LoadFromFile( "test.pdf" ) '创建PDF文件包并添加文件 For i As Integer = 0 To files.Length - 1 pdf.Collection.AddFile(files(i)) Next '保存文档 pdf.SaveToFile( "PortfolioWithFiles.pdf" , FileFormat.PDF) System.Diagnostics.Process.Start( "PortfolioWithFiles.pdf" ) End Sub End Class End Namespace |
2. 创建文件包时创建文件夹,并将文件添加到文件夹
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
using Spire.Pdf; using Spire.Pdf.Collections; using System; namespace CreatePDFPortfolio2 { class Program { static void Main( string [] args) { //创建数组 String[] files = new String[] { "sample.pdf" , "sample.docx" , "sample.xlsx" , "sample.txt" , "sample.pptx" }; //创建PdfDocument实例 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( "PortfolioWithFolders2.pdf" , FileFormat.PDF); System.Diagnostics.Process.Start( "PortfolioWithFolders2.pdf" ); } } } |
VB.NET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
Imports Spire.Pdf Imports Spire.Pdf.Collections Namespace CreatePDFPortfolio2 Class Program Private Shared Sub Main(args As String()) '创建数组 Dim files As [String]() = New [String]() { "sample.pdf" , "sample.docx" , "sample.xlsx" , "sample.txt" , "sample.pptx" } '创建PdfDocument实例 Dim doc As New PdfDocument() '创建PDF文件包,添加文件夹,并向文件夹添加文件 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( "PortfolioWithFolders2.pdf" , FileFormat.PDF) System.Diagnostics.Process.Start( "PortfolioWithFolders2.pdf" ) End Sub End Class End Namespace |
总结
到此这篇关于C#/VB.NET 在PDF中添加文件包(Portfolio)的方法的文章就介绍到这了,更多相关C#/VB.NET PDF 添加文件包内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/Yesi/archive/2020/06/29/13206277.html