Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To convert HTML to XPS in C#, use
Converter.ConvertHTML() with an HTML source,
XpsSaveOptions, and an output path or stream provider. Aspose.HTML for .NET can convert HTML from a file, string, stream, or URL, and XpsSaveOptions lets you control XPS rendering settings such as page setup, CSS handling, background color, and resolution.
This guide shows the core HTML to XPS workflows for C# applications: a one-line conversion, a file-based conversion, custom XPS save options, and output stream handling with ICreateStreamProvider. 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.
XPS is a fixed-layout document format used for consistent viewing, printing, and sharing. HTML to XPS conversion is useful when a C# application needs a stable page representation from HTML content while still controlling page size, margins, colors, and stream output.
For simple input, the static
Converter class provides a short path from HTML content to an XPS file. The example below passes HTML code, a base URI, default XpsSaveOptions, and the output file path to ConvertHTML().
1// Convert HTML to XPS using C#
2
3// Invoke the ConvertHTML() method to convert the HTML code to XPS
4Converter.ConvertHTML(@"<h1>Convert HTML to XPS!</h1>", ".", new XpsSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.xps"));Use this pattern when your HTML is already available as a string and default XPS settings are enough. For file input, custom options, or stream output, use the workflows below.
A typical file-based conversion uses an
HTMLDocument instance, an XpsSaveOptions object, and Converter.ConvertHTML(). You can load HTML from a local file, HTML code, stream, or URL. For more loading patterns, see
Creating an HTML Document.
The workflow is:
HTMLDocument constructor.XpsSaveOptions instance.Converter.ConvertHTML(document, options, savePath) to write the XPS file.The following C# code converts an HTML document to XPS.
1// Convert HTML to XPS in C#
2
3// Prepare a path to a source HTML file
4string documentPath = Path.Combine(DataDir, "canvas.html");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "canvas-output.xps");
8
9// Initialize an HTML document from the file
10using HTMLDocument document = new HTMLDocument(documentPath);
11
12// Initialize XpsSaveOptions
13XpsSaveOptions options = new XpsSaveOptions();
14
15// Convert HTML to XPS
16Converter.ConvertHTML(document, options, savePath);Use XPS output when a C# application needs a fixed-layout document for Windows-oriented viewing, printing, or internal document pipelines. XPS is useful when page fidelity matters but the workflow expects XPS rather than PDF. If the output must be widely shared outside an XPS-aware environment, HTML to PDF is often the more practical choice.
For XPS output, create XpsSaveOptions and pass it to Converter.ConvertHTML() together with the HTML source and output path. You can tune page setup, CSS processing, background color, rendering resolution, and stream-based output through the options object.
Use
XpsSaveOptions when the output XPS needs custom rendering settings. The options object is passed to ConvertHTML() and controls how the HTML document is rendered to XPS.
| 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. |
The following example creates an XPS file with custom page size and background color.
1// Convert HTML to XPS with custom settings using C#
2
3string documentPath = Path.Combine(OutputDir, "save-options.html");
4string savePath = Path.Combine(OutputDir, "save-options-output.xps");
5
6// Prepare HTML code and save it to a file
7string code = "<h1> XpsSaveOptions Class</h1>\r\n" +
8 "<p>Using XpsSaveOptions Class, you can programmatically apply a wide range of conversion parameters such as BackgroundColor, PageSetup, etc.</p>\r\n";
9
10File.WriteAllText(documentPath, code);
11
12// Initialize an HTML Document from the html file
13using HTMLDocument document = new HTMLDocument(documentPath);
14
15// Set up the page-size, margins and change the background color to AntiqueWhite
16XpsSaveOptions options = new XpsSaveOptions()
17{
18 BackgroundColor = System.Drawing.Color.AntiqueWhite
19};
20options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(Length.FromInches(4.9f), Length.FromInches(3.5f)), new Margin(30, 20, 10, 10));
21
22// Convert HTML to XPS
23Converter.ConvertHTML(document, options, savePath);In this example:
BackgroundColor changes the XPS page background.PageSetup.AnyPage defines the page size and margins used for the output pages.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, or services that need to decide where the rendered output is stored.
Some output formats produce one file, such as PDF, XPS, and DOCX. Image formats such as JPG or PNG can produce multiple output streams when the rendered document has multiple pages.
The MemoryStreamProvider class below implements ICreateStreamProvider and keeps the generated streams in memory.
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 HTML to XPS with MemoryStreamProvider, reads the generated memory stream, and saves it to an XPS file.
1// Convert HTML to XPS in C# using memory stream
2
3// Create an instance of MemoryStreamProvider
4using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
5
6// Initialize an HTML document
7using HTMLDocument document = new HTMLDocument(@"<h1>Convert HTML to XPS File Format!</h1>", ".");
8
9// Convert HTML to XPS using MemoryStreamProvider
10Converter.ConvertHTML(document, new XpsSaveOptions(), streamProvider);
11
12// Get access to the memory stream that contains the result data
13MemoryStream memory = streamProvider.Streams.First();
14memory.Seek(0, SeekOrigin.Begin);
15
16// Flush the result data to the output file
17using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.xps")))
18{
19 memory.CopyTo(fs);
20}PdfSaveOptions.DocSaveOptions.Use the online HTML to XPS Converter for quick manual conversion. Use Aspose.HTML for .NET when HTML 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.