Java 将Word转为PDF/Html/图片/XPS/SVG(基于Spire.Cloud.SDK for Java)
Spire.Cloud.SDK for Java API提供了ConverApi接口支持将Word文档转换为多种文档格式,包括将Word转为PDF、Html、位图Bitmap(JPEG/BMP/ PNG)、矢量图Vectorgraph(支持WMF/SVG)、XPS、PostScript、PCL、dotx、dotm、docm、odt、wordxml、wordml、doc、docx、rtf、epub等等。下面将详细介绍如何来实现转换。
步骤一、导入jar文件
创建Maven项目程序,通过maven仓库下载导入。以IDEA为例,新建Maven项目,在l文件中配置maven仓库路径,并指定spire.cloud.sdk的依赖,如下:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>cloud</name>
<url>repo.e-iceblue/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> cloud </groupId>
<artifactId>spire.cloud.sdk</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId> de.gson</groupId>
comma <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,可参考这里的导入方法。
导入结果:
步骤二、登录冰蓝云账号,创建文件夹,上传文档
步骤三、创建应用程序,获取App ID及App Key
完成以上步骤后,可参考以下代码示例进行文档转换。
步骤四、代码示例
1. Word转为PDF
将Word转为PDF时,可通过convert()方法直接指定转换的格式为PDF格式,或者通过convertToPdf()方法在转换成PDF时嵌入字体文件到PDF。
1.1 直接通过convert()方法转换,这里转换可支持除PDF格式外,其他多种文件格式
import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.ConvertApi;
public class WordToPDF {
//配置App账号信息
static String appId = "App ID";
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.ConvertApi;
public class WordToPDF {
//配置App账号信息
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "api.e-iceblue";
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
static ConvertApi convertApi = new ConvertApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
String name = "Test.docx";//用于测试的Word源文档
String format = "pdf";//转换的目标文档格式,具体可包括多种文档格式,如postScript, pcl, dot, dotx, dotm, docm, odt, wordxml, wordml, pdf, doc, docx, rtf, epub, xps, html
String password = null;//源文档密码
String folder = "input";//源文档所在文件夹java在线导航
prcs4完全自学手册 String storage = null;//冰蓝云提供的2G免费存储空间
String destFilePath = "output/convertWordToPDF.pdf";//转换后的结果文档路径(这里保存在output文件夹下)
static String baseUrl = "api.e-iceblue";
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
static ConvertApi convertApi = new ConvertApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
String name = "Test.docx";//用于测试的Word源文档
String format = "pdf";//转换的目标文档格式,具体可包括多种文档格式,如postScript, pcl, dot, dotx, dotm, docm, odt, wordxml, wordml, pdf, doc, docx, rtf, epub, xps, html
String password = null;//源文档密码
String folder = "input";//源文档所在文件夹java在线导航
prcs4完全自学手册 String storage = null;//冰蓝云提供的2G免费存储空间
String destFilePath = "output/convertWordToPDF.pdf";//转换后的结果文档路径(这里保存在output文件夹下)
//调用方法转换文档格式
convertApi.convert(name, format, destFilePath, password, folder, storage);
}
}
1.2转换时,嵌入字体文件
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.ConvertApi;
import spire.cloud.word.del.PrivateFont;
import java.util.ArrayList;
import java.util.List;
public class WordToPDF {
import spire.cloud.word.sdk.client.api.ConvertApi;
import spire.cloud.word.del.PrivateFont;
import java.util.ArrayList;
import java.util.List;
public class WordToPDF {
//配置App账号信息
static String appId = "App ID";
static String appKey = "APP Key";
static String baseUrl = "api.e-iceblue";
static Configuration wordConfiguration = new 文件格式转换appConfiguration(appId, appKey, baseUrl);
static ConvertApi convertApi = new ConvertApi(wordConfiguration);
public static void main(String[] args) throws ApiException{
String name = "Test.docx";//用于测试的Word源文档
String password = null;//源文档密码
String folder = "input";//源文档所在文件夹
String storage = null;//冰蓝云提供的2G免费存储空间
String destFilePath = "output/convertWordToPdf.pdf";//转换后的结果文档路径(这里保存在output文件夹下)
static String appId = "App ID";
static String appKey = "APP Key";
static String baseUrl = "api.e-iceblue";
static Configuration wordConfiguration = new 文件格式转换appConfiguration(appId, appKey, baseUrl);
static ConvertApi convertApi = new ConvertApi(wordConfiguration);
public static void main(String[] args) throws ApiException{
String name = "Test.docx";//用于测试的Word源文档
String password = null;//源文档密码
String folder = "input";//源文档所在文件夹
String storage = null;//冰蓝云提供的2G免费存储空间
String destFilePath = "output/convertWordToPdf.pdf";//转换后的结果文档路径(这里保存在output文件夹下)
//当系统上没有安装Word文档中使用的字体时,可以通过加载字体文件将字体添加到pdf文档中,以便绘制文本
List<PrivateFont> privateFonts = new ArrayList<PrivateFont>();
privateFonts.add(new PrivateFont("华文中宋", "f"));
privateFonts.add(new PrivateFont("consola", "f"));
//调用方法将Word转为PDF
convertApi.convertToPdf(name, destFilePath, password, folder, storage, privateFonts);
}
}
List<PrivateFont> privateFonts = new ArrayList<PrivateFont>();
privateFonts.add(new PrivateFont("华文中宋", "f"));
privateFonts.add(new PrivateFont("consola", "f"));
//调用方法将Word转为PDF
convertApi.convertToPdf(name, destFilePath, password, folder, storage, privateFonts);
}
}
2. Word转Html
这里转为Html同样也可以通过1.1里面的转换方法,或者通过convertToHtml()方法来转换
import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.ConvertApi;
public class WordToHtml {
//配置App账号信息
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "api.e-iceblue";
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
static ConvertApi convertApi = new ConvertApi(wordConfiguration);
public static void main(String[] args) throws ApiException{
String name = "Test.docx";//用于测试的Word源文档
String password = null;//源文档密码
String folder = "input";//源文档所在文件夹
divcss是什么意思import spire.cloud.word.sdk.client.api.ConvertApi;
public class WordToHtml {
//配置App账号信息
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "api.e-iceblue";
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
static ConvertApi convertApi = new ConvertApi(wordConfiguration);
public static void main(String[] args) throws ApiException{
String name = "Test.docx";//用于测试的Word源文档
String password = null;//源文档密码
String folder = "input";//源文档所在文件夹
String storage = null;//冰蓝云提供的2G免费存储空间
String destFolder = "output/convertWordToHtml.html";//结果文档路径
//调用方法将Word转为HTML
convertApilinux操作系统的版本分为.convertToHtml(name, destFolder, password, folder, storage);
}
}
String destFolder = "output/convertWordToHtml.html";//结果文档路径
//调用方法将Word转为HTML
convertApilinux操作系统的版本分为.convertToHtml(name, destFolder, password, folder, storage);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论