C# 合并PDF文件
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using iTextSharp.text; using iTextSharp.text.pdf; namespace MEAS.Common { public class PdfHelper { //定义一个用于保存静态变量的实例 private static PdfHelper instance = null; //定义一个保证线程同步的标识 private static readonly object locker = new object(); //构造函数为私有,使外界不能创建该类的实例 private PdfHelper() { } public static PdfHelper Instance { get { if (instance == null) { lock (locker) { if (instance == null) instance = new PdfHelper(); } } return instance; } } /// <summary> /// 合并PDF文件 /// </summary> /// <param name="fileList">需要合并的PDF五年级</param> /// <param name="outMergeFile">合并后文件路径</param> public string MergePDFFiles(string[] fileList, string mergeFile) { PdfReader reader; Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(mergeFile, 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(); return mergeFile; } } }
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!