/// <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 [email protected]"C:\Users\Evan\Documents\PDF 文件\AutoSave";
string path_Target=Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//计算机桌面的路径
MergePDF(path_Source, path_Target,"666666.pdf");
}