public class FileFormatUtil
extends java.lang.Object
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());
| Modifier and Type | Method and Description |
|---|---|
static int |
contentTypeToLoadFormat(java.lang.String contentType)
Converts IANA content type into a load format enumerated value.
|
static int |
contentTypeToSaveFormat(java.lang.String contentType)
Converts IANA content type into a save format enumerated value.
|
static FileFormatInfo |
detectFileFormat(java.io.InputStream stream) |
static FileFormatInfo |
detectFileFormat(java.lang.String fileName)
|
static int |
extensionToSaveFormat(java.lang.String extension)
Converts a file name extension into a
SaveFormat value. |
static java.lang.String |
imageTypeToExtension(int imageType) |
static java.lang.String |
loadFormatToExtension(int loadFormat) |
static int |
loadFormatToSaveFormat(int loadFormat) |
static java.lang.String |
saveFormatToExtension(int saveFormat) |
static int |
saveFormatToLoadFormat(int saveFormat) |
public static FileFormatInfo detectFileFormat(java.lang.String fileName) throws java.lang.Exception
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());
fileName - The file name.FileFormatInfo object that contains the detected information.java.lang.Exceptionpublic static FileFormatInfo detectFileFormat(java.io.InputStream stream) throws java.lang.Exception
java.lang.Exceptionpublic static int contentTypeToLoadFormat(java.lang.String contentType)
java.lang.IllegalArgumentException - Throws when cannot convert.
Examples:
Shows how to find the corresponding Aspose load/save format from each media type string.
// The ContentTypeToSaveFormat/ContentTypeToLoadFormat methods only accept official IANA media type names, also known as MIME types.
// All valid media types are listed here: https://www.iana.org/assignments/media-types/media-types.xhtml.
// Trying to associate a SaveFormat with a partial media type string will not work.
Assert.assertThrows(IllegalArgumentException.class, () -> FileFormatUtil.contentTypeToSaveFormat("jpeg"));
// If Aspose.Words does not have a corresponding save/load format for a content type, an exception will also be thrown.
Assert.assertThrows(IllegalArgumentException.class, () -> FileFormatUtil.contentTypeToSaveFormat("application/zip"));
// Files of the types listed below can be saved, but not loaded using Aspose.Words.
Assert.assertThrows(IllegalArgumentException.class, () -> FileFormatUtil.contentTypeToLoadFormat("image/jpeg"));
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);
// For file types that can be saved and loaded, we can match a media type to both a load format and a save format.
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);
public static int contentTypeToSaveFormat(java.lang.String contentType)
java.lang.IllegalArgumentException - Throws when cannot convert.
Examples:
Shows how to find the corresponding Aspose load/save format from each media type string.
// The ContentTypeToSaveFormat/ContentTypeToLoadFormat methods only accept official IANA media type names, also known as MIME types.
// All valid media types are listed here: https://www.iana.org/assignments/media-types/media-types.xhtml.
// Trying to associate a SaveFormat with a partial media type string will not work.
Assert.assertThrows(IllegalArgumentException.class, () -> FileFormatUtil.contentTypeToSaveFormat("jpeg"));
// If Aspose.Words does not have a corresponding save/load format for a content type, an exception will also be thrown.
Assert.assertThrows(IllegalArgumentException.class, () -> FileFormatUtil.contentTypeToSaveFormat("application/zip"));
// Files of the types listed below can be saved, but not loaded using Aspose.Words.
Assert.assertThrows(IllegalArgumentException.class, () -> FileFormatUtil.contentTypeToLoadFormat("image/jpeg"));
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);
// For file types that can be saved and loaded, we can match a media type to both a load format and a save format.
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);
public static java.lang.String loadFormatToExtension(int loadFormat)
public static int saveFormatToLoadFormat(int saveFormat)
public static int loadFormatToSaveFormat(int loadFormat)
public static java.lang.String saveFormatToExtension(int saveFormat)
public static int extensionToSaveFormat(java.lang.String extension)
SaveFormat value.extension - The file extension. Can be with or without a leading dot. Case-insensitive.com.aspose.words.net.System.ArgumentNullException - Throws if the parameter is null.
Remarks:
If the extension cannot be recognized, returns SaveFormat.UNKNOWN.
Examples:
Shows how to use the FileFormatUtil methods to detect the format of a document.
// Load a document from a file that is missing a file extension, and then detect its file format.
FileInputStream docStream = new FileInputStream(getMyDir() + "Word document with missing file extension");
FileFormatInfo info = FileFormatUtil.detectFileFormat(docStream);
int loadFormat = info.getLoadFormat();
Assert.assertEquals(LoadFormat.DOC, loadFormat);
// Below are two methods of converting a LoadFormat to its corresponding SaveFormat.
// 1 - Get the file extension string for the LoadFormat, then get the corresponding SaveFormat from that string:
String fileExtension = FileFormatUtil.loadFormatToExtension(loadFormat);
int saveFormat = FileFormatUtil.extensionToSaveFormat(fileExtension);
// 2 - Convert the LoadFormat directly to its SaveFormat:
saveFormat = FileFormatUtil.loadFormatToSaveFormat(loadFormat);
// Load a document from the stream, and then save it to the automatically detected file extension.
Document doc = new Document(docStream);
Assert.assertEquals(".doc", FileFormatUtil.saveFormatToExtension(saveFormat));
doc.save(getArtifactsDir() + "File.SaveToDetectedFileFormat" + FileFormatUtil.saveFormatToExtension(saveFormat));
public static java.lang.String imageTypeToExtension(int imageType)