java.lang.Objectcom.aspose.words.FileFormatInfo
public class FileFormatInfo
You do not create instances of this class directly. Objects of this class are returned by
Example: Example:
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());
// 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());
Property Getters/Setters Summary | ||
---|---|---|
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. | ||
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. | ||
boolean | isEncrypted() | |
Returns true if the document is encrypted and requires a password to open. | ||
int | getLoadFormat() | |
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() |
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());
hasDigitalSignature | |
public boolean hasDigitalSignature() |
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 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());
isEncrypted | |
public boolean isEncrypted() |
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 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());
getLoadFormat | |
public int getLoadFormat() |
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
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());
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 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));