文档格式转换在实际场景需求中是一种能够快速获得目标文档格式的方法,通常以借助于特定转换工具来实现。本文,以HTML转为XPS为例,通过Java后端程序来介绍如何实现转换。
Jar包引入
通过Maven仓库下载导入,如下配置pom.xml:
com.e-iceblue https://repo.e-iceblue.cn/repository/maven-public/ e-iceblue spire.doc.free 5.2.0
如需手动导入,需要下载jar包-Free Spire.Doc for Java到本地,然后解压,找到lib文件夹下的Spire.Doc.jar文件。在IDEA中打开“Project Structure”界面,执行如图步骤将本地路径下的jar文件手动引入Java程序:

将HTML转为XPS
转换时,只需在调用Document.saveToFile()方法时选择目标文件格式的枚举值为XPS即可。下面是转换时用到的主要类及方法步骤:
-
创建 Document 类的对象。
-
调用 Document.loadFromFile(String fileName, FileFormat fileFormat)方法加载HTML文件。
-
通过 Document.saveToFile(String fileName, FileFormat fileFormat) 方法保存为XPS格式到指定路径。
Java
import com.spire.doc.*;
public class HTMLtoXPS {
public static void main(String[] args) {
//创建Document类的对象
Document doc = new Document();
//加载HTML文件
doc.loadFromFile("sample.html",FileFormat.Html);
//保存为XPS格式
doc.saveToFile("HTMLtoXPS.xps",FileFormat.XPS);
doc.dispose();
}
}

—END—