Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Aug 22, 2012 2:57 pm

Hi e-iceblue,

is there a possability with Spire.Doc to detect the file format (doc, docx, rtf, etc...) by file content (not filename extension)?

Kind regards,
emediat

emediat
 
Posts: 3
Joined: Thu Aug 02, 2012 8:18 am

Thu Aug 23, 2012 3:12 am

Hi emediat,

Thanks for your support to us.

For the function"detect the file format (doc, docx, rtf, etc...) by file content (not filename extension)", Spire.Doc new version will support it. Once the version has released, we will inform you immediately. If you are anxious to use the function " detect the file format" now, you can refer to the following code. Hope it can help you.
Code: Select all
private FileFormat GetFileFormat(string fileName)
        {
            FileFormat fileFormat = FileFormat.Doc;
            FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            stream.Position = 0L;
            byte[] buffer = new byte[5];
            if (((stream.Read(buffer, 0, 5) == 5) && (buffer[0] == 80))
                && (buffer[1] == 0x4b))
            {
                stream.Position = 0L;
                return FileFormat.Docx;
            }
            else if ((((buffer[0] == 0x7b) && (buffer[1] == 0x5c))
                && ((buffer[2] == 0x72) && (buffer[3] == 0x74)))
                && (buffer[4] == 0x66))
            {
                stream.Position = 0L;
                return FileFormat.Rtf;
            }
            stream.Close();
            return fileFormat;
        }

If you have any probelms, please feel free to contact us.
Best wishes.
Amy
e-iceblue support
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc