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);
        }
    }
}

 

文檔格式轉換效果:

 

(本文完)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM