Convert a Document using Aspose.Words


Converting documents from one format to another is one of the main features of Aspose.Words. Conversion in Aspose.Words consists of opening a document in one of the supported load formats and then saving it in another format.

Aspose.Words supports almost all common conversion combinations, for example:

  • DOC, DOCX, RTF to PDF or XPS,
  • DOCX to Markdown or HTML,
  • PDF to DOC, DOCX, or other Word formats,
  • and others.

Please note that this is not a complete list of possible conversions. You can check out LoadFormat that specifies all load or import formats and SaveFormat that specifies all save or export formats supported by Aspose.Words.

How to Convert a Document

First, you need to load a document into Aspose.Words. Then you need to save this document in the required format. Follow these two simple steps and you will find out how easily Aspose.Words allows you to convert documents.

Note that you can specify additional options when saving a document to get the desired result. We will also discuss it below.

How to Load a Document

To load a document pass the file name or the stream into one of the Document constructors. The format of the loaded document is determined automatically most of the times.

The following example shows how to load a document from a file:

// Load the document from the absolute path on disk.
Document doc = new Document(dataDir + "TestDocument.docx");

How to Save a Document

To save your document, use one of the Save methods. Aspose.Words automatically determines the desired save format from the file extension. You can save a document both to a file or a stream. For example, you can save a DOCX document as a PDF file.

The following example shows how to save a document to a file:

// Save the document as a PDF file.
doc.Save(dataDir + "Document.pdf");

Specify Advanced Conversion Options

When converting a document, you can set some advanced properties. Aspose.Words provides you with the SaveOptions class, which allows you to control the process of saving your document in the required format more precisely. Each save format has a corresponding class that holds the save options for this save format. For example, there is HtmlSaveOptions for saving to HTML/MHTML/EPUB format, or PdfSaveOptions for saving to PDF. You can read more about advanced properties in the article ‘Specify Save Options’ in the Aspose.Words documentation.

The following example shows how to convert a document from DOCX to PDF and set Aspose.Words to embed full fonts in the output document:

// Load the document from the file.
Document doc = new Document(dataDir + "TestDocument.docx");

// Aspose.Words embeds full fonts by default when EmbedFullFonts is set to true.
// The property below can be changed each time a document is rendered.
PdfSaveOptions options = new PdfSaveOptions();
options.EmbedFullFonts = true;

// The output PDF will be embedded with all fonts found in the document.
doc.Save(dataDir + "TestDocument.EmbedFullFonts.pdf", options);

Variations of Conversions Supported by Aspose.Words

As mentioned above, Aspose.Words allows you to convert a document from one of most popular formats to another. This means that you can solve most of your conversion tasks using only Aspose.Words without installing special applications or tools. For example, you can easily and reliably convert a document from Microsoft Word formats such as DOC or DOCX to PDF or HTML, or from PDF or HTML to Markdown.

Study the ‘Converting a Document’ documentation section for more Aspose.Words conversion examples.

See Also

To understand this question in more detail, look at the following articles in the Aspose blog: