Get file type

If I have byte[] parameter, is there any way to know if it is a Word or Pdf or Excel file different of doing this?


try{wordDocument = new Word.Document(documentStream);} catch (Exception){isWord = false;}
try{pdfDocument = new Pdf.Document(documentStream);} catch (Exception) {isPdf = false;}

Hi Mauricio,

Thanks for your inquiry. Sure, in case of a Word document, the following code snippet shows how to use the FileFormatUtil methods to detect the format of a document:

FileStream docStream = File.OpenRead(@"C:\Temp\somedocument");
FileFormatInfo info = FileFormatUtil.DetectFileFormat(docStream);
LoadFormatloadFormat = info.LoadFormat;
stringfileExtension = FileFormatUtil.LoadFormatToExtension(loadFormat);

I hope, this helps.

Moreover, as your query involves multiple file formats, I am moving your thread in Aspose.Total forum where you’ll be guided appropriately.

Best regards,

Hi,

Thanks for your posting and using Aspose.

In case of Aspose.Cells, you can detect the file formats of different Excel documents before actually loading them using the CellsHelper.DetectFileFormat.

Please see the following documentation article for your reference.

http://www.aspose.com/docs/display/cellsnet/How+to+Detect+the+File+Format

mega15:
If I have byte[] parameter, is there any way to know if it is a Word or Pdf or Excel file different of doing this?
try{pdfDocument = new Pdf.Document(documentStream);} catch (Exception) {isPdf = false;}
Hi Mauricio,

Thanks for using our products.

In order to determine if the source/input file is PDF or not, please try using the following code snippet.

[C#]

// instantiate PdfFileInfo object<o:p></o:p>

PdfFileInfo info = new PdfFileInfo();

// bind the source file

info.BindPdf("c:/pdftest/Example.jpg");

// display either the file is PDF for not

Console.WriteLine("Is
Pdf file - "
+ info.IsPdfFile);


Besides this, you can also determine the input file type using MIME signature. For further information, please visit

http://stackoverflow.com/questions/11547654/determine-the-file-type-using-c-sharp
http://stackoverflow.com/questions/58510/using-net-how-can-you-find-the-mime-type-of-a-file-based-on-the-file-signature