Convert Word document to EPUB

24 Jul 20265 minutes to read

Convert a Word document to the EPUB v2.0 file format with a few lines of code by using Essential® DocIO.

Assemblies and NuGet packages required

Refer to the following links for the assemblies and NuGet packages required based on your platform to convert a Word document to an EPUB file using the .NET Word Library (DocIO).

Word to EPUB conversion is supported only in Windows Forms, UWP, WPF, ASP.NET Web, and ASP.NET MVC.

The following code illustrates how to convert the Word document to an EPUB file.

//DocIO supports Word to EPUB in Windows Forms, UWP, WPF, ASP.NET Web, and MVC platforms alone
//Loads an existing document
WordDocument document = new WordDocument("Template.docx", FormatType.Docx);
//Exports the fonts used in the document
document.SaveOptions.EPubExportFont = true;
//Exports header and footer
document.SaveOptions.HtmlExportHeadersFooters = true;
//Saves the document as EPub file
document.Save("WordToEPub.epub", FormatType.EPub);
//Closes the document
document.Close();
'Loads an existing document
Dim document As New WordDocument("Template.docx", FormatType.Docx)
'Exports the fonts used in the document
document.SaveOptions.EPubExportFont = True
'Exports header and footer
document.SaveOptions.HtmlExportHeadersFooters = True
'Saves the document as EPub file
document.Save("WordToEPub.epub", FormatType.EPub)
'Closes the document
document.Close()
//Loads the template document 
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
WordDocument document = new WordDocument(assembly.GetManifestResourceStream("Sample.Assets.Template.docx"), FormatType.Docx);
//Exports the fonts used in the document
document.SaveOptions.EPubExportFont = true;
//Exports header and footer
document.SaveOptions.HtmlExportHeadersFooters = true;
//Saves the document as EPub file
MemoryStream stream = new MemoryStream();
await document.SaveAsync(stream, FormatType.EPub);
//Saves the stream as Word file in local machine
Save(stream, "WordToEPub.epub");
//Closes the document
document.Close();

//Saves the EPUB file using a file picker
async void Save(MemoryStream streams, string filename)
{
    streams.Position = 0;
    StorageFile stFile;
    if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
    {
        FileSavePicker savePicker = new FileSavePicker();
        savePicker.DefaultFileExtension = ".epub";
        savePicker.SuggestedFileName = filename;
        savePicker.FileTypeChoices.Add("Word Documents", new List<string>() { ".epub" });
        stFile = await savePicker.PickSaveFileAsync();
    }
    else
    {
        StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
        stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
    }
    if (stFile != null)
    {
        using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
        {
            //Writes compressed data from memory to the file
            using (Stream outstream = zipStream.AsStreamForWrite())
            {
                byte[] buffer = outputStream.ToArray();
                outstream.Write(buffer, 0, buffer.Length);
                outstream.Flush();
            }
        }
    }
    //Launches the saved EPUB file in the default reader
    await Windows.System.Launcher.LaunchFileAsync(stFile);
}

You can download a complete working sample from GitHub.

NOTE

Word to EPUB conversion is supported only in Windows Forms, UWP, WPF, ASP.NET Web, and MVC platforms.

The following elements are supported in Word to EPUB conversion.

  • Text and Paragraph Formatting
  • Lists
  • Tables
  • Images
  • Footnote
  • Hyperlink
  • Styles
  • Table of Contents
  • Document Properties

Frequently Asked Questions

See also