Spire.Cloud.SDK for Java提供了接口PdfConvertApi通過convert()方法将PDF文檔以及XPS文檔轉為指定文檔格式,如轉PDF為Word(支援Docx、Doc)、Html、XPS、SVG、PS、PCL、PNG,将XPS轉為Word(支援Docx、Doc)、Html、PDF、SVG、PS、PCL、PNG等。文本将通過Java示例介紹具體實作方法。首先請參考以下步驟準備程式運作環境:
一、導入jar檔案。(有2種方式)
(推薦)方式1. 建立Maven項目程式,通過maven倉庫下載下傳導入。以IDEA為例,建立Maven項目,在pom.xml檔案中配置maven倉庫路徑,并指定spire.cloud.sdk的依賴,如下:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>cloud</name>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> cloud </groupId>
<artifactId>spire.cloud.sdk</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId> com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId> com.squareup.okhttp</groupId>
<artifactId>logging-interceptor</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId> com.squareup.okhttp </groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId> com.squareup.okio </groupId>
<artifactId>okio</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId> io.gsonfire</groupId>
<artifactId>gson-fire</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.18</version>
</dependency>
<dependency>
<groupId> org.threeten </groupId>
<artifactId>threetenbp</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
完成配置後,點選“Import Changes” 即可導入所有需要的jar檔案。如果使用的是Eclipse,可參考這裡的導入方法。
導入結果:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5yN5QzNykTM1MTMtcTNzIjM4ADNxUjM2ADMyAjMtATOwYDM38CX2ADMyAjMvwFM5AjNwczLcd2bsJ2Lc12bj5ycn9Gbi52YuAjMwIzZtl2Lc9CX6MHc0RHaiojIsJye.png)
方式2:手動下載下傳jar包,然後解壓檔案,手動導入jar,同時還需另行手動導入其他幾個jar檔案。
二、登入冰藍雲賬号,建立檔案夾,上傳文檔。
三、建立應用程式,擷取App ID及App Key。
完成以上步驟後,可參考以下代碼示例進行文檔轉換。
【示例1】将PDF轉為 Word(支援Docx、Doc)、Html、XPS、SVG、PS、PCL、PNG
import spire.cloud.pdf.sdk.*;
import spire.cloud.pdf.sdk.api.PdfConvertApi;
public class PDFtoWord {
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl= "https://api.e-iceblue.cn";
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
static PdfConvertApi pdfConvertApi=new PdfConvertApi(configuration);
public static void main(String[] args) throws ApiException{
String name = "sample.pdf";//PDF源文檔
//PDF轉Word(支援Docx/Doc)
String destFilePath = "output/PDFtoWord.docx";
String format = "Docx";
/*//PDF轉Html
String destFilePath = "output/PDFtoHtml.html";
String format = "Html";*/
/*//PDF轉PCL
String destFilePath = "output/PDFtoPCL.pcl";
String format = "Pcl";*/
/*//PDF轉SVG
String destFilePath = "output/PDFtoSVG.svg";
String format = "Svg";*/
/*//PDF轉PS
String destFilePath = "output/PDFtoPS.ps";
String format = "Ps";*/
/*//PDF轉XPS
String destFilePath = "output/PDFtoXPS.xps";
String format = "Xps";*/
/*//PDF轉PNG
String destFilePath = "output/PDFtoPng.png";
String format = "Png";*/
String folder ="input";//源文檔所在檔案夾
String storage = null;//冰藍雲配置的2G免費存儲空間,可設定為null
String password = null;//源文檔密碼(無密碼可設定為null)
//調用方法将PDF轉為指定文檔格式
pdfConvertApi.convert(name,destFilePath,format,folder,storage,password);
}
}
【示例2】将XPS轉為Word(支援Docx、Doc)、Html、PDF、SVG、PS、PCL、PNG
import spire.cloud.pdf.sdk.ApiException;
import spire.cloud.pdf.sdk.Configuration;
import spire.cloud.pdf.sdk.api.PdfConvertApi;
public class XPStoWord {
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl= "https://api.e-iceblue.cn";
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
static PdfConvertApi pdfConvertApi = new PdfConvertApi(configuration);
public static void main(String[] args) throws ApiException {
String name = "test.xps";//XPS源文檔
//XPS轉Word(支援Docx、Doc)
String destFilepath = "output/XPStoDocx.docx";//結果文檔路徑
String format = "Docx";
/*//XPS轉Html
String destFilepath = "output/XPStoHtml.html";//結果文檔路徑
String format = "Html"; */
/*//XPS轉SVG
String destFilepath = "output/XPStoSVG.svg";//結果文檔路徑
String format = "Svg"; */
/*//XPS轉PCL
String destFilepath = "output/XPStoPCL.pcl";//結果文檔路徑
String format = "Pcl"; */
/*//XPS轉PS
String destFilepath = "output/XPStoPS.ps";//結果文檔路徑
String format = "Ps";*/
/*//XPS轉PNG
String destFilepath = "output/XPStoPNG.png";//結果文檔路徑
String format = "Png";*/
/*//XPS轉PDF
String destFilepath = "output/XPStoPDF.pdf";//結果文檔路徑
String format = "Pdf";*/
String folder = "input";//源文檔所在檔案夾
String storage = null;
String password = null;
//調用方法轉換XPS檔案為指定文檔格式
pdfConvertApi.convert(name, destFilepath, format, folder, storage, password);
}
}
文檔轉換結果如下,注意轉為SVG格式時,将源文檔的每一頁作為一個單獨的svg檔案儲存,當源文檔為多頁時,在轉換結果時會預設生成一個檔案夾放置svg檔案:
(完)