New to Telerik Document ProcessingStart a free 30-day trial

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

Updated on Jun 5, 2026
Product VersionProductAuthor
2022.1.217WordsProcessingMartin Velikov

Description

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

Solution

The following example demonstrates how to read two documents with ".doc" filename extensions that have different actual document types. The StringBuilder class creates the document signature (header) string. You can then compare the signature with predefined values to determine which format provider to use for importing the document.

Example 1: Identify Document Type by File Signature

csharp

	List<byte[]> documents = new List<byte[]>();
	documents.Add(File.ReadAllBytes("rtf.doc"));
	documents.Add(File.ReadAllBytes("doc.doc"));

	foreach (byte[] document in documents)
	{
		string headerCode = GetHeaderInfo(document).ToUpper();

		//! The signatures are taken from: https://www.filesignatures.net/index.php?page=search
		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);
		}
	}

Example 2: Get Document Header

csharp

	private static string GetHeaderInfo(byte[] documentData)
	{
		byte[] buffer = documentData.Take(8).ToArray();

		StringBuilder sb = new StringBuilder();
		foreach (byte b in buffer)
		{
			sb.Append(b.ToString("X2"));
		}

		return sb.ToString();
	}

See Also

In this article
DescriptionSolutionSee Also
Not finding the help you need?
Contact Support