i need to check if the word document is valid, not just another document(pdf, excel,…) with a changed extension, any ideas?
Hi,
Thanks for your inquiry. Please see the following code which shows how to use the FileFormatUtil class to detect the document format and other features of the document. If the document is in unrecognised format or cannot be loaded by Aspose.Words, the LoadFormat.Unknown value would be returned:
FileFormatInfo info = FileFormatUtil.DetectFileFormat(MyDir + "Document.doc");
Console.WriteLine("The document format is: " + FileFormatUtil.LoadFormatToExtension(info.LoadFormat));
Console.WriteLine("Document is encrypted: " + info.IsEncrypted);
Console.WriteLine("Document has a digital signature: " + info.HasDigitalSignature);
Best regards,
thanks for your reply but this isn’t the issue here,
for example i take an excel file and i change its extension to .doc,
i need to check if this word document is valid, for example i have done the following for PDF files and it works:
com.aspose.pdf.facades.PdfFileInfo info = new com.aspose.pdf.facades.PdfFileInfo(filePath);
if (!info.isPdfFile())
{
throw new Exception("Invalid File Content");
}
any idea for other extensions like doc, docx, xls, xlsx, …
Hi,
Thanks for the additional information.
The FileFormatInfo.DetectFileFormat method can detect the document format irrespective of the file extension. It would still return LoadFormat.Unknown value even when you renamed a .xls Excel document to .doc. However, 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. As, Aspose.Words can’t load an Excel document into it’s DOM, the Document constructor would throw a “Aspose.Words.UnsupportedFileFormatException” which you can catch to determine whether a loaded document is valid or not.
Best regards,
Hi,
Similarly, if you need to detect a file format type for Excel spreadsheets, I think you may try the relevant Aspose.Cells API, see the documents for your reference:
https://docs.aspose.com/cells/net/how-to-detect-a-file-format-and-check-if-the-file-is-encrypted/
Thank you.