Removing Content from a PDF Page Using the PDFNet SDK

Question: How do I remove content from a PDF page.

Answer:
You can use the ElementReader and ElementWriter classes to cycle through the page content and add only what you want to retain to the output file. Here is an example that will only retain the images on a specific page, you can select other types to retain based on Element.Type.

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

        // Check if the page to be remove exists.
        if (pdf_doc.GetPageCount() >= PageToRemove)
        {
            Page page_to_remove_content = pdf_doc.GetPage(PageToRemove);                    

            using (ElementReader element_reader = new ElementReader())
            {
                element_reader.Begin(page_to_remove_content);

                using (ElementWriter element_writer = new ElementWriter())
                {
                     element_writer.Begin(
                          page_to_remove_content,
                         ElementWriter.WriteMode.e_replacement, false);

                    Element element = element_reader.Next();
                    while (element != null)
                     {
                          // Here you can decide what elements to retain.
                          if (element.GetType() == Element.Type.e_image)
                          {
                                element_writer.WriteElement(element);
                          }

                          element = element_reader.Next();
                    }

                    element_writer.End();
                }

                 element_reader.End();
            }

             pdf_doc.Save(output_file, pdftron.SDF.SDFDoc.SaveOptions.e_remove_unused);
         }
    }
}
catch (PDFNetException e)
{
    Console.WriteLine(e.Message);
}

If you need to remove all content on a page, or remove one, check out sample on replacing a PDF page.

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