Remove all recognized metadata properties from a file
Sometimes you may need to just remove all or clean metadata properties without applying any filters. The best way to do this is to use the sanitize method.
This example demonstrates how to remove all detected metadata packages/properties.
// Constants.InputPdf is an absolute or relative path to your document. Ex: @"C:\Docs\source.pdf"try(Metadatametadata=newMetadata(Constants.InputPdf)){// Remove detected metadata packagesintaffected=metadata.sanitize();System.out.println(String.format("Properties removed: %s",affected));metadata.save(Constants.OutputPdf);}
As a result, we get a sanitized version of the original file.
Note
Since version 26.11, sanitize() also removes annotations, attachments, bookmarks, form fields and digital signatures from PDF documents. Before 26.11 it removed annotations only. If a PDF has to keep these items, do not call sanitize() on it; remove only the properties you need instead. See 2 PDF metadata cleanup methods for Java for a comparison.
More resources
Advanced usage topics
To learn more about library features and get familiar how to manage metadata and more, please refer to theadvanced usage section.
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples: