java.lang.Objectcom.aspose.words.FileFormatUtil
public class FileFormatUtil
Example:
// Loop through all found files.
for (File file : fileList)
{
if (file.isDirectory())
continue;
// Extract and display the file name without the path.
String nameOnly = file.getName();
System.out.print(nameOnly);
// Check the file format and move the file to the appropriate folder.
String fileName = file.getPath();
FileFormatInfo info = FileFormatUtil.detectFileFormat(fileName);
// Display the document type.
switch (info.getLoadFormat())
{
case LoadFormat.DOC:
System.out.println("\tMicrosoft Word 97-2003 document.");
break;
case LoadFormat.DOT:
System.out.println("\tMicrosoft Word 97-2003 template.");
break;
case LoadFormat.DOCX:
System.out.println("\tOffice Open XML WordprocessingML Macro-Free Document.");
break;
case LoadFormat.DOCM:
System.out.println("\tOffice Open XML WordprocessingML Macro-Enabled Document.");
break;
case LoadFormat.DOTX:
System.out.println("\tOffice Open XML WordprocessingML Macro-Free Template.");
break;
case LoadFormat.DOTM:
System.out.println("\tOffice Open XML WordprocessingML Macro-Enabled Template.");
break;
case LoadFormat.FLAT_OPC:
System.out.println("\tFlat OPC document.");
break;
case LoadFormat.RTF:
System.out.println("\tRTF format.");
break;
case LoadFormat.WORD_ML:
System.out.println("\tMicrosoft Word 2003 WordprocessingML format.");
break;
case LoadFormat.HTML:
System.out.println("\tHTML format.");
break;
case LoadFormat.MHTML:
System.out.println("\tMHTML (Web archive) format.");
break;
case LoadFormat.ODT:
System.out.println("\tOpenDocument Text.");
break;
case LoadFormat.OTT:
System.out.println("\tOpenDocument Text Template.");
break;
case LoadFormat.DOC_PRE_WORD_97:
System.out.println("\tMS Word 6 or Word 95 format.");
break;
case LoadFormat.UNKNOWN:
default:
System.out.println("\tUnknown format.");
break;
}
// Now copy the document into the appropriate folder.
if (info.isEncrypted())
{
System.out.println("\tAn encrypted document.");
fileCopy(fileName, new File(encryptedDir, nameOnly).getPath());
}
else
{
switch (info.getLoadFormat())
{
case LoadFormat.DOC_PRE_WORD_97:
fileCopy(fileName, new File(pre97Dir + nameOnly).getPath());
break;
case LoadFormat.UNKNOWN:
fileCopy(fileName, new File(unknownDir + nameOnly).getPath());
break;
default:
fileCopy(fileName, new File(supportedDir + nameOnly).getPath());
break;
}
}
}
Method Summary | ||
---|---|---|
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) | |
Detects and returns the information about a format of a document stored in a stream. | ||
static FileFormatInfo | detectFileFormat(java.lang.String fileName) | |
Detects and returns the information about a format of a document stored in a disk file. | ||
static int | extensionToSaveFormat(java.lang.String extension) | |
Converts a file name extension into a |
||
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. | ||
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. | ||
static int | loadFormatToSaveFormat(int loadFormat) | |
Converts a |
||
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. | ||
static int | saveFormatToLoadFormat(int saveFormat) | |
Converts a |
Method Detail |
---|
contentTypeToLoadFormat | |
public static int contentTypeToLoadFormat(java.lang.String contentType) |
contentTypeToSaveFormat | |
public static int contentTypeToSaveFormat(java.lang.String contentType) |
detectFileFormat | |
public static FileFormatInfo detectFileFormat(java.io.InputStream stream) throws java.lang.Exception |
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
This method throws
stream
- The stream.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 FileInputStream docStream = new FileInputStream(getMyDir() + "Document.FileWithoutExtension"); // The file format of this document is actually ".doc" 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 infront 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. // Note that in Java we cannot reuse the same InputStream instance that was used for file format detection because InputStream is not seekable. docStream = new FileInputStream(getMyDir() + "Document.FileWithoutExtension"); // The file format of this document is actually ".doc" Document doc = new Document(docStream); // Save the document with the original file name, " Out" and the document's file extension. doc.save(getMyDir() + "Document.WithFileExtension Out" + FileFormatUtil.saveFormatToExtension(saveFormat));
detectFileFormat | |
public static FileFormatInfo detectFileFormat(java.lang.String fileName) throws java.lang.Exception |
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
This method throws
fileName
- The file name.Example:
Check each file in the folder and move it to the appropriate subfolder.// Loop through all found files. for (File file : fileList) { if (file.isDirectory()) continue; // Extract and display the file name without the path. String nameOnly = file.getName(); System.out.print(nameOnly); // Check the file format and move the file to the appropriate folder. String fileName = file.getPath(); FileFormatInfo info = FileFormatUtil.detectFileFormat(fileName); // Display the document type. switch (info.getLoadFormat()) { case LoadFormat.DOC: System.out.println("\tMicrosoft Word 97-2003 document."); break; case LoadFormat.DOT: System.out.println("\tMicrosoft Word 97-2003 template."); break; case LoadFormat.DOCX: System.out.println("\tOffice Open XML WordprocessingML Macro-Free Document."); break; case LoadFormat.DOCM: System.out.println("\tOffice Open XML WordprocessingML Macro-Enabled Document."); break; case LoadFormat.DOTX: System.out.println("\tOffice Open XML WordprocessingML Macro-Free Template."); break; case LoadFormat.DOTM: System.out.println("\tOffice Open XML WordprocessingML Macro-Enabled Template."); break; case LoadFormat.FLAT_OPC: System.out.println("\tFlat OPC document."); break; case LoadFormat.RTF: System.out.println("\tRTF format."); break; case LoadFormat.WORD_ML: System.out.println("\tMicrosoft Word 2003 WordprocessingML format."); break; case LoadFormat.HTML: System.out.println("\tHTML format."); break; case LoadFormat.MHTML: System.out.println("\tMHTML (Web archive) format."); break; case LoadFormat.ODT: System.out.println("\tOpenDocument Text."); break; case LoadFormat.OTT: System.out.println("\tOpenDocument Text Template."); break; case LoadFormat.DOC_PRE_WORD_97: System.out.println("\tMS Word 6 or Word 95 format."); break; case LoadFormat.UNKNOWN: default: System.out.println("\tUnknown format."); break; } // Now copy the document into the appropriate folder. if (info.isEncrypted()) { System.out.println("\tAn encrypted document."); fileCopy(fileName, new File(encryptedDir, nameOnly).getPath()); } else { switch (info.getLoadFormat()) { case LoadFormat.DOC_PRE_WORD_97: fileCopy(fileName, new File(pre97Dir + nameOnly).getPath()); break; case LoadFormat.UNKNOWN: fileCopy(fileName, new File(unknownDir + nameOnly).getPath()); break; default: fileCopy(fileName, new File(supportedDir + nameOnly).getPath()); break; } } }
Example:
Shows how to check a document for digital signatures before loading it into a Document object.// The path to the document which is to be processed. String filePath = getMyDir() + "Document.Signed.docx"; FileFormatInfo info = FileFormatUtil.detectFileFormat(filePath); if (info.hasDigitalSignature()) { System.out.println(java.text.MessageFormat.format( "Document {0} has digital signatures, they will be lost if you open/save this document with Aspose.Words.", new File(filePath).getName())); }
Example:
Shows how to use the FileFormatUtil class to detect the document format and other features of the document.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());
extensionToSaveFormat | |
public static int extensionToSaveFormat(java.lang.String extension) |
If the extension cannot be recognized, returns
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 FileInputStream docStream = new FileInputStream(getMyDir() + "Document.FileWithoutExtension"); // The file format of this document is actually ".doc" 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 infront 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. // Note that in Java we cannot reuse the same InputStream instance that was used for file format detection because InputStream is not seekable. docStream = new FileInputStream(getMyDir() + "Document.FileWithoutExtension"); // The file format of this document is actually ".doc" Document doc = new Document(docStream); // Save the document with the original file name, " Out" and the document's file extension. doc.save(getMyDir() + "Document.WithFileExtension Out" + FileFormatUtil.saveFormatToExtension(saveFormat));
imageTypeToExtension | |
public static java.lang.String imageTypeToExtension(int imageType) |
imageType
- A ImageType value.Example:
Shows how to extract images from a document and save them as files.public void extractImagesToFiles() throws Exception { Document doc = new Document(getMyDir() + "Image.SampleImages.doc"); NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true); int imageIndex = 0; for (Shape shape : (Iterable<Shape>) shapes) { if (shape.hasImage()) { String imageFileName = java.text.MessageFormat.format( "Image.ExportImages.{0} Out{1}", imageIndex, FileFormatUtil.imageTypeToExtension(shape.getImageData().getImageType())); shape.getImageData().save(getMyDir() + imageFileName); imageIndex++; } } }
loadFormatToExtension | |
public static java.lang.String loadFormatToExtension(int loadFormat) |
The
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 FileInputStream docStream = new FileInputStream(getMyDir() + "Document.FileWithoutExtension"); // The file format of this document is actually ".doc" 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 infront 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. // Note that in Java we cannot reuse the same InputStream instance that was used for file format detection because InputStream is not seekable. docStream = new FileInputStream(getMyDir() + "Document.FileWithoutExtension"); // The file format of this document is actually ".doc" Document doc = new Document(docStream); // Save the document with the original file name, " Out" and the document's file extension. doc.save(getMyDir() + "Document.WithFileExtension Out" + FileFormatUtil.saveFormatToExtension(saveFormat));
loadFormatToSaveFormat | |
public static int loadFormatToSaveFormat(int loadFormat) |
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 FileInputStream docStream = new FileInputStream(getMyDir() + "Document.FileWithoutExtension"); // The file format of this document is actually ".doc" 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 infront 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. // Note that in Java we cannot reuse the same InputStream instance that was used for file format detection because InputStream is not seekable. docStream = new FileInputStream(getMyDir() + "Document.FileWithoutExtension"); // The file format of this document is actually ".doc" Document doc = new Document(docStream); // Save the document with the original file name, " Out" and the document's file extension. doc.save(getMyDir() + "Document.WithFileExtension Out" + FileFormatUtil.saveFormatToExtension(saveFormat));
saveFormatToExtension | |
public static java.lang.String saveFormatToExtension(int saveFormat) |
The
The
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 FileInputStream docStream = new FileInputStream(getMyDir() + "Document.FileWithoutExtension"); // The file format of this document is actually ".doc" 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 infront 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. // Note that in Java we cannot reuse the same InputStream instance that was used for file format detection because InputStream is not seekable. docStream = new FileInputStream(getMyDir() + "Document.FileWithoutExtension"); // The file format of this document is actually ".doc" Document doc = new Document(docStream); // Save the document with the original file name, " Out" and the document's file extension. doc.save(getMyDir() + "Document.WithFileExtension Out" + FileFormatUtil.saveFormatToExtension(saveFormat));
saveFormatToLoadFormat | |
public static int saveFormatToLoadFormat(int saveFormat) |
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));