Get Document Format

This example demonstrates how to get document format

Source Word file:

Here we see file browser to select a file for purpose of detecting it file format

Basically it can detect following file formats

  • Microsoft Word 97-2003 document(.DOC)
  • Office Open XML WordprocessingML Macro-Free Document(.DOCX)
  • Microsoft Word 97-2003 template(.DOT)
  • Microsoft RTF document(.RTF)
  • Microsoft Word 2007 document
  • Microsoft Word 2010 document
  • Microsoft Word 2013 document
  • Microsoft Word 2016 document
  • and more

On clicking show button, It will populate format of choosed file.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;

using OfficeComponent.Word;


namespace OfficeComponent.Samples
{
    class GetDocumentFormatExample : WordExampleBase
#if WEB
, IUIExample
#endif
    {
        public string FileName;

        public GetDocumentFormatExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {

        }

        public GetDocumentFormatExample(string commonDataPath, string outputDir, string xmlFile)
            : base(commonDataPath, outputDir, xmlFile)
        {

        }

        public override string Execute()
        {
#if WEB
            ProcessForm();
#endif
            if (string.IsNullOrWhiteSpace(FileName))
            {
                ShowError("Please specify the input file name.");
                return null;
            }

            // Create a new instance of WordDocument class.
            WordDocument document = new WordDocument(FileName);

            string formatMessage;

            switch (document.ActualWordDocumentFormat)
            {
                case WordDocumentFormat.Doc:
                    formatMessage = "Microsoft Word 97-2003 document.";
                    break;

                case WordDocumentFormat.Docx:
                    formatMessage = "Office Open XML WordprocessingML Macro-Free Document.";
                    break;

                case WordDocumentFormat.Dot:
                    formatMessage = "Microsoft Word 97-2003 template.";
                    break;

                case WordDocumentFormat.Rtf:
                    formatMessage = "Microsoft RTF document.";
                    break;

                case WordDocumentFormat.Word2007:
                    formatMessage = "Microsoft Word 2007 document.";
                    break;

                case WordDocumentFormat.Word2010:
                    formatMessage = "Microsoft Word 2010 document.";
                    break;

                case WordDocumentFormat.Word2013:
                    formatMessage = "Microsoft Word 2013 document.";
                    break;

                default:
                    formatMessage = document.ActualWordDocumentFormat.ToString() + " document";
                    break;
            }

            ShowInfo(formatMessage);

            // Close the document.
            document.Close();

            return null;
        }

        public override string ActionTitle
        {
            get
            {
                return "Get";
            }
        }

#if WEB
        protected internal override bool SaveAsFormatVisible
        {
            get
            {
                return false;
            }
        }

        void ProcessForm()
        {
            FileName = GetPostFile("SourceFile");
        }
#endif
    }
}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text

Imports OfficeComponent.Word


Namespace OfficeComponent.Samples
#If WEB Then
	Friend Class GetDocumentFormatExample
		Inherits WordExampleBase
		Implements IUIExample
#Else
	Friend Class GetDocumentFormatExample
		Inherits WordExampleBase
#End If
		Public FileName As String

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
			MyBase.New(commonDataPath, outputDir)

		End Sub

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
			MyBase.New(commonDataPath, outputDir, xmlFile)

		End Sub

		Public Overrides Function Execute() As String
#If WEB Then
			ProcessForm()
#End If
			If String.IsNullOrWhiteSpace(FileName) Then
				ShowError("Please specify the input file name.")
				Return Nothing
			End If

			' Create a new instance of WordDocument class.
			Dim document As New WordDocument(FileName)

			Dim formatMessage As String

			Select Case document.ActualWordDocumentFormat
				Case WordDocumentFormat.Doc
					formatMessage = "Microsoft Word 97-2003 document."

				Case WordDocumentFormat.Docx
					formatMessage = "Office Open XML WordprocessingML Macro-Free Document."

				Case WordDocumentFormat.Dot
					formatMessage = "Microsoft Word 97-2003 template."

				Case WordDocumentFormat.Rtf
					formatMessage = "Microsoft RTF document."

				Case WordDocumentFormat.Word2007
					formatMessage = "Microsoft Word 2007 document."

				Case WordDocumentFormat.Word2010
					formatMessage = "Microsoft Word 2010 document."

				Case WordDocumentFormat.Word2013
					formatMessage = "Microsoft Word 2013 document."

				Case Else
					formatMessage = document.ActualWordDocumentFormat.ToString() & " document"
			End Select

			ShowInfo(formatMessage)

			' Close the document.
			document.Close()

			Return Nothing
		End Function

		Public Overrides ReadOnly Property ActionTitle() As String
			Get
				Return "Get"
			End Get
		End Property

#If WEB Then
		Protected Friend Overrides ReadOnly Property SaveAsFormatVisible() As Boolean
			Get
				Return False
			End Get
		End Property

		Private Sub ProcessForm()
			FileName = GetPostFile("SourceFile")
		End Sub
#End If
	End Class
End Namespace