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); } } }
檔案格式轉換效果:

(本文完)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/84751.html
標籤:C#
上一篇:[譯]C# 7系列,Part 10: Span<T> and universal memory management Span<T>和統一記憶體管理
