zoukankan      html  css  js  c++  java
  • C# 将PDF转为Word、Html、XPS、SVG、PCL、PS——基于Spire.Cloud.SDK for .NET

    Spire.Cloud. SDK for .NET 提供了接口PdfConvertApi可用于将PDF文档转换为其他格式文档,如Word(docx/doc)、Html、XPS、SVG、PCL、PS、Png以及XPS转成PDF。本文将选取其中几种格式为例,介绍具体转换方法。

    必要步骤:

    步骤一:dll文件获取及导入。 在VS程序中通过Nuget搜索下载,直接导入所有dll。

    导入效果如下如所示:

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

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

    C# 代码示例

    【示例1】PDF 转Word(支持docx/doc

    using System;
    using Spire.Cloud.Pdf.Sdk.Client;
    using Spire.Cloud.Pdf.Sdk.Api;
    using System.IO;
    
    
    namespace PDFToWord
    {
        class PDFToDocx
        {
            //配置账号信息
            static String appId = " App ID ";
            static String appKey = " App Key ";
            static String baseUrl = "https://api.e-iceblue.cn";
            static Configuration pdfConfiguration = new Configuration(appId, appKey, baseUrl);
            static PdfConvertApi pdfConvertApi = new PdfConvertApi(pdfConfiguration);
            static void Main(string[] args)
            {
                string name = "sample.pdf";//源文档        
                string folder = "input";//设置源文档所在文件夹(如果源文档在根目录下,不在文件夹中,可设置为null)
                string destFilePath = "pdfconversion/PDFToDocx.docx";//设置转换后的目标文档路径(文档放置在pdfconversion文件夹下)
                string format = "Docx";//转换的目标文档格式
                string storage = null;//冰蓝云提供的2G免费存储空间,可设置为null
                string password = null; //源文档密码(如果文档没有密码则设置成null)
    
                //调用方法转为Word文档格式
                pdfConvertApi.Convert(name, destFilePath,format, folder,storage, password);
               
            }
        }
    }

    【示例2】PDF转Html

    using System;
    using Spire.Cloud.Pdf.Sdk.Client;
    using Spire.Cloud.Pdf.Sdk.Api;
    
    
    namespace PDFToHTML
    {
        class Program
        {
            //配置账号信息
            static String appId = "App ID";
            static String appKey = "App Key";
            static String baseUrl = "https://api.e-iceblue.cn";
            static Configuration pdfConfiguration = new Configuration(appId, appKey, baseUrl);
            static PdfConvertApi pdfConvertApi = new PdfConvertApi(pdfConfiguration);
    
            static void Main(string[] args)
            {
                string name = "sample.pdf";//源文档          
                string folder = "input";//设置源文档所在文件夹(如果源文档在根目录下,不在文件夹中,可设置为null)
                string destFilePath = "pdfconversion/PDFToHtml.html";//设置转换后的目标文档路径(文档放置在pdfconversion文件夹下)
                string password = null; //源文档密码(如果文档没有密码则设置成null)
                string format = "Hmtl";//转换的目标文档格式
                string storage = null;//冰蓝云提供的2G免费存储空间,可设置为null
    
                //调用方法转换为HTML格式
                pdfConvertApi.Convert(name,destFilePath,format,folder,storage,password);
            }
        }
    }

    【示例3】PDF转XPS

        3.1  PDF 转 XPS

    using System;
    using Spire.Cloud.Pdf.Sdk.Client;
    using Spire.Cloud.Pdf.Sdk.Api;
    
    
    namespace PDFToXPS
    {
        class Program
        {
            //配置账号信息
            static String appId = "App ID";
            static String appKey = " App Key ";
            static String baseUrl = "https://api.e-iceblue.cn";
            static Configuration pdfConfiguration = new Configuration(appId, appKey, baseUrl);
            static PdfConvertApi pdfConvertApi = new PdfConvertApi(pdfConfiguration);
            static void Main(string[] args)
            {
                string name = "sample.pdf";//源文档
                string folder = "input";//设置源文档所在文件夹(如果源文档在根目录下,不在文件夹中,可设置为null)
                string destFilePath = "pdfconversion/PDFToXPS.xps";//设置转换后的目标文档路径(文档放置在pdfconversion文件夹下)
                string format = "Xps";//转换的目标文档格式
                string storage = null;//冰蓝云提供的2G免费存储空间,可设置为null           
                string password = null; //源文档密码(如果文档没有密码则设置成null)
    
                //调用方法转为XPS
                pdfConvertApi.Convert(name, destFilePath, format,folder, storage, password);
            }
        }
    }

    3.2 XPS 转 PDF

    using System;
    using Spire.Cloud.Pdf.Sdk.Client;
    using Spire.Cloud.Pdf.Sdk.Api;
    
    
    namespace XPSToPDF
    {
        class Program
        {
            //配置账号信息
            static String appId = "App ID";
            static String appKey = "App Key";
            static String baseUrl = "https://api.e-iceblue.cn";
            static Configuration pdfConfiguration = new Configuration(appId, appKey, baseUrl);
            static PdfConvertApi pdfConvertApi = new PdfConvertApi(pdfConfiguration);
            static void Main(string[] args)
            {           
                string name = "test.xps";//源文档
                string destFileFolder = "pdfconversion/XPSToPdf.pdf";//目标文件夹路径
                string format = "Pdf";//转换的目标文件格式
                string folder = "input";//源文档所在文件夹
                string storage = null;//冰蓝云提供的2G免费存储空间,可设置为null
                string password = null;//源文档密码(如果文档没有密码则设置成null)
    
                pdfConvertApi.Convert(name, destFileFolder,format,folder,storage,password);
            }
        }
    }

    【示例4】PDF转SVG

    using System;
    using Spire.Cloud.Pdf.Sdk.Client;
    using Spire.Cloud.Pdf.Sdk.Api;
    
    
    namespace PDFToSvg
    {
        class Program
        {
            //配置账号信息
            static String appId = " App ID ";
            static String appKey = " App Key ";
            static String baseUrl = "https://api.e-iceblue.cn";
            static Configuration pdfConfiguration = new Configuration(appId, appKey, baseUrl);
            static PdfConvertApi pdfConvertApi = new PdfConvertApi(pdfConfiguration);
            static void Main(string[] args)
            {
                string name = "sample.pdf";//源文档
                string folder = "input";//设置源文档所在文件夹(如果源文档在根目录下,不在文件夹中,可设置为null)
                string destFilePath = "pdfconversion/PDFToSvg.svg";//设置转换后的目标文档路径(文档放置在pdfconversion文件夹下)
                string format = "Svg";//目标文档格式
                string storage = null;//冰蓝云配置的2G存储空间,可设置为null         
                string password = null; //源文档密码(如果文档没有密码则设置成null)
    
                //调用方法转为SVG
                pdfConvertApi.Convert(name, destFilePath, format,folder,storage, password);
            }
        }
    }

    注:这里转为svg是将原PDF文档中的每一页单独转换为一个svg文档,如果原PDF文档包含多页,转换后默认生成一个文件夹,将生成的每一页svg放在这个文件夹下。

    【示例5】PDF转PCL

    using System;
    using Spire.Cloud.Pdf.Sdk.Client;
    using Spire.Cloud.Pdf.Sdk.Api;
    
    namespace PDFToPcl
    {
        class Program
        {
            //配置账号信息
            static String appId = "App ID";
            static String appKey = "App Key";
            static String baseUrl = "https://api.e-iceblue.cn";
            static Configuration pdfConfiguration = new Configuration(appId, appKey, baseUrl);
            static PdfConvertApi pdfConvertApi = new PdfConvertApi(pdfConfiguration);
            static void Main(string[] args)
            {
                string name = "sample.pdf";//源文档 
                string destFilePath = "pdfconversion/PDFToPcl.pcl";//设置转换后的目标文档路径(文档放置在pdfconversion文件夹下)
                string format = "Pcl";//目标文档格式
                string folder = "input";//设置源文档所在文件夹(如果源文档在根目录下,不在文件夹中,可设置为null)
                string storage = null;//冰蓝云配置的2G存储空间,可设置为null
                string password = null; //源文档密码(如果文档没有密码则设置成null)
    
                //调用方法转换为Pcl格式
                pdfConvertApi.Convert(name, destFilePath, format,folder,storage, password);
            }
        }
    }

    【示例6】PDF转PS

    using System;
    using Spire.Cloud.Pdf.Sdk.Client;
    using Spire.Cloud.Pdf.Sdk.Api;
    
    
    namespace PDFToPs
    {
        class Program
        {
            //配置账号信息
            static String appId = " App ID ";
            static String appKey = " App Key ";
            static String baseUrl = "https://api.e-iceblue.cn";
            static Configuration pdfConfiguration = new Configuration(appId, appKey, baseUrl);
            static PdfConvertApi pdfConvertApi = new PdfConvertApi(pdfConfiguration);
            static void Main(string[] args)
            {           
                string name = "sample.pdf";//源文档
                string folder = "input";//设置源文档所在文件夹(如果源文档在根目录下,不在文件夹中,可设置为null)
                string destFilePath = "pdfconversion/PDFToPs.ps";//设置转换后的目标文档路径(文档放置在pdfconversion文件夹下)
                string format = "Ps";//转换的目标文档格式
                string storage = null;//冰蓝云配置的2G存储空间,可设置为null
                string password = null; //源文档密码(如果文档没有密码则设置成null)
    
                //调用方法转为PS
                pdfConvertApi.Convert(name, destFilePath,format, folder, storage, password);
            }
        }
    }

    文档格式转换效果:

    (本文完)

  • 相关阅读:
    Python 面向对象 —— super 的使用(Python 2.x vs Python 3.x)
    安全移除驱动器、弹出、卸载的差别及详细查看设备的运行前后的异同
    java中不常见的keyword:strictfp,transient
    textarea文本域宽度和高度(width、height)自己主动适应变化处理
    Android 输入框弹出样式
    .net下载优酷1080P视频
    Oracle Hints具体解释
    关于成本核算方法、步骤、成本分析的简单回复
    程序猿接私活经验总结,来自csdn论坛语录
    Android getResources的作用和须要注意点
  • 原文地址:https://www.cnblogs.com/Yesi/p/12133971.html
Copyright © 2011-2022 走看看