Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active March 11, 2022 11:46
Show Gist options
  • Save GroupDocsGists/1098cf3a7914313b466c3e01dc77704a to your computer and use it in GitHub Desktop.
Save GroupDocsGists/1098cf3a7914313b466c3e01dc77704a to your computer and use it in GitHub Desktop.
Find and Replace Text, Phrase & Words in PDF Documents using Java
// Find text in PDF and hide it by drawing rectangle over it using Java
final Redactor redactor = new Redactor("path/document.pdf");
redactor.apply(new ExactPhraseRedaction("John Doe", true, new ReplacementOptions(java.awt.Color.BLACK)));
redactor.save();
// Find exact phrase in PDF and replace it with some other text using Java
final Redactor redactor = new Redactor("path/document.pdf");
redactor.apply(new ExactPhraseRedaction("John Doe", new ReplacementOptions("[censored]")));
// Save the redacted file at different location with diferent name.
FileOutputStream stream = new FileOutputStream("path/exactPhrase.pdf");
RasterizationOptions rasterOptions = new RasterizationOptions();
rasterOptions.setEnabled(false);
redactor.save(stream, rasterOptions);
// Find exact phrase in PDF (case-sensitive) and replace it with some other text using Java
final Redactor redactor = new Redactor("path/document.pdf");
redactor.apply(new ExactPhraseRedaction("John Doe", true /*isCaseSensitive*/, new ReplacementOptions("[censored]")));
redactor.save();
// Find text in PDF using regular expression and replace it with some other text using Java
final Redactor redactor = new Redactor("path/document.pdf");
redactor.apply(new RegexRedaction("\\d{2}\\s*\\d{2}[^\\d]*\\d{6}", new ReplacementOptions("[censored]")));
redactor.save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment