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 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;
    }

结束语

  • 暂时我用到只有这些,如果有不足之处希望各位大手批评指正。
  • 有需要word文档需要表格的有关操作可以参考:http://www.cnblogs.com/wuhuacong/archive/2012/08/30/2662961.html
  • 官方文档地址:https://docs.aspose.com/display/wordsnet/Converting+a+Document
  • 相关文件
https://pan.baidu.com/s/1nMnJsyxz57Bjri7QH5uTkw
提取码:f7py

你可能感兴趣的:(java-Aspose.Words的使用)