We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose.PDF for .NET/ PDF Deleting a page will throw exceptions

Hi Aspose team,

We recently encountered a problem while using Aspose.Pdf. We have a test file. If we try to delete pages, then save file, then delete, then save, repeat these operations several times, an exception will be thrown.

We took a screenshot of the thrown exceptions.
1629969281783_CB653D3B-528A-4220-A42E-F765DDA73EC4.png (38.1 KB)
exception2.png (46.8 KB)

This is the sample code we wrote, which simplifies the original code:

var doc = new Aspose.Pdf.Document(@"C:\Users\User\Desktop\TestFile.pdf");
while (true)
{
    var pages = new int[] { 1 };
    string newPath = @"C:\Users\User\Desktop\New\" + doc.Pages.Count + ".pdf";
    if (doc.Pages.Count > 1)
    {
        doc.Pages.Delete(pages);
        doc.Save(newPath);
    }
    else
    {
        break;
    }
}

But if add a line “doc = new Aspose.Pdf.Document(newPath);” after “doc.Save(newPath);” then no exception will be thrown.

This is our test file:
TestFile.pdf (2.2 MB)

Please take a look, thank you.

@Glority_Developer

A ticket with ID PDFNET-50442 has been created in our issue tracking system to further investigate the issue on our end. This thread has been linked with the issue so that you may be notified once the issue will be fixed.