aspose for java是一款非常好用的文档件转换的工具,还原度贼高
而且支持多种格式转换
现在就其中5种进行转换
1.首先下载jar
aspose.words for java文档转换工具类
下载完成后,导入到自己的项目中这个工具类不开源,如不使用aspose_license.jar会出现水印,但是我提供的资源已经有啦,就不用担心
2.创建你的文件转换的工具类
在自己项目中,创建一个工具类,取名随意,我是这么取得
public class ConversionFile(){}
2.0,准备工作~~~~这是我导入的工具类
PS:下列代码基于我的实体类和详情类进行编写,不然会直接编译错误
import java.io.*;
import com.aspose.cells.Workbook;
import com.aspose.words.*;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.lanyuan.entity.VO.fileDomainVO;
import apach.tools.Details;
import junit.framework.Test;
- 其中fileDomainVO和Details都是我自己项目中文件操作实体类和描述工具类
- 为了方便理解,我贴一下我实体类代码和详情类的代码
//创建fileDomainVO实体类
//复制好后,记得添加set,get方法
private Integer id;
private Date time; //操作时间
private String filename; //原文件名
private String fileNameAfter; //转换后文件名
private String filesize; //原文件大小
private String filetype; //原文件类型
private String filetypeafter; //后文件类型
private String details; //操作详情
private String outputfile; //保存路径
private String inputfile; //原文件路径
private Integer pageno;
private Integer pages;
private Integer rid;
private Integer pageoperation;
private Integer pagestart;
private Integer pageend;
private String fileSizeAfter; //后 文件大小
private Integer status; //操作状态
private Integer afterpages;
private String detail;
private String timeConsuming; //消耗时长
//--------华丽丽的分割线------------注意这是两个类,童鞋们可以在自己项目中创建这两个类哦~~~
//创建Details类.
//这个类不用set,get
public static Integer SuccessStatus=0; //状态:成功
public static Integer FailStatus=1; //状态:失败
public static String DocToPDF="DocToPDF";
public static String DocToDocx="DocToDocx";
public static String DocToTexT="DocToTexT";
public static String DocToXps="DocToXps";
public static String HtmlToDoc="HtmlToDoc";
public static String DocToJPEG="DocToJPEG";
2.1进行License验证 ,这一步骤是为了去水印
PS:license.xml的路径记得选好,不然会抛找不到文件异常
/**
* 验证License
* @return boolean
*/
public static boolean getLicense() {
boolean result = false;
try {
//license.xml
//如果不知道应该放在哪个路径,就在控制台打印一下,然后去放就好啦
//System.out.println(Test.class.getClassLoader().getResource("").getPath());
InputStream is = Test.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
2.2对word进行转换操作
/**
* word转换
* 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
* @param
*/
public static fileDomainVO Conversion(fileDomainVO vo) {
//默认失败
vo.setStatus(Details.FailStatus);
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
System.out.println("验证失败,会产生水印");
}
//创建一个doc对象
Document doc =null;
try {
//统计时间
long old = System.currentTimeMillis();
//初始化定义
File file =null;
ImageSaveOptions iso = null;
//输出流
FileOutputStream os =null;
//判断是否转换图片
if(vo.getDetails().equals(Details.DocToJPEG)){
iso = new ImageSaveOptions(SaveFormat.JPEG);
}else{
//file.mkdirs();
//新建一个空白文档
file = new File(vo.getOutputfile());
//创建文件夹
file.mkdirs();
file = new File(vo.getOutputfile()+vo.getFileNameAfter());
os = new FileOutputStream(file);
}
//getInputfile是将要被转化文档
doc = new Document(vo.getInputfile());
//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
//------------------------------------------逻辑判断
//>>>>>>DocToPDF
if(vo.getDetails().equals(Details.DocToPDF)){
doc.save(os, SaveFormat.PDF);
}//>>>>>>DocToDocx
else if(vo.getDetails().equals(Details.DocToDocx)){
doc.save(os, SaveFormat.DOCX);
}//>>>>>>DocToTexT
else if(vo.getDetails().equals(Details.DocToTexT)){
doc.save(os, SaveFormat.TEXT);
}//>>>>>>DocToXps
else if(vo.getDetails().equals(Details.DocToXps)){
doc.save(os, SaveFormat.XPS);
}//>>>>>>HtmlToDoc
else if(vo.getDetails().equals(Details.HtmlToDoc)){
doc.save(os, SaveFormat.DOC);
}//>>>>>>DocToJPEG
else if(vo.getDetails().equals(Details.DocToJPEG)){
//iso.setResolution(128);
iso.setPrettyFormat(true);
iso.setUseAntiAliasing(true);
for (int i = 0; i < doc.getPageCount(); i++)
{
iso.setPageIndex(i);
doc.save(vo.getOutputfile() + vo.getFileNameAfter() +"---"+(i+1)+ ".jpeg", iso);
}
}
//------------------------------------------逻辑判断
//统计时间
long now = System.currentTimeMillis();
//System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"+"文件保存在:" + vo.getOutputfile());
//设置时间
vo.setTimeConsuming((((now - old) / 1000.0)+"").trim());
//成功
vo.setStatus(Details.SuccessStatus);
} catch (Exception e) {
e.printStackTrace();
}
return vo;
}
2.2.0 Main内测试
public static void main(String[] args) throws Exception {
fileDomainVO vo = new fileDomainVO();
vo.setDetails(Details.DocToPDF); //进行word转pdf
vo.setInputfile("H:/test/123.doc"); //需要转换的word
vo.setOutputfile("H:/test/"); //保存路径
vo.setFileNameAfter("123.pdf"); //转换后的文件名,自己取
Conversion(vo); //开始转换
}
在H:/test/中,生成了123.pdf文件
PS:
- license.xml的路径记得选好,在main方法里运行时和在用tomcat跑起来的时候,读取的路径有时会不一样
- 图片转换是当前word有几页,图片就会转几张,所以取名的时候要进行循环
- 大家根据自己项目的需要可以自己去组装转换条件
- 有些可以互相转换,比如word转html,html转word,但是word转html最好是纯文本,无样式转换,
- 大家可以自己去参考官方文档,试一下其他类型的转换