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最好是純文本,無樣式轉換,
- 大家可以自己去參考官方文檔,試一下其他類型的轉換