码迷,mamicode.com
首页 > Windows程序 > 详细

C# 将Word转为PDF、XPS、Epub、RTF(基于Spire.Cloud.Word.SDK)

时间:2020-01-13 15:59:25      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:font   代码   编辑   代码示例   convert   png   put   搜索   nbsp   

本文介绍通过调用Spire.Cloud.Word.SDK提供的ConvertApi接口将Word转换为PDF、XPS、Epub、RTF以及将Docx转为Doc格式等。调用接口方法及步骤参考以下步骤:

步骤一:dll文件获取及导入。通过官网本地下载SDK文件包。(须在e-iceblue中国官网在线编辑板块中注册账号并登录)

技术图片

 

下载后,解压文件,将Spire.Cloud.Word.Sdk.dll文件及其他三个dll添加引用至VS程序;或者在程序中通过Nuget搜索下载,直接导入所有dll。dll引用结果如下图所示:

技术图片

 

步骤二:App ID及Key获取在“我的应用”板块中创建应用以获得App ID及App Key。

技术图片

步骤三:源文档上传。在“文档管理”板块,上传源文档。这里可以建文件夹,将文档存放在文件夹下。不建文件夹时,源文档及结果文档直接保存在根目录。本文示例中,建了两个文件夹,分别用于存放源文档及结果文档。(云平台提供免费1 万次调用次数和 2G 文档内存)

技术图片

 

C# 代码示例

1. WordPDF

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


namespace WordToPDF
{
    class Program
    {
        static String appId = "App ID";
        static String appKey = "App Key";
        static void Main(string[] args)
        {
            //配置App ID和App Key
            Configuration configuration = new Configuration(appId, appKey);

            //初始化ConvertApi对象
            ConvertApi convertApi = new ConvertApi(configuration);
           
            string name = "Sample.docx";//源文档
            string format = "pdf";//转换的目标文档格式
            string password = null;//源文档
            string folder = "input";//源文档所在文件夹,如果没有文件夹则为null
            string storage = null;//使用冰蓝云配置的2G空间存贮文档,可设置为null            
            string destFilePath = "output/WordToPDF.pdf";//结果文档路径(结果文档保存在output文件夹下)

            //调用方法将Word转为PDF
            convertApi.ConvertDocument(name, format, password, folder, storage, destFilePath);
        }
    }
}

 

2. WordXPS

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 void Main(string[] args)
        {
            //配置App ID和App Key
            Configuration configuration = new Configuration(appId, appKey);

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

            string name = "Sample.docx"; //源文档
            string format = "xps";//转换的目标文档格式
            string password = null;//源文档密码
            string folder = "input";//源文档的文件夹,如果没有文件夹则为null
            string storage = null;//使用冰蓝云配置的2G空间存贮文档,可设置为null  
            string destFilePath = "output/WordToXPS.xps";//结果文档路径及名称(结果文档保存在output文件夹下)

            //调用方法将Word转为XPS
            convertApi.ConvertDocument(name, format, password, folder, storage, destFilePath);
        }
    }
}

 

3. WordEpub

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

namespace WordToEpub
{
    class Program
    {
        static String appId = "App ID";
        static String appKey = "App Key";
        static void Main(string[] args)
        {
            //配置App ID和App Key
            Configuration configuration = new Configuration(appId, appKey);

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

            string name = "Sample.docx"; //源文档
            string format = "epub";//转换的目标文档格式
            string password = null;//源文档密码
            string folder = "input";//源文档的文件夹,如果没有文件夹则为null
            string storage = null;//使用冰蓝云配置的2G空间存贮文档,可设置为null  
            string destFilePath = "output/WordToEpub.epub";//结果文档路径及名称(结果文档保存在output文件夹下)

            //调用方法将Word转为Epub
            convertApi.ConvertDocument(name, format, password, folder, storage, destFilePath);
        }
    }
}

 

4. WordRTF

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

namespace WordToRTF
{
    class Program
    {
        static String appId = "App ID";
        static String appKey = "App Key";
        static void Main(string[] args)
        {
            //配置App ID和App Key
            Configuration configuration = new Configuration(appId, appKey);

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

            string name = "Sample.docx"; //源文档
            string format = "rtf";//转换的目标文档格式
            string password = null;//源文档密码
            string folder = "input";//源文档的文件夹,如果没有文件夹则为null
            string storage = null;//使用冰蓝云配置的2G空间存贮文档,可设置为null  
            string destFilePath = "output/WordToRTF.rtf";//结果文档路径及名称(结果文档保存在output文件夹下)

            //调用方法将Word转为RTF
            convertApi.ConvertDocument(name, format, password, folder, storage, destFilePath);
        }
    }
}

 

5. DocxDoc

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


namespace DocxToDoc
{
    class Program
    {
        static String appId = "App ID";
        static String appKey = "APP Key";
        static void Main(string[] args)
        {
            //配置App ID和App Key
            Configuration configuration = new Configuration(appId, appKey);

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

            string name = "Sample.docx"; //源文档
            string format = "doc";//转换的目标文档格式
            string password = null;//源文档密码
            string folder = "input";//源文档的文件夹,如果没有文件夹则为null
            string storage = null;//使用冰蓝云配置的2G空间存贮文档,可设置为null  
            string destFilePath = "output/DocxToDoc.doc";//结果文档路径及名称(结果文档保存在output文件夹下)

            //调用方法将Docx转为Doc
            convertApi.ConvertDocument(name, format, password, folder, storage, destFilePath);
        }
    }
}

 

(本文完)

C# 将Word转为PDF、XPS、Epub、RTF(基于Spire.Cloud.Word.SDK)

标签:font   代码   编辑   代码示例   convert   png   put   搜索   nbsp   

原文地址:https://www.cnblogs.com/Yesi/p/12187601.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!