com.aspose.words

Class FileFormatInfo

  • java.lang.Object
    • com.aspose.words.FileFormatInfo
public class FileFormatInfo 
extends java.lang.Object

Contains data returned by FileFormatUtil document format detection methods.

You do not create instances of this class directly. Objects of this class are returned by FileFormatUtil.detectFileFormat(java.io.InputStream) methods.

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

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_60:
            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_60:
                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;
        }
    }
}

Property Getters/Setters Summary
java.nio.charset.CharsetgetEncoding()
Gets the detected encoding if applicable to the current document format. At the moment detects encoding only for HTML documents.
booleanhasDigitalSignature()
Returns true if this document contains a digital signature. This property merely informs that a digital signature is present on a document, but it does not specify whether the signature is valid or not.
booleanisEncrypted()
Returns true if the document is encrypted and requires a password to open.
intgetLoadFormat()
Gets the detected document format. The value of the property is LoadFormat integer constant.
 

    • Property Getters/Setters Detail

      • getEncoding

        public java.nio.charset.Charset getEncoding()
        
        Gets the detected encoding if applicable to the current document format. At the moment detects encoding only for HTML documents.
      • hasDigitalSignature

        public boolean hasDigitalSignature()
        
        Returns true if this document contains a digital signature. This property merely informs that a digital signature is present on a document, but it does not specify whether the signature is valid or not.

        This property exists to help you sort documents that are digitally signed from those that are not. If you use Aspose.Words to modify and save a document that is digitally signed, then the digital signature will be lost. This is by design because a digital signature exists to guard the authenticity of a document. Using this property you can detect digitally signed documents before processing them in the same way as normal documents and take some action to avoid losing the digital signature, for example notify the user.

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

        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()));
        }
      • isEncrypted

        public boolean isEncrypted()
        
        Returns true if the document is encrypted and requires a password to open.

        This property exists to help you sort documents that are encrypted from those that are not. If you attempt to load an encrypted document using Aspose.Words without supplying a password an exception will be thrown. You can use this property to detect whether a document requires a password and take some action before loading a document, for example, prompt the user for a password.

        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());
        See Also:
        LoadFormat
      • getLoadFormat

        public int getLoadFormat()
        
        Gets the detected document format. The value of the property is LoadFormat integer constant.

        When an OOXML document is encrypted, it is not possible to ascertained whether it is an Excel, Word or PowerPoint document without decrypting it first so for an encrypted OOXML document this property will always return LoadFormat.DOCX.

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

        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));
        See Also:
        IsEncrypted