GroupDocs.Redaction for Java lets you redact sensitive text in Word documents and other supported file formats on Windows. This tutorial demonstrates how to create a Maven console application, find a phrase in a Word document using a regular expression, replace the matching text, and save the redacted document. The same approach can be applied to other supported document formats.
Create a Java console project on Windows
Start by creating a Maven project that targets JDK 8 or later (JDK 17 LTS is recommended). See System requirements for supported Windows versions and Java runtimes.
Add the GroupDocs repository and GroupDocs.Redaction for Java 26.11 to pom.xml. This package provides the APIs required to load documents, find text using regular expressions, apply redactions, and save the results.
See Installation for Gradle and other build tools.
The package provides the Redactor class for loading and saving documents, RegexRedaction for applying redactions based on regular-expression matches, and License.isLicensed() for checking whether a valid license is applied.
Add the Word file
Place the Word document in the project directory and name it document.docx. The sample document contains the phrase essential tool. The application finds this phrase, replaces the matching text, and creates a redacted output document. The original Word document remains unchanged.
Add the redaction code
Replace the contents of App.java with the following code:
importcom.groupdocs.redaction.License;importcom.groupdocs.redaction.Redactor;importcom.groupdocs.redaction.options.SaveOptions;importcom.groupdocs.redaction.redactions.RegexRedaction;importcom.groupdocs.redaction.redactions.ReplacementOptions;publicclassApp{publicstaticvoidmain(String[]args)throwsException{Licenselicense=newLicense();// license.setLicense("your_license_path");
if(!license.isLicensed()){System.out.println("The library is running in evaluation mode. Some limitations may apply.");}finalRedactorredactor=newRedactor("document.docx");try{redactor.apply(newRegexRedaction("\\s+essential tool",newReplacementOptions(" [redacted]")));SaveOptionssaveOptions=newSaveOptions();saveOptions.setAddSuffix(true);saveOptions.setRasterizeToPDF(false);redactor.save(saveOptions);}finally{redactor.close();}}}
document.docx is the sample file used in this example. Click here to download it.
License.isLicensed() returns false until setLicense loads a valid license. Uncomment setLicense and provide the path to your license file to run the application with a valid license. Without a license, a single process can open up to 25 documents. Saved documents contain a trial badge at the top of each page. For more information, see Evaluation limitations and licensing.
The regular expression \s+essential tool matches one or more whitespace characters followed by essential tool. The search is case-insensitive by default, so Essential Tool also matches. Each match, including the whitespace before the phrase, is replaced with [redacted].
Or compile and run with your IDE. The application reads document.docx from the working directory and saves the redacted document as document_Redacted.docx alongside it.
Check the redacted document
Open document_Redacted.docx to verify the result. The phrase essential tool should be replaced with [redacted]. The original document.docx remains unchanged.
You can adapt this example to redact other phrases, patterns, or sensitive text in Word documents by changing the regular expression and replacement text.
Use the Free Online Document Redaction App to find and redact text and other sensitive content in PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, email files, and other supported formats.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Ask AI
Ask anything about this page — the answer is based on its content.
Searching the docs and writing an answer…
Something went wrong while getting an answer. Please try again in a moment.