Thursday, 07 November 2013 07:22

How to Convert Word to Emf in C#

The article will introduce an easy way to convert Word to Emf by a powerful and independent Word .NET component called Spire.Doc, without Microsoft Word installed on the machine. It also offers support for converting Word and HTML to frequently-used image formats like Jpeg, Png, Gif, Bmp and Tiff, etc. Just click here to have a try.

Emf is a file extension for Enhanced MetaFile, used as a graphics language for printer drivers by the Windows operating system. In 1993, a newer version with additional commands 32-bit version of Win32/GDI introduced the Enhanced Metafile (Emf). Microsoft also recommends enhanced-format (Emf) functions to be used instead of rarely being used Windows-format (WMF) functions.

Spire.Doc presents almost the easiest solution to convert Word to Emf through the following 5 lines simple code.

[C#]
using Spire.Doc;
using System.Drawing.Imaging;


namespace DOCEMF
{
    class Program
    {
        static void Main(string[] args)
        {
            // create an instance of Spire.Doc.Document
            Document doc = new Document();
            // load the file base on a specified file name
            doc.LoadFromFile(@"../../Original Word.docx", FileFormat.Docx);
            //convert the first page of document to image
            System.Drawing.Image image = doc.SaveToImages(0, Spire.Doc.Documents.ImageType.Metafile);
            // save the document object to Emf file
            image.Save(@"../../Convert Word to Image.emf", ImageFormat.Emf);
            //close the document
            doc.Close();

        }
    }
}

Check the effect screenshot below:

word to emf c#

Published in Conversion
Friday, 05 August 2022 06:45

C#/VB.NET: Convert Word to XPS

XPS (XML Paper Specification) is a fixed-layout document format designed to preserve document fidelity and provide device-independent document appearance. It is similar to PDF, but is based on XML rather than PostScript. If you want to save a Word document to a fixed-layout file format, XPS would be an option. This article will demonstrate how to convert Word documents to XPS in C# and VB.NET using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert Word to XPS in C# and VB.NET

The following are the detailed steps to convert a Word document to XPS using Spire.Doc for .NET:

  • Initialize an instance of Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Save the Word document to XPS using Document.SaveToFile(string filePath, FileFormat fileFormat) method.
  • C#
  • VB.NET
using Spire.Doc;
namespace ConvertWordToXps
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document doc = new Document();
            //Load a Word document
            doc.LoadFromFile("Sample.docx");
            //convert the document to XPS
            doc.SaveToFile("ToXPS.xps", FileFormat.XPS);
        }
    }
}

C#/VB.NET: Convert Word to XPS

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Conversion
Friday, 22 July 2022 03:32

C#/VB.NET: Convert Word to EPUB

EPUB is a standard file format for publishing eBooks or other electronic documents. The content in an EPUB file is reflowable, which means that the content automatically adjusts itself to fit the screen it is being displayed on. People who want to publish their eBooks may need to convert their works stored in Word documents to EPUB files. In this article, you will learn how to programmatically achieve this task using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert Word to EPUB

The detailed steps are as follows:

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Save the document to EPUB using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace WordtoEPUB
{
    class Epub
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load a sample Word document
            document.LoadFromFile("demo.docx");

            //Convert the Word document to EPUB
            document.SaveToFile("ToEpub.epub", FileFormat.EPub);
        }
    }
}

C#/VB.NET: Convert Word to EPUB

Convert Word to EPUB with a Cover Image

The detailed steps are as follows.

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Create a DocPicture instance, and then load an image using DocPicture.LoadImage() method.
  • Save the Word document to EPUB with cover image using Document.SaveToEpub(String, DocPicture) method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Fields;
using System.Drawing;

namespace ConvertWordToEpubWithCoverImage
{
    class Program
    {
        static void Main(string[] args)
        {

            //Create a Document instance
            Document doc = new Document();

            //Load a sample Word document
            doc.LoadFromFile("demo.docx");

            //Create a DocPicture instance
            DocPicture picture = new DocPicture(doc);

            //Load an image
            picture.LoadImage(Image.FromFile("CoverImage.png"));

            //Save the Word document to EPUB with cover image
            doc.SaveToEpub("ToEpubWithCoverImage.epub", picture);
        }
    }
}

