DOC To Other Formats

This example demonstrates how to convert a Word document to a different format. Select a Word document to convert and click on the Convert button.

Source DOC or DOCX file:

We use word documents quite often during work. Sometime we require word content in different file formats depending on different application areas.

Firstly, browser which is the primary word file whose content need to be converted to another file format

Secondly, choose the target file format. It supports the following:

  • Word Doc
  • Word Docx
  • Word ML
  • RTF
  • MS Text format
  • E-book format
  • DLS based xml file format
  • HTML

After clicking a generate button will launch a file with above choosen version of word , this file have content of selected 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 DOCToOtherFormatsExample : WordExampleBase
#if WEB
, IUIExample
#endif
    {
        public string FilePath;

        public DOCToOtherFormatsExample()
            : base(null,null)
        {

        }

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

        }

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

        }

        public override string Execute()
        {
#if WEB
            ProcessForm();
#endif
            if (string.IsNullOrWhiteSpace(FilePath))
            {
                ShowError("Please specify the Word file to convert.");
                return null;
            }

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

            string fileName = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + GetExtension(SaveAsFormat));
            // Save the document.
            document.Save(fileName, SaveAsFormat);

            document.Close();
            
            return fileName;
        }

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

#if WEB
        protected internal override IList> SaveAsFormats
        {
            get
            {
                return new List>()
                {
                    new Tuple("Doc", "DOC format"),
                    new Tuple("Docx", "DOCX format"),
                    new Tuple("WordML", "WordML format"),

                    new Tuple("Rtf", "Rtf format"),
                    new Tuple("Txt", "Txt format"),
                    new Tuple("EPub", "EPub format"),
                    new Tuple("Xml", "Xml format"),
                    new Tuple("Html", "Html format"),
                    new Tuple("Odt", "Odt format"),
                };
            }
        }

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

Imports OfficeComponent.Word


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

		Public Sub New()
			MyBase.New(Nothing,Nothing)

		End Sub

		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(FilePath) Then
				ShowError("Please specify the Word file to convert.")
				Return Nothing
			End If

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

			Dim fileName As String = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & GetExtension(SaveAsFormat))
			' Save the document.
			document.Save(fileName, SaveAsFormat)

			document.Close()

			Return fileName
		End Function

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

#If WEB Then
		Protected Friend Overrides ReadOnly Property SaveAsFormats() As IList(Of Tuple(Of String, String))
			Get
'INSTANT VB TODO TASK: This type of object initializer has no direct VB equivalent prior to VB10:
				Return New List(Of Tuple(Of String, String))() { New Tuple(Of String, String)("Doc", "DOC format"), New Tuple(Of String, String)("Docx", "DOCX format"), New Tuple(Of String, String)("WordML", "WordML format"), New Tuple(Of String, String)("Rtf", "Rtf format"), New Tuple(Of String, String)("Txt", "Txt format"), New Tuple(Of String, String)("EPub", "EPub format"), New Tuple(Of String, String)("Xml", "Xml format"), New Tuple(Of String, String)("Html", "Html format"), New Tuple(Of String, String)("Odt", "Odt format") }
			End Get
		End Property

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