Related blog post: Word Search and Replace Text in Word DOC/DOCX Documents using Java
Last active
February 19, 2022 10:33
-
-
Save GroupDocsGists/567c29bb3a59ed182ff727aac4c24742 to your computer and use it in GitHub Desktop.
Find and Replace Text, Phrase, Words in Word Documents using Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Find text and hide it by drawing rectangle over it using Java | |
final Redactor redactor = new Redactor("path/document.docx"); | |
redactor.apply(new ExactPhraseRedaction("John Doe", true, new ReplacementOptions(java.awt.Color.BLACK))); | |
redactor.save(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Find exact phrase and replace it with some other text using Java | |
final Redactor redactor = new Redactor("path/document.docx"); | |
redactor.apply(new ExactPhraseRedaction("John Doe", new ReplacementOptions("[censored]"))); | |
// If you want to save the redacted file at different location with diferent name. | |
FileOutputStream stream = new FileOutputStream("path/exactPhrase.docx"); | |
RasterizationOptions rasterOptions = new RasterizationOptions(); | |
rasterOptions.setEnabled(false); | |
redactor.save(stream, rasterOptions); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Find exact phrase (case-sensitive) and replace it with some other text using Java | |
final Redactor redactor = new Redactor("path/document.docx"); | |
redactor.apply(new ExactPhraseRedaction("John Doe", true /*isCaseSensitive*/, new ReplacementOptions("[censored]"))); | |
redactor.save(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Find text using regular expression and replace it with some other text using Java | |
final Redactor redactor = new Redactor("path/document.docx"); | |
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