iTextSharp 合並PDF後,無法刪除已經合並的單個文件
阿新 • • 發佈:2017-05-16
page 關鍵點 fwrite 引用 foreach ntb span read var
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();
}
iTextSharp 合並PDF後,無法刪除已經合並的單個文件