C#/VB.NET: Convert Word to EPUB

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Conversion
Monday, 24 March 2025 06:15

C#: Convert HTML to Image

Converting HTML to images enables the transformation of dynamic web content-such as text, graphics, and layouts-into static formats like PNG or JPEG. This process is ideal for capturing web pages for documentation, generating thumbnails, or ensuring visually consistent content across platforms, providing both accuracy and versatility.

In this article, you'll learn how to convert HTML files and strings to images using C# with Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert an HTML File to Image in C#

Using Spire.Doc for .NET, you can directly load an HTML file by utilizing the Document.LoadFromFile() method. Once loaded, you can convert the document into Bitmap images with the Document.SaveToImages() method. Afterward, you can loop through the generated images and save each one in widely-used image formats such as PNG, JPG, or BMP.

The following are the steps to convert an HTML file to images using Spire.Doc in C#:

  • Create a Document object.
  • Load an HTML file using the Document.LoadFromFile() method.
  • Adjust properties such as margins, which will affect the output image's layout.
  • Call the Document.SaveToImages() method to convert the loaded document into an array of Bitmap images.
  • Iterate through the images and save each one to your desired output format.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using System.Drawing.Imaging;

namespace ConvertHtmlFileToPng
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Document object
            Document document = new Document();

            // Load an HTML file
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\MyHtml.html", FileFormat.Html, XHTMLValidationType.None);

            // Get the first section
            Section section = document.Sections[0];

            // Set the page margins
            section.PageSetup.Margins.All = 2;

            // Convert the document to an array of bitmap images
            Image[] images = document.SaveToImages(ImageType.Bitmap);

            // Iterate through the images
            for (int index = 0; index < images.Length; index++)
            {
                // Specify the output file name
                string fileName = string.Format(@"C:\Users\Administrator\Desktop\Output\image_{0}.png", index);

                // Save each image as a PNG file
                images[index].Save(fileName, ImageFormat.Png);
  
            }

            // Dispose resources
            document.Dispose();
        }
    }
}

Convert an HTML file to images in C#

Convert an HTML String to Image in C#

In certain scenarios, you might need to convert an HTML string directly into an image. This approach is especially beneficial for handling dynamically generated content or when you prefer not to depend on external HTML files.

Here is how you can convert an HTML string to images using Spire.Doc in C#:

  • Create a Document object.
  • Add a section and a paragraph to the document.
  • Adjust properties such as margins, which will affect the output image's layout.
  • Read the HTML string from a file or define it directly in the script.
  • Use the Paragraph.AppendHTML() method to render the HTML content in the document.
  • Call the Document.SaveToImages() method to convert the document into an array of Bitmap images.
  • Iterate through the images and save each one to your desired output format.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using System.Drawing.Imaging;

namespace ConvertHtmlStringToPng
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Document object
            Document document = new Document();

            // Add a section to the document
            Section section = document.AddSection();

            // Set the page margins
            section.PageSetup.Margins.All = 2;

            // Add a paragraph to the section
            Paragraph paragraph = section.AddParagraph();

            // Read HTML string from a file 
            string htmlFilePath = @"C:\Users\Administrator\Desktop\Html.html";
            string htmlString = File.ReadAllText(htmlFilePath, System.Text.Encoding.UTF8);

            // Append the HTML string to the paragraph
            paragraph.AppendHTML(htmlString);

            // Convert the document to an array of bitmap images
            Image[] images = document.SaveToImages(ImageType.Bitmap);

            // Iterate through the images
            for (int index = 0; index < images.Length; index++)
            {
                // Specify the output file name
                string fileName = string.Format(@"C:\Users\Administrator\Desktop\Output\image_{0}.png", index);

                // Save each image as a PNG file
                images[index].Save(fileName, ImageFormat.Png);

            }

            // Dispose resources
            document.Dispose();
        }
    }
}

Convert an HTML string to images in C#

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Conversion
Wednesday, 26 June 2024 02:30

C#: Convert Word to TIFF

Converting Word to TIFF can be useful in various scenarios. TIFF files have high quality and wide support, making them versatile for sharing documents. The conversion also "flattens" the Word document, preserving the layout so it appears exactly as the original. This can be helpful when the document needs to be incorporated into another application or workflow that requires image-based files.

In this article, you will learn how to convert Word to TIFF using C# and the Spire.Doc for .NET library.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert Word to TIFF in C#

Spire.Doc for .NET provides the Document.SaveToImages() method, which enables developers to convert an entire document into an array of images. Subsequently, these individual images can be combined into a single TIFF image using the built-in .NET library.

The steps to convert Word to TIFF using C# are as follows.

  • Create a Document object.
  • Load a Word document using Document.LoadFile() method.
  • Convert the document into an array of images using Document.SaveToImages() method.
  • Combine these images into a single TIFF file using the custom method ConvertImagesToTiff().
  • C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using System.Drawing.Imaging;

namespace WordToTiff
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Document object
            Document doc = new Document();

            // Load a Word document
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx");

            // Convert the whole document to images
            Image[] images = doc.SaveToImages(ImageType.Bitmap);

            // Convert multiple images into a TIFF file
            ConvertImagesToTiff(images, "ToTiff.tiff", EncoderValue.CompressionLZW);

            // Dispose resource
            doc.Dispose();
        }

        private static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            // Get the image encoders
            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            for (int j = 0; j < encoders.Length; j++)
            {
                // Find the encoder that matches the specified MIME type
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
        }

        public static void ConvertImagesToTiff(Image[] images, string outFile, EncoderValue compressEncoder)
        {
            // Set the encoder parameters 
            Encoder enc = Encoder.SaveFlag;
            EncoderParameters ep = new EncoderParameters(2);
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
            ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);

            // Get the first image
            Image pages = images[0];

            // Create a variable
            int frame = 0;

            // Get an ImageCodecInfo object for processing TIFF image codec information
            ImageCodecInfo info = GetEncoderInfo("image/tiff");

            // Iterate through each Image
            foreach (Image img in images)
            {
                // If it's the first frame, save it to the output file with specified encoder parameters
                if (frame == 0)
                {
                    pages = img;
                    pages.Save(outFile, info, ep);
                }
                else
                {
                    // Save the intermediate frames
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

                    pages.SaveAdd(img, ep);
                }

                // If it's the last frame, flush the encoder parameters and close the file
                if (frame == images.Length - 1)
                {
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                    pages.SaveAdd(ep);
                }
                frame++;
            }
        }
    }
}

C#: Convert Word to TIFF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Conversion
Tuesday, 10 September 2024 02:49

C#: Convert RTF to HTML, Image

RTF, or Rich Text Format, is a file format designed for storing text and formatting information. While processing RTF files, sometimes you might need to convert them into a more web-friendly format such as HTML, or convert to images for better sharing and archiving purposes. In this article, you will learn how to convert RTF to HTML or images in C# using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert RTF to HTML in C#

Converting RTF to HTML ensures that the document can be easily viewed and edited in any modern web browser without requiring any additional software.

With Spire.Doc for .NET, you can achieve RTF to HTML conversion through the Document.SaveToFile(string fileName, FileFormat.Html) method. The following are the detailed steps.

  • Create a Document instance.
  • Load an RTF document using Document.LoadFromFile() method.
  • Save the RTF document in HTML format using Document.SaveToFile(string fileName, FileFormat.Html) method.
  • C#
using Spire.Doc;

namespace ConvertRtfToHtml
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load an RTF document 
            document.LoadFromFile("input.rtf");

            //Save as HTML format
            document.SaveToFile("RtfToHtml.html", FileFormat.Html);
        }
    }
}

C#: Convert RTF to HTML, Image

Convert RTF to Image in C#

To convert RTF to images, we can use the Document.SaveToImages() method to convert an RTF file into individual Bitmap or Metafile images. Then, the Bitmap or Metafile images can be saved as a BMP, EMF, JPEG, PNG, GIF, or WMF format files. The following are the detailed steps.

  • Create a Document object.
  • Load an RTF document using Document.LoadFromFile() method.
  • Convert the document to images using Document.SaveToImages() method.
  • Iterate through the converted image, and then save each as a PNG file using Image[].Save(string fileName, ImageFormat format) method.
  • C#
using Spire.Doc;
using System.Drawing.Imaging;
using System.Drawing;
using Spire.Doc.Documents;

namespace ConvertRtfToImage

{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load an RTF document 
            document.LoadFromFile("input.rtf");

            //Convert the RTF document to images
            Image[] images = document.SaveToImages(ImageType.Bitmap);

            // Iterate through the image collection
            for (int i = 0; i < images.Length; i++)
            {
                //Save the image as png format
                string outputfile = string.Format("image-{0}.png", i);
                images[i].Save(outputfile, ImageFormat.Png);
            }
        }
    }
}

C#: Convert RTF to HTML, Image

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Conversion

Rich Text Format (RTF) is a proprietary text file format which can serve as an exchange format between word processing programs from different manufacturers on different operating systems. Rich text documents include page formatting options, such as custom page margins, line spacing, and tab widths. With rich text, it's easy to create columns, add tables, and format your documents in a way that makes them easier to read. This article will demonstrate how to convert Word Doc/Docx to RTF files and convert RTF to Word Doc/Docx with the help of Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert Word Doc/Docx to RTF

The following steps show you how to convert Word to RTF using Spire.Doc for .NET.

  • Create a Document instance.
  • Load a Word sample document using Document.LoadFromFile() method.
  • Save the document as an RTF file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace ConvertToRtf
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Word document instance
            Document document = new Document();

            //Load a Word sample document
            document.LoadFromFile("sample.docx");

            // Save the document to RTF
            document.SaveToFile("Result.rtf", FileFormat.Rtf);
        }

    }
}

C#/VB.NET: Convert RTF to Word Doc/Docx and Vice Versa

Convert RTF to Word Doc/Docx

The steps to convert an RTF document to Word Doc/Docx is very similar with that of the above example:

  • Create a Document instance.
  • Load an RTF document using Document.LoadFromFile() method.
  • Save the RTF document to Doc/Docx format using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using System;

public class RtfToDocDocx
{

    public static void Main(String[] args)
    {
        // Create a Document instance
        Document document = new Document();
        // Load an RTF document
        document.LoadFromFile("input.rtf", FileFormat.Rtf);
        // Save the document to Doc
        document.SaveToFile("toDoc.doc", FileFormat.Doc);
        // Save the document to Docx
        document.SaveToFile("toDocx.docx", FileFormat.Docx2013);
    }
}

C#/VB.NET: Convert RTF to Word Doc/Docx and Vice Versa

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Conversion

OpenXML is an XML-based file format for MS Office documents. OpenXML is popular among developers as it allows creating and editing Office documents, such Word documents, with no need for MS Office. But when presented to ordinary users, OpenXML files usually need to be converted to Word documents to facilitate reading and editing. In this article, you can learn how to convert XML files to Word documents or Word documents to XML files with the help of Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert an OpenXML File to a Word Document

The detailed steps of the conversion are as follows:

  • Create an object of Document class.
  • Load an OpenXML file from disk using Document.LoadFromFile() method.
  • Convert the OpenXML file to a Word document and save it using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using System;

namespace DocExample
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create a Document class instance
            Document document = new Document();

            //Load an OpenXML file from disk
            document.LoadFromFile(@"C:\Samples\Sample.xml", FileFormat.Xml);

            //Convert the OpenXML file to a Word document and save it
            document.SaveToFile("OpenXMLToWord.docx", FileFormat.Docx2013);
        }
    }
}

C#/VB.NET: Convert OpenXML to Word or Word to OpenXML

Convert a Word Document to an OpenXML File

The detailed steps of the conversion are as follows:

  • Create an object of Document class.
  • Load a Word document from disk using Document.LoadFromFile() method.
  • Convert the Word document to an OpenXML file and save it using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using System;

namespace DocExample
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create a Document class instance
            Document document = new Document();

            //Load a Word document from disk
            document.LoadFromFile(@"C:\Samples\Sample.docx");

            //Convert the Word document to an OpenXML file and save it
            //Change WordXML to WordML to convert to ML file
            document.SaveToFile("WordToOpenXMl.xml", FileFormat.WordXml);
        }
    }
}

C#/VB.NET: Convert OpenXML to Word or Word to OpenXML

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Conversion

Convert HTML to Word and Word to HTML using C# .NET

