private void MergePDFFiles(string[] fileList, string outMergeFile) { List<PdfReader> readerList = new List<PdfReader>();//记录合并PDF集合 iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4.Rotate()); 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++) { if (!string.IsNullOrEmpty(fileList[i])) { PdfReader 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); } readerList.Add(reader); } } document.Close();
1 foreach (var rd in readerList) 2 { 3 rd.Dispose(); 4 }
原文链接:http://blog.csdn.net/hebbers/article/details/70332015 关键点在与
PdfReader的循环释放,定义局部变量后独立释放引用并不能完全释放,需要单独循环释放。
List<PdfReader> readerList = new List<PdfReader>();
foreach (var rd in readerList) { rd.Dispose(); }
时间: 2024-11-03 05:27:55