最近需要將實現一個word和html,pdf相互轉換的功能,考慮到了很多技術,例如 POI,Freemarker等等,發現都有不少缺陷,
例如: POI轉換的表格樣式丟失,freemarker導出的word文件格式不行(PS:freemarker導出的word使用的是xml,轉換出來的word格式非標准格式,POI不支持轉html,freemarker直接轉html的話雖然也行,但是無法做到一個文件多種格式轉換)
於是找到了 Aspose.Words這個組件,Aspose.Words支持Doc,Docx,RTF,HTML,OpenDocument,PDF,XPS,EPUB和其他格式。可以在不使用Microsoft.Word的情況下生成、修改、轉換和打印文檔。
Aspose.Words是收費的,我找到了破解的license.xml,並做了第二次封裝.將license.xml直接封裝到jar包,並提供驗證license方法,
下載地址:
鏈接:https://pan.baidu.com/s/1p_A_P7F1G9wGMkfT9nFMkA
提取碼:1234
將jar包引入到項目即可
1. word轉pdf操作/**
* Word轉PDF操作 *@param sourcerFile 源文件 *@param targetFile 目標文件 */ public static void doc2pdf(String sourcerFile,String targetFile) { LicenseLoad.getLicense(); //驗證License 若不驗證則轉化出的pdf文檔會有水印產生
try { long old = System.currentTimeMillis(); File file = new File(targetFile); //新建一個空白pdf文檔 FileOutputStream os = new FileOutputStream(file); Document doc = new Document(sourcerFile); //sourcerFile是將要被轉化的word文檔 doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互轉換 os.close(); long now = System.currentTimeMillis(); System.out.println("共耗時:" + ((now - old) / 1000.0) + "秒"); //轉化用時 } catch (Exception e) { e.printStackTrace(); } }
2. word 轉html操作
/** * 將word的內容轉為html返回字符串,圖片全部轉為base64編碼。 * @param in * @return */ public static String wordToHtml(InputStream in) { LicenseLoad.getLicense();// 驗證License 若不驗證則轉化出的文件會有水印產生 ByteArrayOutputStream htmlStream = new ByteArrayOutputStream(); String htmlText = ""; try { Document doc = new Document(in); HtmlSaveOptions opts = new HtmlSaveOptions(SaveFormat.HTML); opts.setExportXhtmlTransitional(true); opts.setExportImagesAsBase64(true); opts.setExportPageSetup(true); doc.save(htmlStream,opts); htmlText = new String(htmlStream.toByteArray(), StandardCharsets.UTF_8); htmlStream.close(); } catch (Exception e) { e.printStackTrace(); } return htmlText; }
3. html 轉 word
/** * html內容轉word * @param html html內容 * @param wordPath word保存路徑 * @return */ public static boolean htmlToWord(String html,String wordPath) { LicenseLoad.getLicense(); try { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.insertHtml(html); //生成doc文件 doc.save(wordPath, SaveOptions.createSaveOptions(SaveFormat.DOC)); return true; } catch (Exception e) { e.printStackTrace(); } return false; }
4. 其他轉換: 以上3個方法都是示例,對於 Aspose.words支持的其他操作的,代碼流程如下
/** * 其他操作方法 */ public static void otherOperate () { //加載監聽,用於去除水印 LicenseLoad.getLicense(); //apose.words的其他操作,詳見相關API ......... }
PS: 其他操作可以見 Aspose.words 的相關文檔,這里有個博主翻譯過,附上鏈接: Aspose.words文檔
5. 此jar包已封裝了針對以上 1,2,3 轉換操作的轉換方法,我在二次封裝的時候已經一並打包進去了,可以直接調用,如下
public static void main(String[] args) { try { String wordPath = "E:\\test.doc"; //word轉html FileInputStream inputStream = new FileInputStream(new File(wordPath)); String htmlStr = WordConverUtil.wordToHtml(inputStream); inputStream.close(); //word 轉 pdf String pdfPath = "E:\\test.pdf"; WordConverUtil.wordToPdf(wordPath, pdfPath); //html轉word HtmlConverUtil.htmlToWord(htmlStr, wordPath); } catch (Exception e) { e.printStackTrace(); } }
PS : Aspose.Words 為商用軟件,為避免糾紛,如需商用,還是建議請購買正版.