Specify file type when loading a document

GroupDocs.Signature supports specifying the file type when loading a document. When the file type is not specified, GroupDocs.Signature detects it from the document content, which takes some time. Starting with GroupDocs.Signature for Java 26.10, you can specify the file type directly in LoadOptions, and GroupDocs.Signature skips the detection and uses the specified type right away.

The following are the steps to specify the file type when loading a document.

  • Instantiate the LoadOptions object and set the file type, either with the LoadOptions(FileType) constructor or with the setFileType method;
  • Create an InputStream for the document file, for example new FileInputStream(filePath);
  • Create Signature object by passing the document stream and load options to its constructor;
  • Call the sign method to sign the document and save the signed version to the output path.

The following code sample shows how to specify file type when loading a document.

try (InputStream stream = new FileInputStream(filePath))
{
    // Specify the file type (PDF) while loading the document
    LoadOptions loadOptions = new LoadOptions(FileType.PDF);

    // Initialize the Signature instance with the file stream and load options
    Signature signature = new Signature(stream, loadOptions);
    try
    {
        QrCodeSignOptions options = new QrCodeSignOptions("JohnSmith");
        options.setEncodeType(QrCodeTypes.QR);
        options.setLeft(100);
        options.setTop(100);

        // sign document to file
        signature.sign(outputFilePath, options);
    }
    finally
    {
        signature.dispose();
    }
}
System.out.println("\nSource document signed successfully.\nFile saved at " + outputFilePath);

The same load options work with a file path: new Signature(filePath, new LoadOptions(FileType.PDF)). FileType.Unknown, the default, detects the file type from the document content.

More resources

GitHub Examples

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

Free Online Apps

Along with the full-featured Java library, we provide simple but powerful free online apps.
To sign PDF, Word, Excel, PowerPoint, and other documents you can use the online apps from the GroupDocs.Signature App Product Family.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.