Convert Markdown to XPS | C#

Markdown is a markup language with a plain-text-formatting syntax. Markdown is often used as a format for documentation and readme files since it allows writing in an easy-to-read and easy-to-write style. Its design allows it to be easily converted to many output formats, but initially, it was created to convert only to HTML. Using the Aspose.HTML class library in your C# application, you can easily convert Markdown into an XPS file with just a few lines of code!

Online Markdown Converter

You can convert Markdown to other formats with Aspose.HTML for .NET API in real time. Please load a Markdown file from the local file system, select the output format and run the example. The save options are set by default. You will immediately receive the conversion result as a separate file.

                
            

If you want to convert Markdown to XPS programmatically, please see the following C# code examples.

Convert Markdown to XPS in C#

Conversions from Markdown to other formats go through the Markdown to HTML conversion stage. If your scenario is required rendering Markdown document, for instance, to the XPS file format, the following example demonstrates how that is simple:

  1. Prepare a source Markdown document. In the example, we create a Markdown file from code.
  2. Prepare a path for converted file saving.
  3. Convert Markdown to HTML. Use the ConvertMarkdown(sourcePath) method to save Markdown as an HTML document.
  4. Use the ConvertHTML() method to render the intermediary HTML document to XPSformat. You need to pass the HTMLDocument, XpsSaveOptions, and output file path to the ConvertHTML() method for HTML to XPS conversion.

If your case is to create a Markdown document from a user string directly in your code and convert it to an XPS file, the following example could help you:

 1using System.IO;
 2using Aspose.Html.Converters;
 3using Aspose.Html.Saving;
 4...
 5    // Prepare a path to a source Markdown file
 6     string sourcePath = Path.Combine(OutputDir, "document.md");
 7
 8     // Prepare a simple Markdown example
 9     var code = "### Hello, World!" +
10                "\r\n" +
11                "[visit applications](https://products.aspose.app/html/applications)";
12     // Create a Markdown file
13     File.WriteAllText(sourcePath, code);
14
15     // Prepare a path for converted file saving 
16     string savePath = Path.Combine(OutputDir, "document-output.xps");
17
18     // Convert Markdown to HTML 
19     using var document = Converter.ConvertMarkdown(sourcePath);
20
21     // Convert HTML to XPS file format
22     Converter.ConvertHTML(document, new XpsSaveOptions(), savePath);

You can download the complete examples and data files from GitHub.

Convert Markdown to XPS using XpsSaveOptions

If your case is to convert an existing Markdown document from a local file system, the following example could help you. You need to follow a few steps:

  1. Open an existing Markdown document. In the example, we load a Markdown file from a local file system ( nature.md).
  2. Prepare a path for converted XPS file saving.
  3. Convert Markdown to HTML. Use the ConvertMarkdown(sourcePath) method of the Converter class to save Markdown as an HTML document.
  4. Create a new XpsSaveOptions object and specify the required properties.
  5. Use the ConvertHTML() method for HTML to XPS conversion. You need to pass the HTMLDocument, XpsSaveOptions, and output file path to the ConvertHTML() method.

The following code snippet shows how to convert Markdown to XPS using Aspose.HTML for .NET.

 1using System.IO;
 2using Aspose.Html.Converters;
 3using Aspose.Html.Saving;
 4using Aspose.Html.Drawing;
 5using System.Drawing;
 6...
 7    // Prepare a path to a source Markdown file
 8    string sourcePath = Path.Combine(DataDir, "nature.md");
 9
10    // Prepare a path for converted PDF file saving 
11    string savePath = Path.Combine(OutputDir, "nature-output.xps");
12
13    // Convert Markdown to HTML
14    using var document = Converter.ConvertMarkdown(sourcePath);
15
16    // Initialize XpsSaveOptions. Set up the resilutions, page-size, margins and change the background color to AntiqueWhite 
17    var options = new XpsSaveOptions()
18    {
19        HorizontalResolution = 200,
20        VerticalResolution = 200,
21        BackgroundColor = System.Drawing.Color.AntiqueWhite
22    };
23    options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(Length.FromInches(5.0f), Length.FromInches(10.0f)), new Margin(30, 20, 10, 10));
24
25    // Convert the HTML document to XPS file format
26    Converter.ConvertHTML(document, options, savePath);

The XpsSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the process of converting Markdown to XPS format. To learn more about XpsSaveOptions, please read the Fine-Tuning Converters article.

In the example, we use:

You can check the quality of Markdown to XPS conversion with our online MD to XPS Converter. Upload, convert your files and get results in a few seconds. Try our forceful Markdown to XPS Converter for free now!

Text “Banner MD to XPS Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.