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

如果你不相信努力和时光,那么成果就会是第一个选择辜负你的。不要去否定你自己的过去,也不要用你的过去牵扯你现在的努力和对未来的展望。不是因为拥有希望你才去努力,而是去努力了,你才有可能看到希望的光芒。C# 将Word转为PDF、XPS、Epub、RTF(基于Spire.Cloud.Word.SDK),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

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

步骤一:dll文件获取及导入。在程序中通过Nuget搜索下载,直接导入所有dll。dll引用结果如下图所示:

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

 

 

 

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

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

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

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

 

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.Convert(name, format, destFilePath, password, folder, storage);
        }
    }
}

 

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.Convert(name, format, destFilePath,password, folder, storage);
        }
    }
}

 

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.Convert(name, destFilePath, format, password, folder, storage);
        }
    }
}

 

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.Convert(name, format, destFilePath, password, folder, storage );
        }
    }
}

 

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.Convert(name, format, destFilePath, password, folder, storage);
        }
    }
}

 

(本文完)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/209373.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!