您的位置:首页 > 编程语言 > C#

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

2018-03-01 17:53 423 查看
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(www.dfgjyl.cn/);
  
  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 + www.thd729.com"\\" + NewFileName);
  
  }
  
  }
  
  //调用,这里使用了Aspose.Pdf.dll
  
  using Aspose.Pdf;
  
  using System.IO;
  
  private void button1_Click(object sender, EventArgs e)
  
  {
  
  string path_Source=@"C:\www.ahor.net Users\Evan\Documents\PDF 文件\AutoSave";
  
  string path_Target=Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//计算机桌面的路径
  
  MergePDF(path_Source, path_Target,"666666.pdf");
  
  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