00003-aspose for java 生成水印刻章等,可转为word,pic,pdf
对应java代码:
package com.yoooya.ytp.utils.doc;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.yoooya.ytp.utils.DateUtils;
import com.yoooya.ytp.utils.IdUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* cjianquan 2020/1/3
*/
public class ExportWord {
private Configuration configuration = null;
private static int i = 9;
public ExportWord() {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
}
public File createWordToPic(Map<String, Object> dataMap){
try{
String path = ExportWord.class.getResource("/").getPath()+"aspose/";
File pathFile=new File(path);
if(!pathFile.exists()){
pathFile.mkdirs();
}
return createWord(dataMap,path,"wcsyzm.ftl");
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public File createWord(Map<String, Object> dataMap, String filePath, String tmpName) {
try{
configuration.setDirectoryForTemplateLoading(new File(filePath));
Template t = null;
try {
t = configuration.getTemplate(tmpName); // 文件名
} catch (IOException e) {
e.printStackTrace();
}
String fileName = String.valueOf(IdUtils.id()) + ".doc";
String pngFileName = String.valueOf(IdUtils.id()) + ".png";
File outFile = new File(filePath+"temp", fileName);
File outPngFile = new File(filePath+"temp", pngFileName);
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile), "utf-8"));
} catch (Exception e1) {
e1.printStackTrace();
}
try {
t.process(dataMap, out);
out.close();
getLicense(filePath);
FileOutputStream os = new FileOutputStream(outPngFile);
FileInputStream iStream = new FileInputStream(outFile);
Document doc = new Document(iStream);
// doc.save(os, com.aspose.words.SaveFormat.PDF);
doc.save(os, SaveFormat.PNG);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return outPngFile;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/** 获取注册文件
*/
public static void getLicense(String filePath) {
String path = filePath + "/license.xml";
InputStream is;
try {
is = new FileInputStream(new File(path));
License license = new License();
license.setLicense(is);
} catch (FileNotFoundException e) {
// logger.error("license.xml file not found");
} catch (Exception e) {
// logger.error("license register failed");
}
}
public static void doc2pdf(String wordPath, String pdfPath) {
try {
long old = System.currentTimeMillis();
File file = new File(pdfPath); // 新建一个pdf文档
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(wordPath); // Address是将要被转化的word文档
doc.save(os, com.aspose.words.SaveFormat.PDF);
long now = System.currentTimeMillis();
os.close();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
} catch (Exception e) {
e.printStackTrace();
}
}
public static String GetImageStr(String imgFilePath) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
String basePath = ExportWord.class.getResource("/").toString();
basePath = basePath.replaceAll("file:/","");
byte[] data = null;
// 读取图片字节数组
try {
InputStream in = new FileInputStream(basePath+"aspose/"+imgFilePath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);// 返回Base64编码过的字节数组字符串
}
private void getData(Map<String, Object> dataMap) {
dataMap.put("company", "原力肽");
dataMap.put("address", "某某地址");
dataMap.put("csmj", "100");
dataMap.put("userCompany", "陈建泉个体户");
dataMap.put("dateStart", "2019年12月1日");
dataMap.put("dateEnd", "2020年12月1日");
dataMap.put("nowDate", "2020年1月3日");
dataMap.put("imageData", GetImageStr("seal.png"));
// dataMap.put("address", "福建省泉州市鲤城区");
// dataMap.put("phone", "13459260612");
}
public static void main(String[] args) throws Exception {
/*ExportWord test = new ExportWord();
String basePath = ExportWord.class.getResource("/").toString();
basePath = basePath.replaceAll("file:/","");
System.out.println("basePath="+basePath);
Map<String, Object> dataMap = new HashMap<String, Object>();
test.getData(dataMap);
test.createWord(dataMap, basePath+"aspose/","wcsyzm.ftl");*/
ExportWord exportWord = new ExportWord();
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("company", "company11");
dataMap.put("managerName","managerName22");
dataMap.put("address", "address33");
dataMap.put("csmj", "csmj44");
dataMap.put("userCompany", "userCompany55");
dataMap.put("dateStart", "yyyy年MM月dd日 666");
dataMap.put("dateEnd", "yyyy年MM月dd日777");
dataMap.put("nowDate", DateUtils.format(new Date(),"yyyy年MM月dd日"));
dataMap.put("imageData", exportWord.GetImageStr("seal.png"));
File picFile = exportWord.createWordToPic(dataMap);
System.out.println("picFile="+picFile.getAbsolutePath());
}
}
其中 license.xml 是aspose for java 的license文件。
wcsyzm.ftl 文件,是有doc文件另存为ftl得到了。
dataMap的key值,对应的是ftl的参数,类似:${company}
原始word的内容如图:

seal.png 是一个要加的水印 或者刻章图片。。
00003-aspose for java 生成水印刻章等,可转为word,pic,pdf的更多相关文章
- Java 将PDF 转为Word、图片、SVG、XPS、Html、PDF/A
本文将介绍通过Java编程来实现PDF文档转换的方法.包括: 1. PDF转为Word 2. PDF转为图片 3. PDF转为Html 4. PDF转为SVG 4.1 将PDF每一页转为单个的SVG ...
- Java生成验证码原理(jsp)
验证码的作用: 验证码是Completely Automated Public Turing test to tell Computers and Humans Apart(全自动区分计算机和人类的 ...
- java实现word转pdf在线预览(前端使用PDF.js;后端使用openoffice、aspose)
背景 之前一直是用户点击下载word文件到本地,然后使用office或者wps打开.需求优化,要实现可以直接在线预览,无需下载到本地然后再打开. 随后开始上网找资料,网上资料一大堆,方案也各有不同,大 ...
- 面试题-关于Java线程池一篇文章就够了
在Java面试中,线程池相关知识,虽不能说是必问提,但出现的频次也是非常高的.同时又鉴于公众号"程序新视界"的读者后台留言让写一篇关于Java线程池的文章,于是就有本篇内容,本篇将 ...
- Thinking in Java学习杂记(5-6章)
Java中可以通过访问控制符来控制访问权限.其中包含的类别有:public, "有好的"(无关键字), protected 以及 private.在C++中,访问指示符控制着它后面 ...
- Aspose.Words简单生成word文档
Aspose.Words简单生成word文档 Aspose.Words.Document doc = new Aspose.Words.Document(); Aspose.Words.Documen ...
- Java生成和操作Excel文件(转载)
Java生成和操作Excel文件 JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该A ...
- 利用JAVA生成二维码
本文章整理于慕课网的学习视频<JAVA生成二维码>,如果想看视频内容请移步慕课网. 维基百科上对于二维码的解释. 二维条码是指在一维条码的基础上扩展出另一维具有可读性的条码,使用黑白矩形图 ...
- JAVA生成条形码
1.下载生成条形码所需要的jar包barcode4j.jar: 2.java生成条形码代码 import java.awt.image.BufferedImage;import java.io.Fil ...
随机推荐
- 从源码解读Spring如何解决bean循环依赖
1 什么是bean的循环依赖 循环依赖的原文是circular reference,指多个对象相互引用,形成一个闭环. 以两个对象的循环依赖为例: Spring中的循环依赖有 3 种情况: 构造器(c ...
- Zookeepe的安装和集群的配置
Zookeepe的安装和集群的配置 一.Zookeeper的简介 Zookeeper是个框架 二.Zookeeper在Linux上安装 官方提供的下载服务器 地址1:http://mirror.bit ...
- docker中安装宝塔面板教程
本人电脑是win10,安装的virtualbox,装的centos7.2,在centos7.2装了docker,这个比较简单,网上一大堆教程,今天说一下装好了docker之后怎么在docker中安装面 ...
- How to use QueryPerformanceCounter? (c++,不使用 .Net)
出处:https://stackoverflow.com/questions/1739259/how-to-use-queryperformancecounter 参考:https://docs.mi ...
- 实用的linux 命令(上)
今天介绍几个我常用的Linux 命令,每个命令这里只介绍其常用参数. 对于每个Linux 命令都可以使用man + 命令名称,查看其完整使用方法. 0,man man 命令是一个非常有用的命令,当你不 ...
- 使用spring boot创建fat jar APP
文章目录 介绍 build和run fat jar和 fat war 更多配置 介绍 在很久很很久以前,我们部署web程序的方式是怎么样的呢?配置好服务器,将自己写的应用程序打包成war包,扔进服务器 ...
- python实现逻辑回归
首先得明确逻辑回归与线性回归不同,它是一种分类模型.而且是一种二分类模型. 首先我们需要知道sigmoid函数,其公式表达如下: 其函数曲线如下: sigmoid函数有什么性质呢? 1.关于(0,0. ...
- HTML模版组件
1.介绍 BootStrap (最好看的) css js [x] 学习 BootStrap 规则 jQueryUI (美观一般,偏后台.*) css js [x] 学习 jQueryUI 规则 Eas ...
- ajax 技术
ajax 技术 $.ajax({ url:"", type:'GET', success:function(data){ console.log(data); }, error:f ...
- UVA10599:Robots(II)(最长上升子序列)
Your company provides robots that can be used to pick up litter from fields after sporting events an ...