Load with file type

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Redaction MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-redaction -- dnx GroupDocs.Redaction.Mcp --yes
Folders, licensing and more →

When you open a document from a stream without a file name, or when the file extension does not match the actual format, pass the expected format through LoadOptions (LoadOptions(FileType) or setFileType). GroupDocs.Redaction skips automatic format detection and uses the type you specify.

Set the file type to any supported constant from the FileType class, for example FileType.getDOCX() or FileType.getPDF(). The default value is FileType.getUnknown(), which keeps the existing detection behavior.

The following example demonstrates how to load a document from a stream and from a file path while explicitly specifying the file type:

final FileInputStream stream = new FileInputStream("sample.docx");
try
{
    final Redactor redactor = new Redactor(stream, new LoadOptions(FileType.getDOCX()));
    try
    {
        redactor.apply(new DeleteAnnotationRedaction());
        final FileOutputStream outputStream = new FileOutputStream("redacted-sample.pdf");
        try
        {
            RasterizationOptions options = new RasterizationOptions();
            options.setEnabled(true);
            redactor.save(outputStream, options);
        }
        finally { outputStream.close(); }
    }
    finally { redactor.close(); }
}
finally { stream.close(); }

final Redactor redactor = new Redactor("LoremIpsum.pdf", new LoadOptions(FileType.getPDF()));
try
{
    redactor.apply(new ExactPhraseRedaction("Lorem", new ReplacementOptions(java.awt.Color.BLACK)));
    SaveOptions saveOptions = new SaveOptions();
    saveOptions.setAddSuffix(true);
    saveOptions.setRasterizeToPDF(false);
    saveOptions.setRedactedFileSuffix("redacted");
    redactor.save(saveOptions);
}
finally { redactor.close(); }

sample.docx is the sample file used in this example. Click here to download it.

Binary file (PDF, 2.8 MB)

Download full output

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document redaction App

Along with full featured Java library we provide simple, but powerful free Apps.

You are welcome to perform redactions for various document formats like PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Redaction App.