Sometimes it is necessary to detect the format of a document file before opening because the file extension does not guarantee that the file content is appropriate.
For example, a document maybe saved with the wrong extension or no extension at all. Therefore, if you are not sure what the actual content of the file is and want to avoid throwing an exception, you can use the FileFormatUtil.DetectFileFormat method. This is a static method that accepts either a file name or stream object that contains the file data. The method returns a FileFormatInfo object that contains the detected information about the file type.
Example
Shows how to use the FileFormatUtil class to detect the document format and other features of the document.
[Java]
FileFormatInfo info = FileFormatUtil.detectFileFormat(getMyDir() + "Document.doc");
System.out.println("The document format is: " + FileFormatUtil.loadFormatToExtension(info.getLoadFormat()));
System.out.println("Document is encrypted: " + info.isEncrypted());
System.out.println("Document has a digital signature: " + info.hasDigitalSignature());