Spire.Cloud.Word 转换 Word 文档到 PDF、XPS

Spire.Cloud.Word提供了ConvertApi接口用于将Word文档保存为其他格式文档,如PDF, XPS, Doc, Docx, RTF, EPUB。本文介绍如何转换Word到PDF和XPS。

步骤一:创建.NET应用程序,通过NuGet搜索安装Spire.Cloud.Sdk到您的.NET项目,详细步骤可参考这篇文章

步骤二:通过冰蓝云官网(https://cloud.e-iceblue.cn/)注册账号并登陆,在“我的应用”版块创建应用程序,获得App ID及App Key。

Spire.Cloud.Word 转换 Word 文档到 PDF、XPS

步骤三:上传Word文档至冰蓝云官网的“文档管理”版块。为了便于文档管理,您也可以先创建文件夹“input”和“output”,然后将需要编辑的Word文档上传至input文件夹,output文件夹用于存放生成的文档。本教程将示例文档上传到了input文件夹下。

Spire.Cloud.Word 转换 Word 文档到 PDF、XPS

步骤四:在.NET程序中编写代码操作input文件夹的下的文档。


示例1、转换Word到PDF

using System;
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;

namespace ConvertWordToPDF
{
    class Program
    {
        static string appId = "App ID";
        static string appKey = "App Key";

        static void Main(string[] args)
        {
            //配置App ID和App Key
            Configuration wordConfiguration = new Configuration(appId, appKey);

            //初始化ConvertApi对象
            ConvertApi convertApi = new ConvertApi(wordConfiguration);

            //指定源文档名称
            string name = "示例文档.docx";

            //指定转换的目标格式
            string format = "pdf";

            //设置生成文档的路径及名称
            string destFilePath = "output/ToPDF.pdf";

            //指定源文档的打开密码,无密码则为null
            string password = null;

            //指定存放源文档的文件夹
            string folder = "input";

            //使用冰蓝云默认的存储空间,设置为null
            string storage = null;

            //调用Convert方法转换源文档为PDF,并存放到指定位置
            convertApi.Convert(name, format, destFilePath, password, folder, storage);  
        }
    }
}

Spire.Cloud.Word 转换 Word 文档到 PDF、XPS

示例2、转换Word到XPS

using System;
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;

namespace ConvertWordToXPS
{
    class Program
    {
        static string appId = "App ID";
        static string appKey = "App Key";

        static void Main(string[] args)
        {
            //配置App ID和App Key
            Configuration wordConfiguration = new Configuration(appId, appKey);

            //初始化ConvertApi对象
            ConvertApi convertApi = new ConvertApi(wordConfiguration);

            //指定需要转换的文档名称
            string name = "示例文档.docx";

            //指定转换的目标格式
            string format = "xps";

            //设置生成文档的路径及名称
            string destFilePath = "output/ToXPS.xps";

            //指定源文档的打开密码,无密码则为null
            string password = null;

            //指定存放源文档的文件夹
            string folder = "input";

            //使用冰蓝云默认的存储空间,设置为null
            string storage = null;

            //调用Convert方法转换源文档为XPS,并存放到指定位置
            convertApi.Convert(name, format, destFilePath, password, folder, storage);
        }
    }
}

Spire.Cloud.Word 转换 Word 文档到 PDF、XPS