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 ...
随机推荐
- SVM之不一样的视角
在上一篇学习SVM中 从最大间隔角度出发,详细学习了如何用拉格朗日乘数法求解约束问题,一步步构建SVM的目标函数,这次尝试从另一个角度学习SVM. 回顾监督学习要素 数据:(\(x_i,y_i\)) ...
- for嵌套setTimeout的心得
export default { data() { return { dialogList: [] } }, created() { this.setList() }, methods: { setL ...
- 在c++中引用c头文件里的函数
在c++中有的时候想要引用c头文件里的函数有两种方法;就拿c语言里面的<stdlib.h>举例 在c中我们想要用<stdlib.h>里的函数,形式为:#include<s ...
- VideoView--简单的设置全屏幕播放
我说的最主要的是要在布局哪里设置一下,如: <com.example.mypalyer.fullScreen android:id="@+id/videoView1& ...
- android位运算简单讲解
一.前言 在查看源码中,经常会看到很多这样的符号“&”.“|”.“-”,咋一看挺高大上:仔细一看,有点懵:再看看,其实就是大学学过的再普通不过的与.或.非.今天小盆友就以简单的形式分享下,同时 ...
- js 实现淘宝放大镜功能,可更改配置参数 带完整版解析代码[magnifier.js]
前言: 本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽. 本篇文章为您分析一下原生JS写淘宝放大镜效果 基本功能: 运 ...
- 编程语言50年来的变化,我用50种编程语言告诉你“Hello world”怎么写!
当我们学习一门新的语言时,"Hello, World!"通常是我们所写的第一个程序. 因此,所有程序员在职业生涯中至少完成了"Hello, World!"程序员 ...
- Android APP 性能测试之 GT 工具
一.介绍: GT(随身调)是 APP 的随身调测平台,它是直接运行在手机上的"集成调测环境"(IDTE, Integrated Debug Environment).利用 GT,仅 ...
- requests抓取数据示例
1:获取豆瓣电影名称及评分 # 抓取豆瓣电影名称及评分 url="https://movie.douban.com/j/search_subjects" start=input(& ...
- MySQL 子查询——查询最大值
子查询指将一个查询语句嵌套在另一个查询语句中.子查询可以在 SELECT.UPDATE 和 DELETE 语句中使用,而且可以进行多层嵌套.在实际开发时,子查询经常出现在 WHERE 子句中.子查询在 ...