c# 将多个PDF文件合并成一个

 引用   iTextSharp.text

/// <summary>
/// 调用方法
/// </summary>
public static void button1_Click()
{
string path_Source = @"C:\Users\Administrator\Desktop\原始备份\原始备份";//PDF文档文件夹位置
string path_Target = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//计算机桌面的路径
MergePDF(path_Source, path_Target, "PDF总文档.pdf");
}

#region 合并多个PDF
/// <param name="SourcePath">源路径</param>
/// <param name="TargetPath">目标路径</param>
/// <param name="NewFileName">新文件名</param>
private static void MergePDF(string SourcePath, string TargetPath, string NewFileName)
{
//需要合并的pdf集合
string[] fileList = Directory.GetFiles(SourcePath, "*.pdf", SearchOption.AllDirectories);
//合并到的总PDF
string outMergeFile = TargetPath + "\\" + NewFileName;
PdfReader reader;
iTextSharp.text.Document document = new iTextSharp.text.Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage newPage;
for (int i = 0; i < fileList.Length; i++)
{
reader = new PdfReader(fileList[i]);
int iPageNum = reader.NumberOfPages;
for (int j = 1; j <= iPageNum; j++)
{
document.NewPage();
newPage = writer.GetImportedPage(reader, j);
cb.AddTemplate(newPage, 0, 0);
}
}
document.Close();
}
#endregion

上一篇:nginx报错


下一篇:(1)《QT+OpenGL学习之我见》初始化窗口及三个重要函数 vs+Qt