Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To convert MHTML to XPS in C#, open the MHTML file as a stream, create XpsSaveOptions, and call Converter.ConvertMHTML() with the stream, options, and output path or stream provider.
MHTML stores a complete web page and its resources in one archive. XPS is a fixed-layout document format useful for Windows-oriented viewing, printing, and internal document workflows when the target pipeline expects XPS output.
Before running the examples, install and configure Aspose.HTML for .NET in your project. The complete C# example project is available in the Aspose.HTML for .NET GitHub repository.
The static
Converter class provides the shortest path from an MHTML stream to an XPS file. The example below opens an MHTML file and converts it to XPS with default XpsSaveOptions.
1// Convert MHTML to XPS using C#
2
3// Open an existing MHTML file for reading
4using FileStream stream = File.OpenRead(DataDir + "sample.mht");
5
6// Invoke the ConvertMHTML() method to convert the MHTML code to XPS
7Converter.ConvertMHTML(stream, new XpsSaveOptions(), Path.Combine(OutputDir, "convert-by-two-lines.xps"));Use this workflow when the MHTML source exists on disk and the XPS should be saved to a file path.
The workflow is:
File.OpenRead() or another readable stream.XpsSaveOptions instance.Converter.ConvertMHTML(stream, options, savePath). 1// Convert MHTML to XPS in C#
2
3// Open an existing MHTML file for reading
4using FileStream stream = File.OpenRead(DataDir + "sample.mht");
5
6// Prepare a path for converted file saving
7string savePath = Path.Combine(OutputDir, "sample-output.xps");
8
9// Create an instance of XpsSaveOptions
10XpsSaveOptions options = new XpsSaveOptions();
11
12// Call the ConvertMHTML() method to convert MHTML to XPS
13Converter.ConvertMHTML(stream, options, savePath);Use XpsSaveOptions when the generated XPS file needs custom rendering settings.
To customize MHTML to XPS conversion:
XpsSaveOptions.PageSetup, BackgroundColor, CSS processing, or resolution.Converter.ConvertMHTML().| Option | Use it to control |
|---|---|
PageSetup | Page size, margins, and page layout settings. |
Css | CSS processing behavior through CssOptions. |
BackgroundColor | The color used to fill the page background. The default value is transparent. |
HorizontalResolution and VerticalResolution | Output resolution values used by the rendering process. The default value is 300 dpi for each property. |
1// Convert MHTML to XPS in C# with custom settings
2
3// Open an existing MHTML file for reading
4using FileStream stream = File.OpenRead(DataDir + "sample.mht");
5
6// Prepare a path for converted file saving
7string savePath = Path.Combine(OutputDir, "sample-options.xps");
8
9// Create an instance of XpsSaveOptions. Set up the page-size and change the background color to AliceBlue
10XpsSaveOptions options = new XpsSaveOptions();
11options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(Length.FromInches(8.3f), Length.FromInches(5.8f)));
12options.BackgroundColor = System.Drawing.Color.AliceBlue;
13
14// Call the ConvertMHTML() method to convert MHTML to XPS
15Converter.ConvertMHTML(stream, options, savePath);Use XPS output when a C# application needs a fixed-layout document for Windows-oriented viewing, printing, or internal document pipelines. If the result must be widely shared outside an XPS-aware environment, MHTML to PDF is usually the more practical choice.
Use ICreateStreamProvider when the conversion output should be written through streams instead of only to a file path. This is useful for memory-based workflows, remote storage, databases, and services that decide where the rendered output is stored.
1// Implement a custom MemoryStream provider for advanced control over HTML rendering output streams
2
3class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
4{
5 // List of MemoryStream objects created during the document rendering
6 public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
7
8 public Stream GetStream(string name, string extension)
9 {
10 // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats
11 MemoryStream result = new MemoryStream();
12 Streams.Add(result);
13 return result;
14 }
15
16 public Stream GetStream(string name, string extension, int page)
17 {
18 // This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
19 MemoryStream result = new MemoryStream();
20 Streams.Add(result);
21 return result;
22 }
23
24 public void ReleaseStream(Stream stream)
25 {
26 // Here you can release the stream filled with data and, for instance, flush it to the hard-drive
27 }
28
29 public void Dispose()
30 {
31 // Releasing resources
32 foreach (MemoryStream stream in Streams)
33 stream.Dispose();
34 }
35}The following example converts MHTML to XPS with MemoryStreamProvider.
1// Convert MHTML to XPS in C# using memory stream
2
3// Create an instance of MemoryStreamProvider
4using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
5
6// Open an existing MHTML file for reading
7using FileStream stream = File.OpenRead(DataDir + "sample.mht");
8
9// Prepare a path for converted file saving
10string savePath = Path.Combine(OutputDir, "stream-provider.xps");
11
12// Convert MHTML to XPS by using the MemoryStreamProvider class
13Converter.ConvertMHTML(stream, new XpsSaveOptions(), streamProvider);
14
15// Get access to the memory stream that contains the result data
16MemoryStream memory = streamProvider.Streams.First();
17memory.Seek(0, SeekOrigin.Begin);
18
19// Flush the result data to the output file
20using (FileStream fs = File.Create(savePath))
21{
22 memory.CopyTo(fs);
23}Use the online MHTML to XPS Converter for quick manual conversion. Use Aspose.HTML for .NET when MHTML to XPS conversion must run inside a C# application, service, or document pipeline.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.