redact-word-docs-before-llm-java is a runnable Java demo that redacts personal names, masks digit-pattern PII, and erases metadata from Word DOCX files before LLM or RAG ingestion. This repository provides production-oriented code examples for that hygiene pass using GroupDocs.Redaction for Java. Built with Maven (compiler target 11; runtime JDK 8+), the samples open a DOCX with Redactor, apply ExactPhraseRedaction, RegexRedaction, and EraseMetadataRedaction, then save an editable Word file with rasterization disabled. Designed for backend and data-pipeline developers who must scrub intake documents before chat or RAG indexing.
Use this demo when a Word DOCX will leave your trust boundary and enter an LLM or RAG pipeline. Keep fixed names as exemption tokens, cover digit patterns with a solid color box, and wipe document metadata in one apply call. I built this when I needed to strip names and ID-like digit runs from intake DOCX files before a RAG indexer consumed them. Prefer these samples when you need editable DOCX output rather than a rasterized PDF, and when you want the exact three redaction types shown here.
- Platform: Java (JDK 8+)
- Product: GroupDocs.Redaction for Java 26.11
- Language: Java
- Build: Apache Maven (exec-maven-plugin)
Developers who feed Word DOCX files into LLM chat or RAG indexes often need to remove personal names, national-ID-style digit runs, and embedded metadata first. Doing that with ad-hoc string replace leaves body text inconsistent, skips headers and footers, and never touches Author or custom properties. GroupDocs.Redaction for Java supplies typed redactions (ExactPhraseRedaction, RegexRedaction, EraseMetadataRedaction) and a single Redactor.apply path that writes a clean, still-editable DOCX suitable for downstream ingestion.
GroupDocs.Redaction for Java addresses these challenges through a stream-friendly Redactor that applies one or more Redaction instances and saves with RasterizationOptions disabled so the result stays DOCX. Key technical features include:
- ExactPhraseRedaction: Replaces a fixed personal name with a string token via
ReplacementOptions. - RegexRedaction: Masks digit patterns that match a PII-style regular expression with a color box.
- EraseMetadataRedaction: Clears metadata using
MetadataFilters.Allbefore export. - Batch apply: Passes a
Redaction[]so name, regex, and metadata steps run in one call. - Editable save: Disables rasterization so the RAG or chat pipeline receives DOCX, not PDF images.
Before running these examples, ensure you have:
- JDK - JDK 8 or later (11/17 LTS recommended)
- Build tool - Apache Maven 3.6+
- License - evaluation mode works; use a temporary license to remove evaluation marks
- Sample input -
documents/sample.docxincluded in the project
Using Package Manager:
mvn dependency:resolveManual Installation:
Add the GroupDocs Java repository (https://releases.groupdocs.com/java/repo/) and the com.groupdocs:groupdocs-redaction dependency (version 26.11) to your pom.xml, then run mvn compile.
- Set
licensePathinapplyLicense()to your.licfile, or leaveREPLACE_WITH_YOUR_LICENSE_PATHfor evaluation mode. - Place the source Word file at
documents/sample.docx(already present). - Run
mvn exec:javafrom the project root; outputs land underResult/.
redact-word-docs-before-llm-java/
│
├── pom.xml
├── documents/
│ └── sample.docx
└── src/
└── main/
└── java/
└── com/
└── groupdocs/
└── demo/
└── RedactBeforeLlmDemo.java
- pom.xml - Maven build file with GroupDocs.Redaction 26.11 and exec plugin.
- documents/sample.docx - Sample Word intake document used by all three methods.
- RedactBeforeLlmDemo.java - Entry point plus
redactExactPhrase,redactPiiWithRegex, andredactBeforeLlmPipeline.
Opens the source with Redactor, applies ExactPhraseRedaction with a string ReplacementOptions value, and writes an editable DOCX via a stream save with rasterization disabled. Use this when a known person name must become a stable token such as [NAME] before the file is chunked for RAG.
final Redactor redactor = new Redactor(sourcePath);
try {
RedactorChangeLog result = redactor.apply(
new ExactPhraseRedaction(
phrase, new ReplacementOptions(replacement)));
if (result.getStatus() != RedactionStatus.Failed) {
RasterizationOptions rasterOpts = new RasterizationOptions();
rasterOpts.setEnabled(false);
FileOutputStream output = new FileOutputStream(outputPath);
try {
redactor.save(output, rasterOpts);
} finally {
output.close();
}
}
return result.getStatus();
} finally {
redactor.close();
}ExactPhraseRedaction matches the literal phrase across the document body and replaces each hit with the string from ReplacementOptions. Status is read from RedactorChangeLog; on success the code saves through a FileOutputStream with RasterizationOptions.setEnabled(false) so the file remains DOCX.
Key Components:
Redactor: Opens and owns the document lifetimeExactPhraseRedaction: Literal name match and replaceReplacementOptions: Holds the exemption token string
Parameters:
phrase: Fixed personal name to locate (for exampleJohn Doe)replacement: Token written in place (for example[NAME])
Output: Result/llm-exact-phrase.docx with the name replaced by the token.
Applies RegexRedaction with a Color ReplacementOptions value so matched digit patterns are permanently masked before the document is handed to an LLM pipeline. The sample pattern targets sequences that look like spaced national-ID style digit runs.
final Redactor redactor = new Redactor(sourcePath);
try {
RedactorChangeLog result = redactor.apply(
new RegexRedaction(
pattern, new ReplacementOptions(boxColor)));
if (result.getStatus() != RedactionStatus.Failed) {
RasterizationOptions rasterOpts = new RasterizationOptions();
rasterOpts.setEnabled(false);
FileOutputStream output = new FileOutputStream(outputPath);
try {
redactor.save(output, rasterOpts);
} finally {
output.close();
}
}
return result.getStatus();
} finally {
redactor.close();
}RegexRedaction compiles the supplied pattern and paints each match with the given Color (here Color.BLACK). Unlike string replacement, the match is covered so the original digits cannot be recovered from the body text that an LLM would read.
Key Components:
RegexRedaction: Pattern-based text matchReplacementOptions(Color): Solid-box cover for matchesRedactorChangeLog: Reports apply status
Parameters:
pattern: PII-style regex such as\d{2}\s*\d{2}[^\d]*\d{6}boxColor: Cover color (Color.BLACKin the demo)
Output: Result/llm-regex-pii.docx with digit runs blacked out.
Implementation: Runs the full pre-LLM hygiene pass: name redaction, digit-pattern masking, and metadata erase
Builds a Redaction array with ExactPhraseRedaction, RegexRedaction, and EraseMetadataRedaction(MetadataFilters.All), applies them in one call, and saves an editable DOCX suitable for RAG or chat ingestion. This is the path most intake services should call in production.
Redaction[] redactions = new Redaction[] {
new ExactPhraseRedaction(
personName, new ReplacementOptions("[NAME]")),
new RegexRedaction(
"\\d{2}\\s*\\d{2}[^\\d]*\\d{6}",
new ReplacementOptions(Color.BLACK)),
new EraseMetadataRedaction(MetadataFilters.All)
};
RedactorChangeLog result = redactor.apply(redactions);
if (result.getStatus() != RedactionStatus.Failed) {
RasterizationOptions rasterOpts = new RasterizationOptions();
rasterOpts.setEnabled(false);
FileOutputStream output = new FileOutputStream(outputPath);
try {
redactor.save(output, rasterOpts);
} finally {
output.close();
}
}One apply call runs all three redactions against the same open document, then the stream save keeps DOCX structure. Metadata wipe with MetadataFilters.All removes Author and other properties that often leak identity even after body text is cleaned.
Key Components:
Redaction[]: Batch of phrase, regex, and metadata redactionsEraseMetadataRedaction: Clears properties viaMetadataFilters.AllRasterizationOptions: Kept disabled for editable DOCX output
Parameters:
personName: Name passed intoExactPhraseRedactionsourcePath/outputPath: Input DOCX andResult/llm-ready.docx
Output: Result/llm-ready.docx ready for LLM or RAG ingestion.
When implementing redact documents before LLM ingestion, consider these best practices:
- Close the Redactor: Always use try/finally so
redactor.close()runs after apply and save. - Check status: Treat
RedactionStatus.Failedas a hard stop before uploading to any model endpoint. - Keep DOCX editable: Disable rasterization when the next stage needs text extraction, not page images.
- Tune the regex: Validate digit patterns against real intake samples so you do not over-mask.
- Erase metadata always: Call
EraseMetadataRedaction(MetadataFilters.All)even when body redaction looks complete. - License for batch jobs: Set a temporary or purchased license before high-volume pipeline runs.
For more in-depth information about redacting Word documents before LLM ingestion, explore these technical resources:
-
Step-by-step use case guide in the documentation - Architecture notes for the three redaction paths shown in this repo: Read the article ->
-
In-depth blog article about this project - Business and pipeline context for pre-LLM DOCX hygiene in Java: Read the article ->
-
Text redactions in GroupDocs.Redaction for Java - Exact phrase and regex redaction API details: Read the article ->
-
How to Redact a Word Document on Windows with GroupDocs.Redaction for .NET - Related .NET Word redaction walkthrough: Read the article ->
-
Word Search and Replace Text in Word Documents using Java - Phrase and regex replace patterns for DOCX in Java: Read the article ->
-
Redact PDF Scanned Documents in Java - Text and image cover techniques for PDF intake: Read the article ->
GroupDocs.Redaction, Java, ExactPhraseRedaction, RegexRedaction, EraseMetadataRedaction, DOCX, PII redaction, LLM ingestion, RAG pipeline, metadata erase, ReplacementOptions, Redactor, Word redaction, pre-LLM hygiene, digit pattern masking
For technical support, visit: