使用Aspose.Words将实现word,pdf,html的相互转换,去水印和头部文字版本(附jar包)

您所在的位置:网站首页 word文档变成doc文件 使用Aspose.Words将实现word,pdf,html的相互转换,去水印和头部文字版本(附jar包)

使用Aspose.Words将实现word,pdf,html的相互转换,去水印和头部文字版本(附jar包)

2023-07-06 02:53| 来源: 网络整理| 查看: 265

自动永久激活office/visio 2021,2019,2016,2013、win10/win11工具,仅需15元,点击购买!

出chatgpt独享账号!内含120美元!仅需38元/个!独享永久使用!点击购买!

   最近需要将实现一个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 为商用软件,为避免纠纷,如需商用,还是建议请购买正版.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3