com.aspose.words

Class FileFormatUtil

  • java.lang.Object
    • 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.

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.
      • contentTypeToSaveFormat

        public static int contentTypeToSaveFormat(java.lang.String contentType)
        Converts IANA content type into a save format enumerated value.
      • 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
        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() + "\\Artifacts\\Document.WithFileExtension" + 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 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)
        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
        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() + "\\Artifacts\\Document.WithFileExtension" + 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.
        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("\\Artifacts\\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)
        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
        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() + "\\Artifacts\\Document.WithFileExtension" + 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
        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() + "\\Artifacts\\Document.WithFileExtension" + 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
        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() + "\\Artifacts\\Document.WithFileExtension" + 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));