C#将制定文件夹下的PDF文件合并成一个并输出至指定路径

2021-02-11 17:18

阅读:603

///


/// 将源路径下的PDF合并至目标路径下
///

/// 源路径
/// 目标路径
/// 新文件名
private void MergePDF(string SourcePath,string TargetPath,string NewFileName)
{
string[] filenames = Directory.GetFiles(SourcePath, "*.pdf", SearchOption.AllDirectories);
int fileNum = filenames.Length;//pdf的个数
Dictionary myDictionary = new Dictionary();
if (fileNum {
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");
}


评论


亲,登录后才可以留言!