Microsoft Word and HTML (Hypertext Markup Language) are two of the most widely used formats worldwide. Microsoft Word is the go-to solution for crafting rich, feature-packed documents such as reports, proposals, and print-ready files, while HTML is the foundational language that powers content on the web. Understanding how to effectively convert between these formats can enhance document usability and accessibility.

In this article, we will provide a detailed step-by-step guide on converting HTML to Word and Word to HTML in .NET using C#. It covers the following topics:

Why Convert Between Word and HTML?

Before diving into the technical details, let's understand why you might need to convert between Word and HTML:

  • Cross-Platform Accessibility: HTML is the backbone of web pages, while Word documents are industry-standard for creating, sharing and editing content. Converting between them enables content to be accessible and editable across different platforms.
  • Rich Formatting: Word documents support complex formatting and elements; converting HTML to Word lets users retain formatting when exporting web content.
  • Document Archiving and Data Exchange: Archive HTML content as Word or publish Word-based reports to the web.

.NET Word Library Installation

The .NET framework does not natively support HTML or Word conversions. To bridge this gap, Spire.Doc for .NET provides a powerful, developer-friendly API for document creation, manipulation, and conversion—without requiring Microsoft Office or Interop libraries.

Install Spire.Doc for .NET

Before getting started with the conversion, you need to install Spire.Doc for .NET through one of the following methods:

Method 1: Install via NuGet

Run the following command in the NuGet Package Manager Console:

Install-Package Spire.Doc

Method 2: Manually Add the DLLs

You can also download the Spire.Doc for .NET package, extract the files, and then reference Spire.Doc.dll manually in your Visual Studio project.

How to Convert HTML to Word Using C#

Spire.Doc enables you to load HTML files or HTML strings and save them as Word documents. Let’s see how to implement these conversions.

Convert HTML String to Word

To convert an HTML string to Word format, follow these steps:

  • Create a Document Object: Instantiate a new Document object.
  • Add a Section and Paragraph: Create a section in the document and add a paragraph.
  • Append HTML String: Use the Paragraph.AppendHTML() method to include the HTML content.
  • Save the Document: Save the document using Document.SaveToFile() with the desired format (e.g., Docx).

Example code

using Spire.Doc;
using Spire.Doc.Documents;
using System.IO;

namespace ConvertHtmlStringToWord
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Document object
            Document document = new Document();

            // Add a section to the document
            Section section = document.AddSection();

            // Set the page margins
            section.PageSetup.Margins.All = 2;

            // Add a paragraph to the section
            Paragraph paragraph = section.AddParagraph();

            // Read HTML string from a file
            string htmlFilePath = @"C:\Users\Administrator\Desktop\Html.html";
            string htmlString = File.ReadAllText(htmlFilePath, System.Text.Encoding.UTF8);

            // Append the HTML string to the paragraph
            paragraph.AppendHTML(htmlString);

            // Save the document to a Word file
            document.SaveToFile("AddHtmlStringToWord.docx", FileFormat.Docx);

            // Dispose resources
            document.Dispose();
        }
    }
}

Convert HTML String to Word using C# .NET

Convert HTML File to Word

If you have existing HTML files, converting them to Word is straightforward. Here’s how to do that:

  • Create a Document Object: Instantiate a new Document object.
  • Load the HTML File: Use Document.LoadFromFile() to load the HTML file.
  • Save as Word Format: Save the document using Document.SaveToFile() with the desired format (e.g., Docx).

Example Code

using Spire.Doc;

namespace ConvertHtmlToWord
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Document object
            Document document = new Document();
            // Load the HTML file
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\MyHtml.html", FileFormat.Html);

            // Save the file as a Word document
            document.SaveToFile("HtmlToWord.docx", FileFormat.Docx);

            // Dispose resources
            document.Dispose();
        }
    }
}

Convert HTML File to Word using C# .NET

How to Convert Word to HTML Using C#

Spire.Doc also supports exporting Word documents (such as .docx and .doc) to HTML format. You can perform basic conversion with default behavior, or customize the output using advanced settings.

Basic Word to HTML Conversion

To convert a Word document to an HTML file using default settings, follow these steps:

  • Create a Document Object: Instantiate a new Document object.
  • Load the Word Document: Use Document.LoadFromFile() to load the Word document.
  • Save as HTML File: Save the document using Document.SaveToFile() with HTML as the format.

