java-Aspose.Words的使用

概述

  • Aspose.Words是一个商业.NET类库,可以使得应用程序处理大量的文件任务。Aspose.Words支持Doc,Docx,RTF,HTML,OpenDocument,PDF,XPS,EPUB和其他格式。使用Aspose.Words可以在不使用Microsoft.Word的情况下生成、修改、转换和打印文档。

在项目中使用Aspose.Words有以下好处

  • 其丰富的功能特性主要有以下4个方面:
    1)格式转换。Aspose.Words具有高质量的文件格式转换功能,可以和Doc,OOXL,RTF,TXT等格式互相转换。
    2)文档对象模型。通过丰富的API以编程方式访问所有的文档元素和格式,允许创建,修改,提取,复制,分割,加入,和替换文件内容。
    3)文件渲染。可以在服务器端转换整个文档或者页面为PDF,XPS,SWF格式,同样可以转换文档页面为图像格式,或者.NET Graphics对象,这些功能和Microsoft.Word是一样的。
    4)报表。可以从对象或者数据源填充模版生成文件。

使用组件

  • 用到的jar包
    aspose-words-18.5.0718-jdk16.jar
  • 用到的文件
    license.xml

使用域插入替换文件

  • 模板中插入域
    插入——>文档部件——>域



  • 结果
  • 代码
// 参数1 request请求   参数2 想要替换的数据(map中键名要和模板文件中域名相同) 参数3 文件名 根据自己实际需求调整
private  File createFile2(HttpServletRequest request,PageData dataMap,String fileName) throws Exception {
        //获取文件的路径,根据要读取文件路径自己完成不过多赘述
        String path = PathUtil.getClassResources() + "ftl/template/我的模板文件.doc";
        File file = new File(path);
        //拼接生成文件的路径
        String tpath = request.getServletContext().getRealPath("/uploadFiles/uploadFile/");
        File filepath = new File(tpath);
        if (!filepath.exists()) {
            filepath.mkdirs();
        }
        //要生成的文件地址 注意如想生成pdf,此处为.pdf后缀,其他文件写法相同,这里用png举例
        FileOutputStream out1 = new FileOutputStream(tpath + "/" +fileName + ".png");
        File targetFile = new File(tpath + "/"+fileName  + ".png");
        //调用去水印的方法 读取license.xml文件
        AsposeUtil.setWordsLicense1();
        Document document = new Document(new FileInputStream(file));
        String text = document.getText();
        DocumentBuilder builder = new DocumentBuilder(document);
       
        LinkedHashMap<String, Object> map = new LinkedHashMap(dataMap);
        if (map != null && !map.isEmpty()) {
            Set set = map.keySet();
            Iterator it = set.iterator();
            while (it.hasNext()) {
                String next = String.valueOf(it.next());//获取模板中的域名
                String s = String.valueOf(map.get(next));//通过域名获取想要在该域名处替换的数据
              //向域中添加图片
                if(next.equals("image")){
                    builder.moveToMergeField(next);
                    builder.insertImage(new FileInputStream(new File(s)), 80, 80);//设置图片的宽高,
                }
                //普通文本的数据替换
                if (builder.moveToMergeField(next)) {
                    builder.write(String.valueOf(s));
                }
               
            }
        }

       
       //注意SaveFormat的格式要与上面生成格式保持一致
        document.save(out1, SaveFormat.PNG);
        out1.flush();
        out1.close();
        return targetFile;
    }

使用书签插入替换文件

  • 模板中插入书签
    插入——>书签



  • 代码与域类似,不做过多赘述,文章最下方有官方文档
//跳转到书签
builder.moveToBookmark("书签名");

格式转换

  • License验证去水印
private static InputStream inputStream = null;
    //当前创建工具类名AsposeUtil
    private static Logger logger = Logger.getLogger(AsposeUtil.class);

    /**
     * 获取License的输入流
     *
     * @return
     */
    private static InputStream getLicenseInput() {
        if (inputStream == null) {
            ClassLoader contextClassLoader =AsposeUtil.class.getClassLoader();
            try {

                inputStream =contextClassLoader.getResourceAsStream("license.xml");
            } catch (Exception e) {
                logger.error("license not found!", e);
            }
        }
        return inputStream;
    }
public static boolean setWordsLicense() {
        InputStream licenseInput = getLicenseInput();
        if (licenseInput != null) {
            try {
                com.aspose.words.License aposeLic = new com.aspose.words.License();
//              俩种都可以,任选其一,这里用InputStream 
//              aposeLic.setLicense("这里是你license.xml的路径");
                aposeLic.setLicense(licenseInput);
                return aposeLic.getIsLicensed();
            } catch (Exception e) {
                logger.error("set words license error!", e);
            }
        }
        return false;
    }

结束语

https://pan.baidu.com/s/1nMnJsyxz57Bjri7QH5uTkw
提取码:f7py
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,464评论 0 5
  • 1.问:WORD里边怎样设置每页不同的页眉?如何使不同的章节显示的页眉不同? 答:分节,每节可以设置不同的页眉。文...
    勤奋的keena阅读 1,796评论 0 10
  • 1. 问:WORD 里边怎样设置每页不同的页眉?如何使不同的章节显示的页眉不同? 答:分节,每节可以设置不同的页眉...
    花开易见落难寻阅读 2,277评论 2 37
  • PDFelement 6 Pro Mac 破解版是一款大家十分熟悉的pdf编辑器。PDFelement 6 Pro...
    希希的莫斯呱阅读 14,115评论 0 2
  • 下雨️天,上午迟到了,上午依旧是理论知识➕演练,下午去探市场,我们跟队友分开行动,我去了帝凯大厦,之后去了恒隆,发...
    李丹spring阅读 368评论 0 2