Spire.Cloud.Word provides the ConvertApi interface, which is used for saving Word documents to other formats, such as PDF, XPS, Doc, Docx, RTF, and Epub. This article shows you how to convert Word to PDF and XPS.
Example 1. Convert Word to PDF
using System;
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
namespace WordToPdf
{
class Program
{
static string appId = "App ID";
static string appKey = "App Key";
static string baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration object based on App ID, App Key and base URL
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
//Create a ConvertApi object
static ConvertApi convertApi = new ConvertApi(configuration);
static void Main(string[] args)
{
//Specify the source document name
string name = "sample.docx";
//Specify the target file format to convert
string format = "pdf";
//Specify the path and name of the generated document
string destFilePath = "output/ToPDF.pdf";
//Specify the open password of the source document. Null if no password
string password = null;
//Specify the folder where the source document is stored
string folder = "input";
//Use the default storage, set to null
string storage = null;
//Call Convert method to convert the source document to PDF and store it in the specified location
convertApi.Convert(name, format, destFilePath, password, folder, storage);
}
}
}
Example 2. Convert Word to XPS
using System;
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
namespace WordToXps
{
class Program
{
static string appId = "App ID";
static string appKey = "App Key";
static string baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration object based on App ID, App Key and base URL
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
//Create a ConvertApi object
static ConvertApi convertApi = new ConvertApi(configuration);
static void Main(string[] args)
{
//Specify the source document name
string name = "sample.docx";
//Specify the target file format to convert
string format = "xps";
//Specify the path and name of the generated document
string destFilePath = "output/ToXPS.xps";
//Specify the open password of the source document. Null if no password
string password = null;
//Specify the folder where the source document is stored
string folder = "input";
//Use the default storage, set to null
string storage = null;
//Call Convert method to convert the source document to XPS and store it in the specified location
convertApi.Convert(name, format, destFilePath, password, folder, storage);
}
}
}