com.aspose.words
Class FileFormatUtil

java.lang.Object
    extended by com.aspose.words.FileFormatUtil

public abstract class FileFormatUtil 
extends java.lang.Object

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

Example:

Shows how to detect encoding in an html file.
// 'DetectFileFormat' not working on a non-html files
FileFormatInfo info = FileFormatUtil.detectFileFormat(getMyDir() + "Document.docx");
Assert.assertEquals(info.getLoadFormat(), LoadFormat.DOCX);
Assert.assertNull(info.getEncoding());

// This time the property will not be null
info = FileFormatUtil.detectFileFormat(getMyDir() + "Document.html");
Assert.assertEquals(LoadFormat.HTML, info.getLoadFormat());
Assert.assertNotNull(info.getEncoding());

Method Summary
static intcontentTypeToLoadFormat(java.lang.String contentType)
           Converts IANA content type into a load format enumerated value.
static intcontentTypeToSaveFormat(java.lang.String contentType)
           Converts IANA content type into a save format enumerated value.
static FileFormatInfodetectFileFormat(java.io.InputStream stream)
           Detects and returns the information about a format of a document stored in a stream.
static FileFormatInfodetectFileFormat(java.lang.String fileName)
           Detects and returns the information about a format of a document stored in a disk file.
static intextensionToSaveFormat(java.lang.String extension)
           Converts a file name extension into a SaveFormat value.
static java.lang.StringimageTypeToExtension(int imageType)
           Converts an Aspose.Words image type enumerated value into a file extension. The returned extension is a lower-case string with a leading dot.
static java.lang.StringloadFormatToExtension(int loadFormat)
           Converts a load format enumerated value into a file extension. The returned extension is a lower-case string with a leading dot.
static intloadFormatToSaveFormat(int loadFormat)
           Converts a LoadFormat value to a SaveFormat value if possible.
static java.lang.StringsaveFormatToExtension(int saveFormat)
           Converts a save format enumerated value into a file extension. The returned extension is a lower-case string with a leading dot.
static intsaveFormatToLoadFormat(int saveFormat)
           Converts a SaveFormat value to a LoadFormat value if possible.
 

Method Detail

contentTypeToLoadFormat

public static int contentTypeToLoadFormat(java.lang.String contentType)
Converts IANA content type into a load format enumerated value.

Example:

Shows how to find the corresponding Aspose load/save format from an IANA content type string.
// Trying to search for a SaveFormat with a simple string will not work
try {
    Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("jpeg"), SaveFormat.JPEG);
} catch (IllegalArgumentException e) {
    System.out.println(e.getMessage());
}

// The convertion methods only accept official IANA type names, which are all listed here:
//      https://www.iana.org/assignments/media-types/media-types.xhtml
// Note that if a corresponding SaveFormat or LoadFormat for a type from that list does not exist in the Aspose enums,
// converting will raise an exception just like in the code above 

// File types that can be saved to but not opened as documents will not have corresponding load formats
// Attempting to convert them to load formats will raise an exception
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("image/jpeg"), SaveFormat.JPEG);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("image/png"), SaveFormat.PNG);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("image/tiff"), SaveFormat.TIFF);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("image/gif"), SaveFormat.GIF);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("image/x-emf"), SaveFormat.EMF);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("application/vnd.ms-xpsdocument"), SaveFormat.XPS);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("application/pdf"), SaveFormat.PDF);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("image/svg+xml"), SaveFormat.SVG);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("application/epub+zip"), SaveFormat.EPUB);

// File types that can both be loaded and saved have corresponding load and save formats
Assert.assertEquals(FileFormatUtil.contentTypeToLoadFormat("application/msword"), LoadFormat.DOC);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("application/msword"), SaveFormat.DOC);

Assert.assertEquals(FileFormatUtil.contentTypeToLoadFormat("application/vnd.openxmlformats-officedocument.wordprocessingml.document"), LoadFormat.DOCX);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("application/vnd.openxmlformats-officedocument.wordprocessingml.document"), SaveFormat.DOCX);

Assert.assertEquals(FileFormatUtil.contentTypeToLoadFormat("text/plain"), LoadFormat.TEXT);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("text/plain"), SaveFormat.TEXT);

Assert.assertEquals(FileFormatUtil.contentTypeToLoadFormat("application/rtf"), LoadFormat.RTF);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("application/rtf"), SaveFormat.RTF);

Assert.assertEquals(FileFormatUtil.contentTypeToLoadFormat("text/html"), LoadFormat.HTML);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("text/html"), SaveFormat.HTML);

Assert.assertEquals(FileFormatUtil.contentTypeToLoadFormat("multipart/related"), LoadFormat.MHTML);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("multipart/related"), SaveFormat.MHTML);

contentTypeToSaveFormat

public static int contentTypeToSaveFormat(java.lang.String contentType)
Converts IANA content type into a save format enumerated value.

Example:

Shows how to find the corresponding Aspose load/save format from an IANA content type string.
// Trying to search for a SaveFormat with a simple string will not work
try {
    Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("jpeg"), SaveFormat.JPEG);
} catch (IllegalArgumentException e) {
    System.out.println(e.getMessage());
}

// The convertion methods only accept official IANA type names, which are all listed here:
//      https://www.iana.org/assignments/media-types/media-types.xhtml
// Note that if a corresponding SaveFormat or LoadFormat for a type from that list does not exist in the Aspose enums,
// converting will raise an exception just like in the code above 

// File types that can be saved to but not opened as documents will not have corresponding load formats
// Attempting to convert them to load formats will raise an exception
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("image/jpeg"), SaveFormat.JPEG);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("image/png"), SaveFormat.PNG);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("image/tiff"), SaveFormat.TIFF);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("image/gif"), SaveFormat.GIF);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("image/x-emf"), SaveFormat.EMF);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("application/vnd.ms-xpsdocument"), SaveFormat.XPS);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("application/pdf"), SaveFormat.PDF);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("image/svg+xml"), SaveFormat.SVG);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("application/epub+zip"), SaveFormat.EPUB);

// File types that can both be loaded and saved have corresponding load and save formats
Assert.assertEquals(FileFormatUtil.contentTypeToLoadFormat("application/msword"), LoadFormat.DOC);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("application/msword"), SaveFormat.DOC);

Assert.assertEquals(FileFormatUtil.contentTypeToLoadFormat("application/vnd.openxmlformats-officedocument.wordprocessingml.document"), LoadFormat.DOCX);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("application/vnd.openxmlformats-officedocument.wordprocessingml.document"), SaveFormat.DOCX);

Assert.assertEquals(FileFormatUtil.contentTypeToLoadFormat("text/plain"), LoadFormat.TEXT);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("text/plain"), SaveFormat.TEXT);

Assert.assertEquals(FileFormatUtil.contentTypeToLoadFormat("application/rtf"), LoadFormat.RTF);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("application/rtf"), SaveFormat.RTF);

Assert.assertEquals(FileFormatUtil.contentTypeToLoadFormat("text/html"), LoadFormat.HTML);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("text/html"), SaveFormat.HTML);

Assert.assertEquals(FileFormatUtil.contentTypeToLoadFormat("multipart/related"), LoadFormat.MHTML);
Assert.assertEquals(FileFormatUtil.contentTypeToSaveFormat("multipart/related"), SaveFormat.MHTML);

detectFileFormat

public static FileFormatInfo detectFileFormat(java.io.InputStream stream)
                                           throws java.lang.Exception
Detects and returns the information about a format of a document stored in a stream.

The stream must be positioned at the beginning of the document.

Detecting a file format might require seeking to various positions in the stream. Because java.io.InputStream is not seekable, this method loads the whole stream into memory temporarily. When this method returns, the stream is positioned at the end of the document.

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.

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

Example:

Shows how to use the FileFormatUtil methods to detect the format of a document without any extension and save it with the correct file extension.
// Load the document without a file extension into a stream and use the DetectFileFormat method to detect it's format
// These are both times where you might need extract the file format as it's not visible
// The file format of this document is actually ".doc"
FileInputStream docStream = new FileInputStream(getMyDir() + "Word document with missing file extension");
FileFormatInfo info = FileFormatUtil.detectFileFormat(docStream);

// Retrieve the LoadFormat of the document
int loadFormat = info.getLoadFormat();

// Let's show the different methods of converting LoadFormat enumerations to SaveFormat enumerations
//
// Method #1
// Convert the LoadFormat to a String first for working with. The String will include the leading dot in front of the extension
String fileExtension = FileFormatUtil.loadFormatToExtension(loadFormat);
// Now convert this extension into the corresponding SaveFormat enumeration
int saveFormat = FileFormatUtil.extensionToSaveFormat(fileExtension);

// Method #2
// Convert the LoadFormat enumeration directly to the SaveFormat enumeration
saveFormat = FileFormatUtil.loadFormatToSaveFormat(loadFormat);

// Load a document from the stream.
Document doc = new Document(docStream);

// Save the document with the original file name, " Out" and the document's file extension
doc.save(getArtifactsDir() + "File.SaveToDetectedFileFormat" + FileFormatUtil.saveFormatToExtension(saveFormat));

detectFileFormat

public static FileFormatInfo detectFileFormat(java.lang.String fileName)
                                           throws java.lang.Exception
Detects and returns the information about a format of a document stored in a disk file.

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.

Parameters:
fileName - The file name.
Returns:
A FileFormatInfo object that contains the detected information.

