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,可參考這裏的導入方法。
導入結果:
方式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文件:
(完)