1. 程式人生 > 實用技巧 >Java 將PDF/XPS轉為Word/html /SVG/PS/PCL/PNG、PDF和XPS互轉(基於Spire.Cloud.SDK for Java)

Java 將PDF/XPS轉為Word/html /SVG/PS/PCL/PNG、PDF和XPS互轉(基於Spire.Cloud.SDK for Java)

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檔案:

(完)