Hot to detect file format

Technical support for Spire.Doc

Moderator: iceblue support

Hot to detect file format

Postby emediat » 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,
Posts: 3
Joined: Thu Aug 02, 2012 8:18 am

Re: Hot to detect file format

Postby amy.zhao » 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;
            return fileFormat;

If you have any probelms, please feel free to contact us.
Best wishes.
e-iceblue support
User avatar
Posts: 2251
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc

Who is online

Users browsing this forum: No registered users and 0 guests