java下freemarker2.3.28+aspose实现生成word文档生成、word转pdf转换

2021/4/30 20:28:29

本文主要是介绍java下freemarker2.3.28+aspose实现生成word文档生成、word转pdf转换,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、java生成word文档

java生成word文档常用的两种方式:

(1)apache下的poi类库处理office文档

(2)freemarker编辑word文件转换的xml文件

通过网上了解,第(1)种方式使用poi操作数据相对麻烦些,对于word07兼容些好些,第(2)种方式使用freemarker的标签处理数据极为方便,生成doc格式文件不会有兼容问题,但是生成docx文件会有兼容性问题,office07以上版本不能直接打开(wps不受影响),但是可以通过程序进行另存为处理后即可解决

下面使用freemarker生成word文件:

导入依赖包:(为了方便使用freemarker,此处直接导入的starter)

<dependency>
	<groupId>org.freemarker</groupId>
	<artifactId>freemarker</artifactId>
	<version>2.3.28</version>
</dependency>

1.将需要生成word文件另外为xml格式(word XML或word 2003 XML都可以),用文本管理器打开会发现xml标签内容的文件啦,可以在里面写freemarker标签了

2.将xml后缀名改成ftl后缀

3.使用freemarker对ftl文件进行渲染

import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.*;

/**
 * 创建word文件
 * @param clazz
 * @param path 模板文件所在路径
 * @param template 模板文件名
 * @param newWordName 生成的文件名
 * @param dataMap 渲染数据
 */
public static void generateWord(Class clazz, String path,String template, String newWordName,Map<String, Object> dataMap) {
    try {
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
        configuration.setClassForTemplateLoading(clazz,path);
        Template temp = null;
        //加载模板文件
        temp = configuration.getTemplate(template,"utf-8");
        File outFile = null;
        Writer out = null;
        String filename = newWordName;
        outFile = new File(newWordName);
        out = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(outFile),"utf-8"));
        //渲染数据
        temp.process(dataMap, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

4.渲染后的文件后缀名存为doc,若存为docx,office03以上版本,打开会出错,需要java模拟另存为高版本文件可以解决

二、java word转pdf

doc转pdf最常见的有一下几种方式

(1)使用com.documents4j

(2)使用 com.aspose.words

(3)使用poi、itext

(4)使用openoffice

第(1)种:优点:转换简单,windows环境下几行代码即可实现完美转换,缺点:Linux下需要安装ms-office插件(插件据说官方未提供),否则会出现转换失败。windows服务器环境下有兴趣朋友可以尝试下

见:https://blog.csdn.net/lihaiyang722/article/details/108405355

第(2)种:优点:转换简单,几行代码即可转换,缺点:aspose包依赖下载会失败,需要从第三方下载后手动放到仓库进行依赖,其次,如果部署到Linux下,环境中必须含有和window环境下的字库相一致,否则会出现乱码。

第(3)(4)操作起来相对麻烦些,见:

https://blog.csdn.net/zhangjunli/article/details/104940663

https://www.cnblogs.com/ph7seven/archive/2018/12/21/10158489.html

所以我这边采用第(2)种方案:

导入依赖,jar包从第三方下载放入maven仓库

<dependency>
	<groupId>com.aspose</groupId>
	<artifactId>aspose-words-jdk16</artifactId>
	<version>15.8.0</version>
	<classifier>jdk16</classifier>
</dependency>

word转pdf实现: 

import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import java.io.*;

/**
 *word转pdf
 * @param docPath word文件路径
 * @param pdfPath pdf文件路径
 */
public static void doc2pdf(String docPath,String pdfPath){
    File pdfFile = new File(pdfPath);
    try {
        String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
        ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
        License license = new License();
        license.setLicense(is);
        com.aspose.words.Document document = new com.aspose.words.Document(docPath);
        document.save(new FileOutputStream(pdfFile), SaveFormat.PDF);
    } catch (Exception e) {
        logger.info("doc转pdf异常");
        e.printStackTrace();
    }
};

linux下乱码字符处理:

https://blog.csdn.net/qq_40102178/article/details/100738793

https://blog.csdn.net/qq_27319683/article/details/103344196

 

 

 

 



这篇关于java下freemarker2.3.28+aspose实现生成word文档生成、word转pdf转换的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程