This repository contains runnable code examples demonstrating how to use GroupDocs.Redaction for Java to redact sensitive information from documents, spreadsheets, presentations, PDFs, images, and more.
NOTE: These examples require a valid license to run properly. Get a temporary license at: https://purchase.groupdocs.com/temp-license/100510
- Java JDK 8 or later (17 LTS recommended)
- Maven 3.6 or later
- Java Environment: Set
JAVA_HOMEand add toPATH
Windows PowerShell:
$env:JAVA_HOME="C:\Program Files\Java\jdk-17"
$env:Path="$env:JAVA_HOME\bin;$env:Path"Linux/macOS:
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
export PATH=$JAVA_HOME/bin:$PATHFor detailed installation instructions and platform-specific notes, refer to the documentation:
Navigate to the Examples directory and install dependencies:
cd Examples
mvn clean installThis downloads groupdocs-redaction and other dependencies referenced in pom.xml.
Execute all examples using the test runner:
cd Examples
mvn compile exec:javaOutputs are saved to target/Output/<example-name>/.
You can also run examples directly using your IDE (IntelliJ IDEA, Eclipse, VS Code) by executing the main method or run() static method in each example class.
Examples are organized into three categories:
Minimal examples to get started:
HelloWorld.java- Basic document redaction exampleSetLicenseFromFile.java- License setup from fileSetLicenseFromStream.java- License setup from streamSetMeteredLicense.java- Metered license setup
Core redaction functionality examples:
ApplyRedaction.java- Apply a single redaction to a documentApplyMultipleRedactions.java- Apply multiple redactions at once
UseExactPhraseRedaction.java- Redact exact phrase matchesUseExactPhraseCaseSensitive.java- Case-sensitive phrase redactionUseExactPhraseRightToLeft.java- Right-to-left text redactionUseRegularExpression.java- Redact using regular expressionsUseRegexForParagraph.java- Redact entire paragraphs with regexUseColoredRectangle.java- Apply colored rectangle redactions
CleanMetadata.java- Remove all metadata from documentsCleanMetadataWithFilter.java- Remove metadata with filteringRedactMetadata.java- Redact specific metadata entriesRedactMetadataWithFilter.java- Redact metadata with filtering
RemoveAllAnnotations.java- Remove all annotations from documentsRemoveAnnotations.java- Remove specific annotationsRedactAnnotations.java- Redact annotation content
RedactImageArea.java- Redact specific areas in imagesCleanImageMetadada.java- Clean image metadata (EXIF, etc.)RedactEmbeddedImages.java- Redact images embedded in documents
FilterBySpreadsheetAndColumn.java- Redact specific spreadsheet columns
RemovePageRange.java- Remove a range of pagesRemoveLastPage.java- Remove the last pageRemoveFrameFromImage.java- Remove frames from animated images
GetSupportedFileFormats.java- List all supported file formatsGetFileInfoForAFileFromLocalDisk.java- Get document properties (file path)GetFileInfoForAFileFromStream.java- Get document properties (stream)GetDocumentPagePreview.java- Generate document page previews
Advanced scenarios organized by feature:
LoadFromLocalDisc.java- Load documents with custom optionsLoadFromStream.java- Load documents from streamsLoadPasswordProtectedFile.java- Load password-protected documentsPreRasterize.java- Pre-rasterize documents before redactionUseAdvancedLogging.java- Configure advanced logging options
SaveInOriginalFormat.java- Save redacted document in original formatSaveInRasterizedPDF.java- Save as rasterized PDFSaveOverwritingOriginalFile.java- Overwrite original fileSaveToStream.java- Save redacted document to streamSaveWithDefaultOptions.java- Save with default optionsSelectSpecificPagesForRasterizedPDF.java- Select pages for rasterizationUseAdvancedRasterizationOptions.java- Advanced rasterization settingsUseBorderRasterizationOption.java- Add borders during rasterizationUseGrayscaleRasterizationOption.java- Convert to grayscaleUseTiltRasterizationOption.java- Apply tilt effect
UseMicrosoftAzureComputerVision.java- Use Microsoft Azure OCRUseAsposeOCRForCloud.java- Use Aspose Cloud OCR
UsePdfRedactionFilters.java- Apply PDF-specific redaction filtersUsePageAreaRedaction.java- Redact specific page areas
CreateCustomFormatHandler.java- Create custom format handlersCreateRedactionPolicy.java- Create redaction policiesUseRedactionPolicy.java- Apply redaction policiesCreatePDFWithImageRedaction.java- Create PDFs with image redactionsExtendSupportedExtensionsList.java- Extend supported file extensionsUseRedactionCallback.java- Use redaction callbacks
All code snippets require the following imports:
import com.groupdocs.redaction.Redactor;
import com.groupdocs.redaction.examples.java.SampleFiles;
// ... other imports as needed// Examples/src/main/java/com/groupdocs/redaction/examples/java/basic_usage/redaction_basics/ApplyRedaction.java
import com.groupdocs.redaction.Redactor;
import com.groupdocs.redaction.examples.java.SampleFiles;
import com.groupdocs.redaction.redactions.ExactPhraseRedaction;
import com.groupdocs.redaction.redactions.ReplacementOptions;
final Redactor redactor = new Redactor(SampleFiles.SAMPLE_DOCX);
try {
redactor.apply(new ExactPhraseRedaction("John Doe",
new ReplacementOptions("[personal]")));
redactor.save();
} finally {
redactor.close();
}// Examples/src/main/java/com/groupdocs/redaction/examples/java/basic_usage/redaction_basics/ApplyMultipleRedactions.java
final Redactor redactor = new Redactor(SampleFiles.SAMPLE_DOCX);
try {
RedactionStatus result = redactor.apply(
new ExactPhraseRedaction("John Doe", new ReplacementOptions("[personal]")),
new ExactPhraseRedaction("Jane Doe", new ReplacementOptions("[personal]"))
);
redactor.save();
} finally {
redactor.close();
}// Examples/src/main/java/com/groupdocs/redaction/examples/java/basic_usage/text_redactions/UseRegularExpression.java
final Redactor redactor = new Redactor(SampleFiles.SAMPLE_DOCX);
try {
redactor.apply(new RegexRedaction("\\d{2}\\s*\\d{2}[^\\w]*\\d{6}",
new ReplacementOptions("[redacted]")));
redactor.save();
} finally {
redactor.close();
}// Examples/src/main/java/com/groupdocs/redaction/examples/java/basic_usage/metadata_redactions/RedactMetadata.java
final Redactor redactor = new Redactor(SampleFiles.SAMPLE_DOCX);
try {
redactor.apply(new MetadataRedaction(MetadataFilters.Author,
new ReplacementOptions("[redacted]")));
redactor.save();
} finally {
redactor.close();
}// Examples/src/main/java/com/groupdocs/redaction/examples/java/advanced_usage/loading_documents/LoadPasswordProtectedFile.java
LoadOptions loadOptions = new LoadOptions();
loadOptions.setPassword("1234");
final Redactor redactor = new Redactor(SampleFiles.PROTECTED_SAMPLE_DOCX, loadOptions);
try {
redactor.apply(new ExactPhraseRedaction("John Doe",
new ReplacementOptions("[personal]")));
redactor.save();
} finally {
redactor.close();
}// Examples/src/main/java/com/groupdocs/redaction/examples/java/advanced_usage/saving_documents/SaveInRasterizedPDF.java
final Redactor redactor = new Redactor(SampleFiles.SAMPLE_DOCX);
try {
redactor.apply(new ExactPhraseRedaction("John Doe",
new ReplacementOptions("[personal]")));
RasterizationOptions rasterOptions = new RasterizationOptions();
rasterOptions.setEnabled(true);
redactor.save(SampleFiles.OutputPath + "output.pdf", rasterOptions);
} finally {
redactor.close();
}// Examples/src/main/java/com/groupdocs/redaction/examples/java/advanced_usage/UseRedactionPolicy.java
RedactionPolicy policy = RedactionPolicy.load(SampleFiles.POLICY_FILE);
Redactor redactor = new Redactor(SampleFiles.POLICY_INBOUND + "/sample.docx");
try {
redactor.apply(policy);
redactor.save();
} finally {
redactor.close();
}All examples follow a consistent pattern:
- Create a
Redactorinstance with a document path or stream - Apply one or more redactions using
redactor.apply() - Save the redacted document using
redactor.save() - Close the redactor in a
finallyblock
Each example class contains a static run() method that can be called directly:
// Run all examples
mvn compile exec:java
// Or run specific example from code
com.groupdocs.redaction.examples.java.quick_start.HelloWorld.run();├── Examples/ # All example code
│ ├── src/main/java/com/groupdocs/
│ │ └── redaction/examples/java/
│ │ ├── basic_usage/ # Basic redaction examples
│ │ │ ├── annotation_redactions/
│ │ │ ├── image_redactions/
│ │ │ ├── metadata_redactions/
│ │ │ ├── redaction_basics/
│ │ │ ├── remove_page_redactions/
│ │ │ ├── spreadsheet_redactions/
│ │ │ └── text_redactions/
│ │ │
│ │ ├── advanced_usage/ # Advanced features
│ │ │ ├── loading_documents/
│ │ │ ├── saving_documents/
│ │ │ ├── using_ocr/
│ │ │ └── using_redaction_filters/
│ │ │
│ │ ├── quick_start/ # Quick start examples
│ │ │ ├── HelloWorld.java
│ │ │ ├── SetLicenseFromFile.java
│ │ │ ├── SetLicenseFromStream.java
│ │ │ └── SetMeteredLicense.java
│ │ │
│ │ ├── helper_classes/ # Helper utilities
│ │ ├── Constants.java # License configuration constants
│ │ ├── SampleFiles.java # File paths and path utilities
│ │ ├── ExampleTestRunner.java # Run all examples
│ │ └── MainClass.java # Main entry point
│ │
│ ├── Resources/
│ │ └── SampleFiles/ # Sample input files
│ │ ├── Doc/
│ │ ├── Pdf/
│ │ ├── Ppt/
│ │ ├── Xls/
│ │ ├── Image/
│ │ └── Bulk/
│ │
│ └── pom.xml # Maven configuration
│
└── README.md # This file
- Input Files:
Examples/Resources/SampleFiles/(configured inSampleFiles.java) - Output Files:
target/Output/<example-name>/(auto-created per example)
Modify SampleFiles.java to change paths if needed:
public static final String SamplesPath = "Resources/SampleFiles";
public static final String OutputPath = "target/Output/";Examples run in trial mode by default. To use a license:
Option 1: Update Constants.java
public static final String LicensePath = "C:\\licenses\\GroupDocs.Redaction.lic";Then use SetLicenseFromFile or SetLicenseFromStream examples.
Option 2: Set Metered License
Update Constants.java:
public static final String MeteredPublicKey = "Your Public Key";
public static final String MeteredPrivateKey = "Your Private Key";Then use the SetMeteredLicense example.
Get a temporary license: Get Temporary License
Learn more about licensing at: GroupDocs Licensing FAQ
Ensure Maven is properly installed:
mvn --versionIf dependencies fail to download, check your internet connection and Maven repository settings.
Ensure you're using Java 8 or later:
java -versionSet JAVA_HOME environment variable if needed.
Ensure write access to target/Output/ directory. The directory will be created automatically if it doesn't exist.
Ensure all sample files are present in Examples/Resources/SampleFiles/. If files are missing, examples may fail.
If you see license-related warnings, either:
- Set up a valid license file (see License Setup above)
- Or ignore warnings if running in trial mode (some features may be limited)
- Documentation: docs.groupdocs.com/redaction/java
- API Reference: apireference.groupdocs.com/redaction/java
- Support Forum: forum.groupdocs.com/c/redaction
- Product Page: products.groupdocs.com/redaction/java
- GitHub Repository: github.com/groupdocs-redaction/GroupDocs.Redaction-for-Java
Note: These are code examples demonstrating GroupDocs.Redaction features. For production use, ensure you have a valid license.