
How to convert HTML to XPS in C# .NET
The HTML file is widely used over the internet for information sharing. Since it is supported by the majority of platforms and almost all web browsers are capable of loading this format, so it makes it one of the acceptable formats. However, owing to its wide acceptance, many hackers try to embed malicious scripts inside HTML files. The high-profile attacks of malicious HTML and JavaScript code have seen a dramatic increase in both awareness and exploitation in recent years. On the other end, an XPS file represents page layout files that are based on XML Paper Specifications.
- XPS is similar to PDF file format but uses XML in the layout, appearance, and printing information of a document.
HTML Conversion API
Aspose.HTML Cloud is specifically designed to provide the capabilities to process HTML files. Apart from input HTML, we can also load XHTML, MHTML, EPUB, Markdown, or a Web page by its URL. The resultant output can be PDF, XPS, DOCX, or raster image formats: JPEG, PNG, TIFF, BMP, GIF. As the API is developed according to REST architecture, so it can be used in Desktop, Mobile, Web, and Hybrid applications, as well as desktop or mobile platforms.
In order to further facilitate our customers, we have created programming language-specific SDKs. The Cloud SDKs are wrappers around REST APIs in various programming languages, allowing you to process documents in the language of your choice. This way, you can quickly and easily, gain all benefits of strong types and IDE highlights. Please check the Aspose.HTML GitHub repository for SDKs along with working examples, to get you started in no time. In this article, we are going to discuss the document conversion features of Aspose.HTML Cloud SDK for .NET.
Installation
To get started with using the Cloud SDK, the first step is to install it over the system. The SDK is available over NuGet and GitHub. Please execute the following command in the terminal window to perform the installation for NuGet
nuget install Aspose.HTML-Cloud
Another option is to install the SDK is through Package Manager Console within Visual Studio. Please execute the following command:
PM> Install-Package Aspose.HTML-Cloud

Image 1:- Aspose.HTML NuGet package installed.
NuGet Package Manager
Another easiest approach is to install the SDK via NuGet Package Manager within Visual Studio IDE.
- Open the Solution Explorer.
- Expand the project and Right-click Packages folder within your solution.
- Select Manage NuGet Packages… option.
- Click on the Browse tab and search for “Aspose.HTML-Cloud“.
- Click on the Aspose.HTML-Cloud package, select the appropriate version in the right-tab and click Add Package button.

Image 2:- Aspose.HTML Package in NuGet Package Manager.
Free Cloud Subscription
The next important step is a free account subscription on Aspose.Cloud dashboard. No credit card or other payment details are required. If you have a GitHub or Google account, simply Sign Up or, click on the create a new account button and provide the required information. Now login to the dashboard using credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

Image 3:- Client Credentials at Aspose.Cloud dashboard.
HTML to XPS in C#
In this section, we are going to discuss the conversion of HTML files to XPS format for the following two scenarios.
Load HTML from Cloud Storage
Please follow the instructions below to upload an HTML file to Cloud storage and then perform conversion to XPS format.
- First of all, please create HtmlApi instance while passing ClientID and ClientSecret details as arguments.
- Secondly, read HTML file using File.OpenRead(…) method.
- Thirdly, create an instance of StorageApi and use the UploadFile(…) method to upload the HTML file to Cloud storage.
- Finally, call PutConvertDocumentToXps(…) method to perform HTML to XPS conversion and save the resultant XPS to Cloud storage.
// Get ClientID from https://dashboard.aspose.cloud/ | |
string clientSecret = "d757548a9f2558c39c2feebdf85b4c44"; | |
string clientID = "4db2f826-bf9c-42e7-8b2a-8cbca2d15553"; | |
// base URL string | |
const string SERVICE_API_HOST = "https://api.aspose.cloud"; | |
// Create HtmlApi instance | |
HtmlApi htmlApi = new HtmlApi(clientID, clientSecret,SERVICE_API_HOST); | |
String name = "inputHTML.html";// inpit Document name. | |
int width = 800; // Resulting image width. | |
int height = 1000; // Resulting image height. | |
int leftMargin = 10; // Left resulting image margin. | |
int rightMargin = 10; // Right resulting image margin. | |
int topMargin = 10; // Top resulting image margin. | |
int bottomMargin = 10; // Bottom resulting image margin. | |
// name of resultant file | |
string resultantFile = "Resultant.xps"; | |
try | |
{ | |
// load the file from local drive | |
using (var file = System.IO.File.OpenRead(@"C:\Users\shahbnay\Desktop\" + name)) | |
{ | |
// Create StorageApi instance | |
var uploadFileRequest = new StorageApi(clientID, clientSecret, SERVICE_API_HOST); | |
// upload HTML file to Cloud storage | |
uploadFileRequest.UploadFile(file, "inputHTML.html"); | |
} | |
// Perform the conversion to PDF format | |
// save the output to Cloud storage | |
AsposeResponse response = htmlApi.PutConvertDocumentToPdf( | |
name, resultantFile, width, height, | |
leftMargin, rightMargin, topMargin, bottomMargin); | |
// print success message if conversion is successful | |
if (response != null && response.Equals("OK")) | |
{ | |
Console.WriteLine("HTML successfully converted to XPS !"); | |
} | |
}catch (Exception ex) | |
{ | |
Console.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace); | |
} |

Image 4:- HTML to XPS conversion preview.
For testing purposes, the input HTML and resultant XPS can be downloaded from inputHTML.html and Resultant.xps.
Convert HTML to XPS using cURL
As our APIs are developed according to REST architecture, so they can also be accessed via cURL commands. In this section, we are going to discuss the conversion of the HTML page to XPS format using the cURL command. But before we proceed further, the first step is to create JSON Web Token (JWT) based on client credentials from Aspose.Cloud dashboard. Please follow the steps given below to perform the HTML to XPS conversion.
- Get your Client ID and Client Secret from Cloud dashboard.
- Get JSON Web Access Token based on client credentials.
- Call the PutConvertDocumentToXps API.
- The resultant PDF file will be saved in the Cloud storage.
Please execute the following cURL command to generate the JWT access token.
curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=4db2f826-bf9c-42e7-8b2a-8cbca2d15553&client_secret=d757548a9f2558c39c2feebdf85b4c44" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
Please execute the following command to upload the HTML files to Cloud storage:
curl -X PUT "https://api.aspose.cloud/html/storage/file/inputHTML.html" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-H "Content-Type: multipart/form-data" \
-d {"File":{}}
After the HTML is uploaded to Cloud storage, the following command can be used to perform the HTML to XPS conversion and save the resultant file in Cloud storage.
curl -X PUT "https://api.aspose.cloud/html/inputHTML.html/convert/xps?outPath=converted.xps&width=800&height=1000&leftMargin=10&rightMargin=10&topMargin=10&bottomMargin=10" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>"
Free XPS Viewer
In order to view the XPS file, you may consider using XPS Viewer App. It is developed on top of REST APIs and enables you to experience the amazing capabilities of the API.
Conclusion
We have explored the capabilities of Aspose.HTML Cloud for the conversion of HTML files to fixed page layout and other raster images. We have explored the features to convert HTML using C# .NET code snippet as well as using the cURL commands.
Nevertheless, Aspose.HTML Cloud SDK for .NET which is a wrapper around Aspose.HTML Cloud is developed under MIT license, so the complete code snippet can be downloaded from GitHub. If you encounter any issues while using the API, please feel free to contact us via the free product support forum.
Related articles
We recommend visiting the following links to learn more about