Remove All Content on a PDF Page Using the PDFNet SDK

Question: How do I remove all content on a PDF page.

Answer:
Normally if you need to remove some content from individual pages you would need to cycle through all elements on the page, and use only the ones you want to retain and then write them to the page when saving the output document. If you want to remove all comment on the page a simpler method would be to remove the page, and then add a new blank page if you desire.

try
{
    using (PDFDoc pdf_doc = new PDFDoc(input_file))
    {
        pdf_doc.InitSecurityHandler();

        // Check if the page to be remove exists.
        if (pdf_doc.GetPageCount() >= PageToRemove) 
        {
            // Get the page iterator for the page to be removed
            PageIterator pdf_page_iterator = pdf_doc.GetPageIterator(PageToRemove);

            // Remove the page
            pdf_doc.PageRemove(pdf_page_iterator);

            // Replace it with a new page - or don't add a new page if you want to remove it.               
            pdf_doc.PageInsert(pdf_page_iterator, pdf_doc.PageCreate());

            // The removed page and its resources are still available until
            // the document is saved in 'full save mode' with the
            // 'remove unused objects' flag. If you are saving the file in
            // 'incremental mode', the serialized document may contain the
            // content of the removed page.
            pdf_doc.Save(output_file, pdftron.SDF.SDFDoc.SaveOptions.e_remove_unused);
        }
        else
        {
            Console.WriteLine($"Page {PageToRemove} not found.");
        }
    }
}
catch (PDFNetException e)
{
    Console.WriteLine(e.Message);
}

You can find descriptions of the API calls used in this sample in our online documentation here:
https://sdk.apryse.com/api/PDFTronSDK/dotnet/index.html

  • This example was done using our .NET API but we support a wide range of languages and platforms.
1 Like