FileFormatUtil

Inheritance: java.lang.Object

public class FileFormatUtil

Provides utility methods for working with file formats, such as detecting file format or converting file extensions to/from file format enums.

To learn more, visit the Detect File Format and Check Format Compatibility documentation article.

Examples:

Shows how to detect encoding in an html file.


 FileFormatInfo info = FileFormatUtil.detectFileFormat(getMyDir() + "Document.html");

 Assert.assertEquals(LoadFormat.HTML, info.getLoadFormat());

 // The Encoding property is used only when we create a FileFormatInfo object for an html document.
 Assert.assertEquals("windows-1252", info.getEncoding().name());
 

Methods

MethodDescription
contentTypeToLoadFormat(String contentType)Converts IANA content type into a load format enumerated value.
contentTypeToSaveFormat(String contentType)Converts IANA content type into a save format enumerated value.
detectFileFormat(InputStream stream)
detectFileFormat(String fileName)Detects and returns the information about a format of a document.
extensionToSaveFormat(String extension)Converts a file name extension into a SaveFormat value.
imageTypeToExtension(int imageType)
loadFormatToExtension(int loadFormat)
loadFormatToSaveFormat(int loadFormat)
saveFormatToExtension(int saveFormat)
saveFormatToLoadFormat(int saveFormat)

contentTypeToLoadFormat(String contentType)

public static int contentTypeToLoadFormat(String contentType)

Converts IANA content type into a load format enumerated value.

Parameters:

ParameterTypeDescription
contentTypejava.lang.String

Returns: int

contentTypeToSaveFormat(String contentType)

public static int contentTypeToSaveFormat(String contentType)

Converts IANA content type into a save format enumerated value.

Parameters:

ParameterTypeDescription
contentTypejava.lang.String

Returns: int

detectFileFormat(InputStream stream)

public static FileFormatInfo detectFileFormat(InputStream stream)

Parameters:

ParameterTypeDescription
streamjava.io.InputStream

Returns: FileFormatInfo

detectFileFormat(String fileName)

public static FileFormatInfo detectFileFormat(String fileName)

Detects and returns the information about a format of a document. Detects and returns the information about a format of a document stored in a disk file.

Remarks:

Even if this method detects the document format, it does not guarantee that the specified document is valid. This method only detects the document format by reading data that is sufficient for detection. To fully verify that a document is valid you need to load the document into a Document object.

This method throws FileCorruptedException when the format is recognized, but the detection cannot complete because of corruption.

Examples:

Shows how to use the FileFormatUtil class to detect the document format and encryption.


 Document doc = new Document();

 // Configure a SaveOptions object to encrypt the document
 // with a password when we save it, and then save the document.
 OdtSaveOptions saveOptions = new OdtSaveOptions(SaveFormat.ODT);
 saveOptions.setPassword("MyPassword");

 doc.save(getArtifactsDir() + "File.DetectDocumentEncryption.odt", saveOptions);

 // Verify the file type of our document, and its encryption status.
 FileFormatInfo info = FileFormatUtil.detectFileFormat(getArtifactsDir() + "File.DetectDocumentEncryption.odt");

 Assert.assertEquals(".odt", FileFormatUtil.loadFormatToExtension(info.getLoadFormat()));
 Assert.assertTrue(info.isEncrypted());
 

Shows how to use the FileFormatUtil class to detect the document format and presence of digital signatures.


 // Use a FileFormatInfo instance to verify that a document is not digitally signed.
 FileFormatInfo info = FileFormatUtil.detectFileFormat(getMyDir() + "Document.docx");

 Assert.assertEquals(".docx", FileFormatUtil.loadFormatToExtension(info.getLoadFormat()));
 Assert.assertFalse(info.hasDigitalSignature());

 CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw", null);
 DigitalSignatureUtil.sign(getMyDir() + "Document.docx", getArtifactsDir() + "File.DetectDigitalSignatures.docx",
         certificateHolder);

 // Use a new FileFormatInstance to confirm that it is signed.
 info = FileFormatUtil.detectFileFormat(getArtifactsDir() + "File.DetectDigitalSignatures.docx");

 Assert.assertTrue(info.hasDigitalSignature());

 // We can load and access the signatures of a signed document in a collection like this.
 Assert.assertEquals(1, DigitalSignatureUtil.loadSignatures(getArtifactsDir() + "File.DetectDigitalSignatures.docx").getCount());
 

Parameters:

ParameterTypeDescription
fileNamejava.lang.StringThe file name.

Returns: FileFormatInfo - A FileFormatInfo object that contains the detected information.

extensionToSaveFormat(String extension)

public static int extensionToSaveFormat(String extension)

Converts a file name extension into a SaveFormat value.

Parameters:

ParameterTypeDescription
extensionjava.lang.StringThe file extension. Can be with or without a leading dot. Case-insensitive.

Returns: int

imageTypeToExtension(int imageType)

public static String imageTypeToExtension(int imageType)

Parameters:

ParameterTypeDescription
imageTypeint

Returns: java.lang.String

loadFormatToExtension(int loadFormat)

public static String loadFormatToExtension(int loadFormat)

Parameters:

ParameterTypeDescription
loadFormatint

Returns: java.lang.String

loadFormatToSaveFormat(int loadFormat)

public static int loadFormatToSaveFormat(int loadFormat)

Parameters:

ParameterTypeDescription
loadFormatint

Returns: int

saveFormatToExtension(int saveFormat)

public static String saveFormatToExtension(int saveFormat)

Parameters:

ParameterTypeDescription
saveFormatint

Returns: java.lang.String

saveFormatToLoadFormat(int saveFormat)

public static int saveFormatToLoadFormat(int saveFormat)

Parameters:

ParameterTypeDescription
saveFormatint

Returns: int