Specify file type for comparison manually

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.Comparison 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-comparison -- dnx GroupDocs.Comparison.Mcp --yes
Folders, licensing and more →

GroupDocs.Comparison lets you specify the document file type explicitly via LoadOptions.setFileType() instead of relying on automatic format detection.

Why specify the file type

When no file type is provided, GroupDocs.Comparison inspects the file’s contents to determine its format. This detection is reliable but adds processing time, which can be noticeable for large files.

If you already know the format — for example, because the input is constrained by your application — passing it explicitly skips detection and lets the library go straight to loading. For high-throughput pipelines or batch jobs over large documents, this can produce a measurable speedup.

Specify the file type explicitly

Use this approach when the document format is known at compile time.

import com.groupdocs.comparison.Comparer;
import com.groupdocs.comparison.options.load.LoadOptions;
import com.groupdocs.comparison.result.FileType;
// ...

LoadOptions loadOptions = new LoadOptions();
loadOptions.setFileType(FileType.DOCX);

try (Comparer comparer = new Comparer("source.docx", loadOptions)) {
    comparer.add("target.docx", loadOptions);
    comparer.compare("result.docx");
}

LoadOptions also has a constructor that takes the file type directly: new LoadOptions(FileType.DOCX).

Derive the file type from a file path

When the file path is available and its extension is reliable, use FileType.fromFileNameOrExtension() to resolve the FileType from the extension. This still skips content-based auto-detection, but keeps the calling code generic.

import com.groupdocs.comparison.Comparer;
import com.groupdocs.comparison.options.load.LoadOptions;
import com.groupdocs.comparison.result.FileType;
// ...

String sourcePath = "source.docx";
String targetPath = "target.docx";

LoadOptions sourceLoadOptions = new LoadOptions();
sourceLoadOptions.setFileType(FileType.fromFileNameOrExtension(sourcePath));

LoadOptions targetLoadOptions = new LoadOptions();
targetLoadOptions.setFileType(FileType.fromFileNameOrExtension(targetPath));

try (Comparer comparer = new Comparer(sourcePath, sourceLoadOptions)) {
    comparer.add(targetPath, targetLoadOptions);
    comparer.compare("result.docx");
}

See also