C#將制定文件夾下的PDF文件合並成一個並輸出至指定路徑
/// <summary>
/// 將源路徑下的PDF合並至目標路徑下
/// </summary>
/// <param name="SourcePath">源路徑</param>
/// <param name="TargetPath">目標路徑</param>
/// <param name="NewFileName">新文件名</param>
private void MergePDF(string SourcePath,string TargetPath,string NewFileName)
{
string[] filenames = Directory.GetFiles(SourcePath, "*.pdf", SearchOption.AllDirectories);
int fileNum = filenames.Length;//pdf的個數
Dictionary<int, string> myDictionary = new Dictionary<int, string>();
if (fileNum < 2)
{
return;//源路徑下只有一個PDF文件,那麽不合並
}
Aspose.Pdf.Document a = new Document();
foreach (var file in filenames)//遍歷源路徑,獲取該路徑下所有PDF文件的path
{
Document b = new Document(file);
foreach (Page item in b.Pages)
{
a.Pages.Add(item);
}
a.Save(TargetPath + "\\" + NewFileName);
}
}
//調用,這裏使用了Aspose.Pdf.dll
using Aspose.Pdf;
using System.IO;
private void button1_Click(object sender, EventArgs e)
{
string path_Source=@"C:\Users\Evan\Documents\PDF 文件\AutoSave";
string path_Target=Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//計算機桌面的路徑
MergePDF(path_Source, path_Target,"666666.pdf");
}
C#將制定文件夾下的PDF文件合並成一個並輸出至指定路徑