原文鏈接:http://blog.csdn.net/hebbers/article/details/70332015 關鍵點在與 ...
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();
}