Home > Enterprise >  Strange problem with itext 7 page removal C#
Strange problem with itext 7 page removal C#

Time:11-05

I am removing pages from PDF. I have the list of page numbers what I would like to delete. So I am doing it that way.

  pdfDoc = new PdfDocument(new PdfReader(arrAllFiles[docNumber]), new PdfWriter(newpdfPathWithfilename));
             

                foreach (var pageNumber in BlankPagesList)
                {
                    pdfDoc.RemovePage(pageNumber);
                }

When it removes first page in the list, then all pages after this removed page will get page numbers decreased by one. So on the second round it will delete wrong page, and so on. Is there some elegant way to do it right, without decreasing page number by the count of the cycle?

CodePudding user response:

If you sort the elements of BlankPagesList in decreasing order before entering that foreach loop, you will prevent that problem.

  • Related