After iTextSharp merges PDF files, you cannot delete a single merged file,

Source: Internet
Author: User

After iTextSharp merges PDF files, you cannot delete a single merged file,

Private void MergePDFFiles (string [] fileList, string outMergeFile) {List <PdfReader> readerList = new List <PdfReader> (); // record merge PDF set iTextSharp. text. document document = new iTextSharp. text. document (iTextSharp. text. pageSize. a4.Rotate (); analyze writer = analyze writer. getInstance (document, new FileStream (outMergeFile, FileMode. create); document. open (); Response contentbyte cb = writer. directContent; PdfImpo RtedPage 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             }  

The key point of http://blog.csdn.net/hebbers/article/details/70332015 lies in

PdfReader is released cyclically. When a local variable is defined, the reference cannot be released independently and must be released cyclically.
  List<PdfReader> readerList = new List<PdfReader>(); 

    foreach (var rd in readerList)               {                 rd.Dispose();               }  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.