Example Code

using Spire.Doc;

namespace BasicWordToHtmlConversion
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Document object
            Document document = new Document();
            // Load the Word document
            document.LoadFromFile("input.docx");

            // Save the document as an HTML file
            document.SaveToFile("BasicWordToHtmlConversion.html", FileFormat.Html);

            // Dispose resources
            document.Dispose();
        }
    }
}

Advanced Word to HTML Conversion Settings

To tailor the conversion process, use the HtmlExportOptions class, which allows you to adjust a variety of settings, including:

  • Whether to export the document's styles.
  • Whether to embed images in the converted HTML.
  • Whether to export headers and footers.
  • Whether to export form fields as text.

Follow these steps to convert a Word document to HTML with customized options:

  • Create a Document Object: Instantiate a new Document object.
  • Load the Word Document: Use Document.LoadFromFile() to load the Word document.
  • Get HtmlExportOptions: Access the HtmlExportOptions through Document.HtmlExportOptions.
  • Customize Conversion Settings: Modify the properties of HtmlExportOptions to customize the conversion.
  • Save as HTML File: Save the document using Document.SaveToFile() with HTML as the format.

Example Code

using Spire.Doc;

namespace AdvancedWordToHtmlConversion
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile("sample.docx");

            HtmlExportOptions htmlExportOptions = doc.HtmlExportOptions;
            // Set whether to export the document styles
            htmlExportOptions.IsExportDocumentStyles = true;
            // Set whether to embed the images in the HTML
            htmlExportOptions.ImageEmbedded = true;
            // Set the type of the CSS style sheet
            htmlExportOptions.CssStyleSheetType = CssStyleSheetType.Internal;
            // Set whether to export headers and footers
            htmlExportOptions.HasHeadersFooters = true;
            // Set whether to export form fields as text
            htmlExportOptions.IsTextInputFormFieldAsText = false;

            // Save the document as an HTML file
            doc.SaveToFile("AdvancedWordToHtmlConversion.html", FileFormat.Html);
            doc.Close();
        }
    }
}

Conclusion

Converting HTML to Word and Word to HTML using C# and the Spire.Doc library is a seamless process that enhances document management and accessibility. By following the detailed steps outlined in this tutorial, developers can easily implement these conversions in their applications, improving workflow and productivity.

FAQs

Q1: Is it possible to batch convert multiple Word files to HTML using C#?

A1: Yes, you can loop through a list of Word files and apply the conversion logic in your C# code.

Q2: What types of HTML elements are supported during conversion to Word?

A2: Spire.Doc supports a wide range of HTML elements, including text, tables, images, lists, and more. However, certain elements not supported by Microsoft Word may also not be rendered correctly in Spire.Doc.

Q3: Can I convert formats other than HTML and Word?

A3: Yes. Spire.Doc supports various file format conversions, such as Word to PDF, Markdown to Word, Word to Markdown, RTF to Word, RTF to PDF.

Q4: Is Spire.Doc free to use?

A4: Spire.Doc offers a free version for lightweight use, but for extensive features and commercial use, a licensed version is recommended.

Get a Free License

To fully experience the capabilities of Spire.Doc for .NET without any evaluation limitations, you can request a free 30-day trial license.

Published in Conversion
Sunday, 27 March 2022 06:14

C#/VB.NET: Convert Word to HTML

When you'd like to put a Word document on the web, it's recommended that you should convert the document to HTML in order to make it accessible via a web page. This article will demonstrate how to convert Word to HTML programmatically in C# and VB.NET using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert Word to HTML

The following steps show you how to convert Word to HTML using Spire.Doc for .NET.

  • Create a Document instance.
  • Load a Word sample document using Document.LoadFromFile() method.
  • Save the document as an HTML file using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace WordToHTML
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document mydoc = new Document();

            //Load a Word document
            mydoc.LoadFromFile("sample.docx");

            //Save to HTML
            mydoc.SaveToFile("WordToHTML.html", FileFormat.Html);
        }
    }
}

C#/VB.NET: Convert Word to HTML

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Conversion
Page 3 of 4