Delete multiple pages from pdf file

using the following code produces a null exception error:

pdfFile.delete(5);

pdfFile.delete(6);

pdfFile.save("pdfFile.pdf");

however,

pdfFile.delete(5);

pdfFile.save("pdfFile.pdf");--works and saves file as intended

pdfFile.delete(6);

pdfFile.save("pdfFile.pdf"); --Fails here

have also tried copying page collection to new pdf this also fails as it seems that the page is still there it is now set to null.

this process seems like it should be simple but i can't seem to find a way around it. any suggestions?

Hi Jim,

Thanks for using our products.

Kindly use below mentioned code for deleting multiple pages from PDF Document.

Document pdfFile = new Document("adobe.pdf");
pdfFile.Pages.Delete(5);
pdfFile.Pages.Delete(6);
pdfFile.Save("adobe.pdf");
pdfFile.Pages.Delete(5);
pdfFile.Save("adobe.pdf");
pdfFile.Pages.Delete(6);
pdfFile.Save("adobe.pdf");

You may check Delete a Particular Page from the PDF File for more detail.

Please feel free to contact us in case of any issue.

Thanks & Regards,

the code you posted, which looks identical to the code i posted(minus the “pages” reference which is in my code, i ommitted it from my post for some unknown reason), does not work. i am getting a null reference exception… still stuck. it appears to be deleting the page, but not removing the index reference, so when it saves it hits a null page and throws the error.

Hi Jim,

Please share the template PDF Document with us. This will help us to figure out the issue and reply back to you soon.

We apologize for your inconvenience.

Thanks & Regards,