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

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

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Aspose.Words插件是一个用于处理Word文档的强大工具。它可以读取、写入和转换各种Word文档格式,包括doc、docx、rtf、html、xml等。在Unity中使用Aspose.Words插件读取Word文档可以实现将文本、表格和图像在UI上显示的功能。 以下是使用Aspose.Words插件读取Word文档并在UI上显示的步骤: 1. 下载Aspose.Words插件并将其导入Unity项目中。 2. 创建一个空的UI界面,用于显示Word文档的内容。 3. 编写代码,使用Aspose.Words插件读取Word文档并将其内容显示在UI界面上。 以下是代码示例: ```csharp using UnityEngine; using UnityEngine.UI; using Aspose.Words; using Aspose.Words.Tables; public class WordDocumentReader : MonoBehaviour { public TextAsset wordDocument; public Text textObject; public GameObject tableObject; public GameObject imageObject; private Document document; void Start() { LoadDocument(); DisplayContent(); } void LoadDocument() { document = new Document(wordDocument.bytes); } void DisplayContent() { foreach (Section section in document.Sections) { foreach (Paragraph paragraph in section.Body.Paragraphs) { string text = paragraph.GetText().Trim(); if (!string.IsNullOrEmpty(text)) { textObject.text += text + "\n"; } } foreach (Table table in section.Body.Tables) { GameObject newTable = Instantiate(tableObject, transform); TableLayoutGroup tableLayoutGroup = newTable.GetComponent<TableLayoutGroup>(); tableLayoutGroup.CreateTable(table); } foreach (Shape shape in section.Body.GetChildNodes(NodeType.Shape, true)) { if (shape.HasImage) { Image image = Instantiate(imageObject, transform).GetComponent<Image>(); image.sprite = Sprite.Create(shape.ImageData.ToStream(), new Rect(0, 0, shape.ImageData.ImageSize.Width, shape.ImageData.ImageSize.Height), Vector2.zero); } } } } } ``` 这段代码首先通过LoadDocument方法加载Word文档。然后,在DisplayContent方法中,它遍历文档的每个段落、表格和图像,并将它们显示在UI界面上。如果文本不为空,则将其添加到Text组件中。如果存在表格,则使用TableLayoutGroup组件创建表格。如果存在图像,则将其添加到Image组件中。 通过这种方式,使用Aspose.Words插件可以实现Word文档的文本、表格和图像在Unity中显示的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值