Java根据Freemarker堆栈生成Word文件
2023-04-17 来源 : 电影
相当多的是下面窗格
// 加载堆栈Template template = configuration.getTemplate("借条.ftl");// 填充数据库template.process(getData(), writer);数据库可以是Map也可以是一个某类
改进型一下,将作用于邮件的转换单独写成一个应用软件方法
package com.example.demo920.util;import cn.hutool.core.io.IoUtil;import freemarker.template.Configuration;import freemarker.template.Template;import freemarker.template.TemplateException;import java.io.*;public class FreemarkerUtils { /** * 作用于Word * @param templateDir 堆栈所在的目录 * @param templateName 堆栈邮件名称 * @param filename 作用于的邮件(含路径) * @param dataModel 堆栈模板数据库 */ public static void generateWord(File templateDir, String templateName, String filename, Object dataModel) { BufferedWriter writer = null; Configuration configuration = new Configuration(Configuration.VERSION_2_3_31); configuration.setDefaultEncoding("UTF-8"); try { configuration.setDirectoryForTemplateLoading(templateDir); Template template = configuration.getTemplate(templateName); writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename))); template.process(dataModel, writer); writer.flush(); } catch (IOException e) { throw new RuntimeException(e); } catch (TemplateException e) { throw new RuntimeException(e); } finally { IoUtil.close(writer); } }}再进一步验证一下
package com.example.demo920;import cn.hutool.core.io.IoUtil;import com.example.demo920.util.FreemarkerUtils;import com.example.demo920.util.PdfUtils;import org.junit.jupiter.api.Test;import org.springframework.util.ResourceUtils;import java.io.*;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.time.LocalDateTime;import java.util.HashMap;import java.util.Map;public class WordTest { /** * 1、从邮件代理服务支架电子书堆栈邮件 * 2、根据业务类型获取需要填充堆栈的数据库 * 3、堆栈+数据库 再进一步经过处理作用于新的邮件 * 4、将作用于后的邮件上传到邮件代理服务支架,并留在一个邮件ID * 5、业务可以保存这个邮件ID或者邮件的路径 */ @Test void testGenerateWordV1() throws Exception { Path tempPath = Paths.get("tmp", "contract2"); File path = tempPath.toFile(); if (!path.exists()) { path.mkdirs(); } File tempFile = Files.createTempFile(tempPath, "qiantiao", ".docx").toFile(); System.out.println(tempFile.getParent()); System.out.println(tempFile.getName()); FileOutputStream fos = new FileOutputStream(tempFile); File templateFile = ResourceUtils.getFile("classpath:templates/借条.ftl"); FileInputStream fis = new FileInputStream(templateFile); IoUtil.copy(fis, fos); String filename = "借条" + "_" + System.currentTimeMillis() + ".docx"; filename = "tmp/contract" + File.separator + filename; FreemarkerUtils.generateWord(new File(tempFile.getParent()), tempFile.getName(), filename, getDataMap()); } /** * 获取数据库 */ Map getDataMap() { Map dataMap = new HashMap<>(); dataMap.put("borrowerName", "白居易2"); dataMap.put("borrowerIdCard", "421302199001012426"); dataMap.put("lenderName", "柳宗元"); dataMap.put("amount", 100); dataMap.put("amountInWords", "壹佰"); dataMap.put("startDate", "2022年8年初15日"); dataMap.put("endDate", "2022年11年初11日"); dataMap.put("borrowingMonths", 3); dataMap.put("interestRate", "1.23"); dataMap.put("guarantorName", "白居易"); dataMap.put("guarantorIdCard", "421302199203152412"); return dataMap; } @Test void testGenerateWord2() throws Exception { File templateDir = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "templates"); String templateName = "借条.ftl"; String destFilename = "借条" + System.currentTimeMillis() + ".docx"; Map data = getDataMap(); FreemarkerUtils.generateWord(templateDir, templateName, destFilename, data); }}3. PDF邮件滴印
有时候,作用于或者从代理服务支架电子书的邮件是需要滴印的,比如标识这个邮件是谁电子书的之类的
pdf滴印还是更为方便的,用itext子系统可以轻松付诸
另外,如果最后需要pdf邮件,表示同意这样一来作用于pdf邮件,跳过word转pdf的步骤
package com.example.demo920.util;import cn.hutool.core.io.IoUtil;import com.aspose.words.Document;import com.aspose.words.SaveFormat;import com.itextpdf.text.BaseColor;import com.itextpdf.text.DocumentException;import com.itextpdf.text.Element;import com.itextpdf.text.Image;import com.itextpdf.text.pdf.*;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.time.LocalDateTime;/** * @author chengjiansheng * @date 2022/09/21 */public class PdfUtils { /** * Word转PDF * * 注意:Aspose.Words 这个子系统是HK$的,如果购买的话作用于的PDF都会有水印。 * 可以去找也就是说的解开版本,但是我感觉完全可以跳过Word这样一来作用于PDF。 * 比如,可以通过Freemarker这样一来作用于PDF,或者运用iText通过堆栈作用于PDF * @param src * @param dest */ public static void wordToPdf(String src, String dest) { File file = new File(src); if (!file.exists()) { throw new RuntimeException("邮件不假定"); } FileInputStream fis = null; try { fis = new FileInputStream(file); Document wpd = new Document(fis); wpd.save(dest, SaveFormat.PDF); } catch (Exception e) { throw new RuntimeException(e); } finally { IoUtil.close(fis); } } /** * 滴印 * @param src 源邮件 * @param dest 最后目标邮件 * @param text 文字 * @param imagePath 照片位址 */ public static void addWatermark(String src, String dest, String text, String imagePath) { try { // 待滴印的邮件 PdfReader reader = new PdfReader(src); // 加完水印的邮件 PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); // 手写体 BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // 竞争性 PdfGState gs = new PdfGState(); gs.setFillOpacity(0.4f); // PDF邮件总页数 int total = reader.getNumberOfPages() + 1; // 循环对每一页都滴印 PdfContentByte content; for (int i = 1; i < total; i++) { // 水印在文本之上 content = stamper.getOverContent(i); content.setGState(gs); if (null != imagePath) { Image image = Image.getInstance(imagePath);// image.setAbsolutePosition(150, 150);// image.scaleToFit(300,300);// content.addImage(image); for (int x = 0; x < 700; x = x + 300) { for (int y = 0; y < 900; y = y + 200) { image.setAbsolutePosition(x+50, y+50); image.scaleToFit(100,100); content.addImage(image); } } } if (null != text) { content.beginText(); content.setColorFill(BaseColor.RED); content.setFontAndSize(baseFont, 20);// content.showTextAligned(Element.ALIGN_CENTER, text, 50, 50, 45); for (int x = 0; x < 700; x = x + 300) { for (int y = 0; y < 900; y = y + 200) { //水印内容和水印位置 content.showTextAligned(Element.ALIGN_CENTER, "哈哈哈哈哈", x - 20, y + 10, 30); content.showTextAligned(Element.ALIGN_CENTER, LocalDateTime.now().toString(), x, y, 30); } } content.endText(); } } stamper.close(); reader.close(); } catch (IOException e) { throw new RuntimeException(e); } catch (DocumentException e) { throw new RuntimeException(e); } }}赛跑一下
@Testvoid testWatermark() { String src2 = "D:\借条_2.pdf"; String dest2 = "D:\借条_3.pdf"; String imagePath = "D:\1.jpg"; PdfUtils.addWatermark(src2, dest2, "哈哈哈哈哈", imagePath);}加完水印后效果如图
最后,示例项目结构如图
中文翻译链接:_source=tuicoolWildutm_medium=referral
。惠州治疗早泄医院北京妇科专科医院哪家好
吃什么复合维生素好
西宁哪家白癜风医院比较好
常州男科检查哪些项目
-
上海9月16日新增本土无症状感染者1同上
院171同上,痊愈住院191同上,在院治疗4同上,被害0同上;泌尿道HIV749同上。原有待排查的疑似患者0同上。 截至2022年9同年16日24时,总共境内外读写开放性住院患者5285...
-
人工在酒店服务领域会被机器替代吗?比如客房
#酒吧##酒吧人# 随着科技的大幅度演进,日渐多的行业开始引入机器人和自动化系统设计,以提高实习量和降低成本。在酒吧免费应用领域,也有人担忧人工是否都会被机器替代,尤其是一些基层免...[详细]
-
美佳音控股(06939)披露2022年业绩 净利润4489.2万元 同比增长43.6% 末期息每股3.46分
美美梦成真控股06939公布2022年营业额,收入降低约3.3%至约1.73亿元为单位另加,净利润4489.2万元,同比增长43.6%;每股大体上盈利8.7分,末期股息每股3.46分。...[详细]
-
巴西新增新冠肺炎确诊发病10252例 累计确诊超3457万例
【秘鲁增添新冠肺结核确诊病症10252亦然 累计确诊超3457万亦然】财联社9年末17日电,秘鲁卫生部当地时间16日晚披露的数据显示,依然24小时,该国增添新冠肺结核确诊病症10252亦然,增添...[详细]
-
薄薄的飞机翅膀为何那么厚实?快来看看它的材料结构和优化理论
,铝较强较较低的值和弹性,符合于拒绝较较低有效载荷的机尾在结构上,而复合工艺较强较较低的比值和比弹性,符合于拒绝铝制的机尾在结构上。 在工艺自由选择时,必须根据具体的可控和其设计拒绝做显...[详细]
-
中国核能科技(00611)附属与浙江联盛订立融资租赁开放性协议
中国核能科技00611即日起,于2023年3月初30日,该公司间接全资附设公司核建融资租赁与台州联盛订立台州联盛融资租赁方两国政府。据此,核建融资租赁作为出租人表示同意于2023年3月初30日...[详细]