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 文件:
(完)
来源: http://www.bubuko.com/infodetail-3607002.html