New to Telerik Document Processing? Download free 30-day trial

How to identify the actual document type when the filename extension is not correct

Product Version Product Author
2022.1.217 WordsProcessing Martin Velikov


This article describes how to identify the actual document type when the filename extension is incorrect which helps us to determine the appropriate format provider.


The following example demonstrates how to read two documents with ".doc" filename extensions but actually different document types. Using the StringBuilder class we are creating the document signature (header) string, which later to compare with predefined values. Once having the right document type we can determine which format provider to use to import the document.


List<byte[]> documents = new List<byte[]>(); 
foreach (byte[] document in documents) 
    string headerCode = GetHeaderInfo(document).ToUpper(); 
    //! The signatures are taken from: 
    if (headerCode.StartsWith("7B5C72746631")) 
        //! The document is RTF 
        RtfFormatProvider rtfFormatProvider = new RtfFormatProvider(); 
        RadFlowDocument rtfDocument = rtfFormatProvider.Import(new MemoryStream(document)); 
    else if (headerCode.StartsWith("D0CF11E0A1B11AE1")) 
        //! The document is DOC 
        DocFormatProvider docFormatProvider = new DocFormatProvider(); 
        RadFlowDocument docDocument = docFormatProvider.Import(document); 

Getting document header

private static string GetHeaderInfo(byte[] documentData) 
    byte[] buffer = documentData.Take(8).ToArray(); 
    StringBuilder sb = new StringBuilder(); 
    foreach (byte b in buffer) 
    return sb.ToString(); 
In this article