Example:

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());

SignOptions signOptions = new SignOptions();
signOptions.setSignTime(new Date());

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

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

Assert.assertTrue(info.hasDigitalSignature());

// The signatures can then be accessed like this
Assert.assertEquals(1, DigitalSignatureUtil.loadSignatures(getArtifactsDir() + "File.DetectDigitalSignatures.docx").getCount());

Example:

Shows how to use the FileFormatUtil class to detect the document format and encryption.
Document doc = new Document();

// Save it as an encrypted .odt
OdtSaveOptions saveOptions = new OdtSaveOptions(SaveFormat.ODT);
saveOptions.setPassword("MyPassword");

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

// Create a FileFormatInfo object for this document
FileFormatInfo info = FileFormatUtil.detectFileFormat(getArtifactsDir() + "File.DetectDocumentEncryption.odt");

// Verify the file type of our document and its encryption status
Assert.assertEquals(".odt", FileFormatUtil.loadFormatToExtension(info.getLoadFormat()));
Assert.assertTrue(info.isEncrypted());

extensionToSaveFormat

public static int extensionToSaveFormat(java.lang.String extension)
Converts a file name extension into a SaveFormat value.

If the extension cannot be recognized, returns SaveFormat.UNKNOWN.

Parameters:
extension - The file extension. Can be with or without a leading dot. Case-insensitive.

Example:

Shows how to use the FileFormatUtil methods to detect the format of a document without any extension and save it with the correct file extension.
// Load the document without a file extension into a stream and use the DetectFileFormat method to detect it's format
// These are both times where you might need extract the file format as it's not visible
// The file format of this document is actually ".doc"
FileInputStream docStream = new FileInputStream(getMyDir() + "Word document with missing file extension");
FileFormatInfo info = FileFormatUtil.detectFileFormat(docStream);

// Retrieve the LoadFormat of the document
int loadFormat = info.getLoadFormat();

// Let's show the different methods of converting LoadFormat enumerations to SaveFormat enumerations
//
// Method #1
// Convert the LoadFormat to a String first for working with. The String will include the leading dot in front of the extension
String fileExtension = FileFormatUtil.loadFormatToExtension(loadFormat);
// Now convert this extension into the corresponding SaveFormat enumeration
int saveFormat = FileFormatUtil.extensionToSaveFormat(fileExtension);

// Method #2
// Convert the LoadFormat enumeration directly to the SaveFormat enumeration
saveFormat = FileFormatUtil.loadFormatToSaveFormat(loadFormat);

// Load a document from the stream.
Document doc = new Document(docStream);

// Save the document with the original file name, " Out" and the document's file extension
doc.save(getArtifactsDir() + "File.SaveToDetectedFileFormat" + FileFormatUtil.saveFormatToExtension(saveFormat));

imageTypeToExtension

public static java.lang.String imageTypeToExtension(int imageType)
Converts an Aspose.Words image type enumerated value into a file extension. The returned extension is a lower-case string with a leading dot.
Parameters:
imageType - A ImageType value.

Example:

Shows how to extract images from a document and save them as files.
Document doc = new Document(getMyDir() + "Images.docx");

NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);

int imageIndex = 0;
for (Shape shape : (Iterable<Shape>) shapes) {
    if (shape.hasImage()) {
        String imageFileName = MessageFormat.format("File.ExtractImagesToFiles.{0}{1}", imageIndex,
                FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType()));
        shape.getImageData().save(getArtifactsDir() + imageFileName);
        imageIndex++;
    }
}

Assert.assertEquals(9, imageIndex);

loadFormatToExtension

public static java.lang.String loadFormatToExtension(int loadFormat)
Converts a load format enumerated value into a file extension. The returned extension is a lower-case string with a leading dot.

The SaveFormat.WORD_ML value is converted to ".wml".

Parameters:
loadFormat - A LoadFormat value.

Example:

Shows how to use the FileFormatUtil methods to detect the format of a document without any extension and save it with the correct file extension.
// Load the document without a file extension into a stream and use the DetectFileFormat method to detect it's format
// These are both times where you might need extract the file format as it's not visible
// The file format of this document is actually ".doc"
FileInputStream docStream = new FileInputStream(getMyDir() + "Word document with missing file extension");
FileFormatInfo info = FileFormatUtil.detectFileFormat(docStream);

// Retrieve the LoadFormat of the document
int loadFormat = info.getLoadFormat();

// Let's show the different methods of converting LoadFormat enumerations to SaveFormat enumerations
//
// Method #1
// Convert the LoadFormat to a String first for working with. The String will include the leading dot in front of the extension
String fileExtension = FileFormatUtil.loadFormatToExtension(loadFormat);
// Now convert this extension into the corresponding SaveFormat enumeration
int saveFormat = FileFormatUtil.extensionToSaveFormat(fileExtension);

// Method #2
// Convert the LoadFormat enumeration directly to the SaveFormat enumeration
saveFormat = FileFormatUtil.loadFormatToSaveFormat(loadFormat);

// Load a document from the stream.
Document doc = new Document(docStream);

// Save the document with the original file name, " Out" and the document's file extension
doc.save(getArtifactsDir() + "File.SaveToDetectedFileFormat" + FileFormatUtil.saveFormatToExtension(saveFormat));

loadFormatToSaveFormat

public static int loadFormatToSaveFormat(int loadFormat)
Converts a LoadFormat value to a SaveFormat value if possible.
Parameters:
loadFormat - A LoadFormat value.

Example:

Shows how to use the FileFormatUtil methods to detect the format of a document without any extension and save it with the correct file extension.
// Load the document without a file extension into a stream and use the DetectFileFormat method to detect it's format
// These are both times where you might need extract the file format as it's not visible
// The file format of this document is actually ".doc"
FileInputStream docStream = new FileInputStream(getMyDir() + "Word document with missing file extension");
FileFormatInfo info = FileFormatUtil.detectFileFormat(docStream);

// Retrieve the LoadFormat of the document
int loadFormat = info.getLoadFormat();

// Let's show the different methods of converting LoadFormat enumerations to SaveFormat enumerations
//
// Method #1
// Convert the LoadFormat to a String first for working with. The String will include the leading dot in front of the extension
String fileExtension = FileFormatUtil.loadFormatToExtension(loadFormat);
// Now convert this extension into the corresponding SaveFormat enumeration
int saveFormat = FileFormatUtil.extensionToSaveFormat(fileExtension);

// Method #2
// Convert the LoadFormat enumeration directly to the SaveFormat enumeration
saveFormat = FileFormatUtil.loadFormatToSaveFormat(loadFormat);

// Load a document from the stream.
Document doc = new Document(docStream);

// Save the document with the original file name, " Out" and the document's file extension
doc.save(getArtifactsDir() + "File.SaveToDetectedFileFormat" + FileFormatUtil.saveFormatToExtension(saveFormat));

saveFormatToExtension

public static java.lang.String saveFormatToExtension(int saveFormat)
Converts a save format enumerated value into a file extension. The returned extension is a lower-case string with a leading dot.

The SaveFormat.WORD_ML value is converted to ".wml".

The SaveFormat.FLAT_OPC value is converted to ".fopc".

Parameters:
saveFormat - A SaveFormat value.

Example:

Shows how to use the FileFormatUtil methods to detect the format of a document without any extension and save it with the correct file extension.
// Load the document without a file extension into a stream and use the DetectFileFormat method to detect it's format
// These are both times where you might need extract the file format as it's not visible
// The file format of this document is actually ".doc"
FileInputStream docStream = new FileInputStream(getMyDir() + "Word document with missing file extension");
FileFormatInfo info = FileFormatUtil.detectFileFormat(docStream);

// Retrieve the LoadFormat of the document
int loadFormat = info.getLoadFormat();

// Let's show the different methods of converting LoadFormat enumerations to SaveFormat enumerations
//
// Method #1
// Convert the LoadFormat to a String first for working with. The String will include the leading dot in front of the extension
String fileExtension = FileFormatUtil.loadFormatToExtension(loadFormat);
// Now convert this extension into the corresponding SaveFormat enumeration
int saveFormat = FileFormatUtil.extensionToSaveFormat(fileExtension);

// Method #2
// Convert the LoadFormat enumeration directly to the SaveFormat enumeration
saveFormat = FileFormatUtil.loadFormatToSaveFormat(loadFormat);

// Load a document from the stream.
Document doc = new Document(docStream);

// Save the document with the original file name, " Out" and the document's file extension
doc.save(getArtifactsDir() + "File.SaveToDetectedFileFormat" + FileFormatUtil.saveFormatToExtension(saveFormat));

saveFormatToLoadFormat

public static int saveFormatToLoadFormat(int saveFormat)
Converts a SaveFormat value to a LoadFormat value if possible.
Parameters:
saveFormat - A SaveFormat value.

Example:

Shows how to use the FileFormatUtil class and to convert a SaveFormat enumeration into the corresponding LoadFormat enumeration.
// Define the SaveFormat enumeration to convert
int saveFormat = SaveFormat.HTML;
// Convert the SaveFormat enumeration to LoadFormat enumeration
int loadFormat = FileFormatUtil.saveFormatToLoadFormat(saveFormat);
System.out.println("The converted LoadFormat is: " + FileFormatUtil.loadFormatToExtension(loadFormat));

See Also:
          Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
          Aspose.Words Support Forum - our preferred method of support.