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.

Thu Mar 10, 2022 7:54 pm

Hi,

We are using Spire.DOC to convert Word documents to PDF. For the most part working well; however we may get file formats that we dont know the format type via stream. An example would be loading a PDF file into the Document object - DetectedFormatType returns TXT. Is this a bug or is there another call to get the document type?

Thanks
Don

dlanglois
 
Posts: 1
Joined: Thu Mar 10, 2022 7:38 pm

Fri Mar 11, 2022 3:46 am

Hello,

Thank you for your inquiry.
Note that Spire.Doc can only load files of the format type it supports, PDF is not a format type supported by Spire.Doc, so after loading a PDF stream file, the value obtained by DetectedFormatType is incorrect. So you need to use your own code to determine the file format before loading the flow file, and then deal with it.
Please refer to the following code to determine the file format of the stream file:
Code: Select all
static void Main(string[] args)
{
    string fileName = "input.pdf";
    FileExtension fileExtension= CheckFileType(fileName);
}
public static FileExtension CheckFileType(string fileName)
{
    FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    string fileType = string.Empty; ;
    try
    {
        byte data = br.ReadByte();
        fileType += data.ToString();
        data = br.ReadByte();
        fileType += data.ToString();
        FileExtension extension;
        try
        {
            extension = (FileExtension)Enum.Parse(typeof(FileExtension), fileType);
        }
        catch
        {
            extension = FileExtension.VALIDFILE;
        }
        return extension;
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        if (fs != null)
        {
            fs.Close();
            br.Close();
        }
    }
}
public enum FileExtension
{
    JPG = 255216,
    GIF = 7173,
    BMP = 6677,
    PNG = 13780,
    COM = 7790,
    EXE = 7790,
    DLL = 7790,
    RAR = 8297,
    ZIP = 8075,
    XML = 6063,
    HTML = 6033,
    ASPX = 239187,
    CS = 117115,
    JS = 119105,
    TXT = 210187,
    SQL = 255254,
    BAT = 64101,
    BTSEED = 10056,
    RDP = 255254,
    PSD = 5666,
    PDF = 3780,
    CHM = 7384,
    LOG = 70105,
    REG = 8269,
    HLP = 6395,
    DOC = 208207,
    XLS = 208207,
    DOCX = 208207,
    XLSX = 208207,
    VALIDFILE = 9999999
}

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1642
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc