Search code examples
javaannotationscommentshighlightpdfbox

How to delete annotations in PDF file using PDFBox


I would like to remove all the existing annotations in the PDF file. I could not find any direct method or API in PDFBox Annotaions API. Please provide any pointers to resolve the issue.

Thanks in advance for your help.


Solution

  • Assuming that you have a PDPage object, just do this:

    pdPage.setAnnotations(null);
    

    Here's the full code for the 1.8.* versions of PDFBox:

    PDDocument document = PDDocument.loadNonSeq(new File(pdfFilename), null);
    List<PDPage> pdPages = document.getDocumentCatalog().getAllPages();
    for (PDPage pdPage : pdPages)
    { 
        pdPage.setAnnotations(null);
    }
    document.save(new File(...));
    document.close();