Java替换Word模板中的文字、图片、表格、水印

Java替换Word模板中的文字、图片、表格、水印

前言

由于业务需求,要求开发一个工具类,需要对Word模板中的内容进行替换,并且支持插入图片、根据业务生成自定义表格,以及对处理后的文件增加水印,处理完的文件支持保存doc、docx、pdf格式

1、定义模板预览

2、生成效果预览

保存为.docx格式 保存为.pdf格式

3、重点来了上代码

pom文件依赖

前提:需要引入包 Spire.Doc.jar 大家可以去Maven官网进行下载: https://mvnrepository.com/artifact/e-iceblue/spire.doc.free/5.2.0 如果出现jar包无法从仓库拉取,可以看下这篇帖子 https://blog.csdn.net/Dai_Haijiao/article/details/128670619

com.itextpdfitext-asian5.2.0e-icebluespire.doc.free5.2.0com.deepoovepoi-tl1.0.0com.itextpdfitextpdf5.4.3org.apache.poipoi-ooxml-schemas3.17 Java代码 public static void main(String[] args) {//文本数据Map dataMap =new HashMap();dataMap.put("fileNo", "88556");dataMap.put("name", "阿里巴巴国际站");dataMap.put("socialCreditCode", "91330100799655058B");dataMap.put("regAddress", "杭州市余杭区文一西路969号");dataMap.put("frName", "张勇");dataMap.put("regCapital", "12000万");String tempPath = "officefile/gernerTemplate.docx";//模板地址(当前地址为项目根节点,如果用本地地址可直接复制文件所在位置全路径即可)String newTextFilePath = "officefile/gernerNewFile1.pdf";//生成文件地址//图片数据Map pictureMap=new HashMap();pictureMap.put("localPicture", "http://file.static.fijo.com.cn/ZSZX/ZSJC/ECZC0004/png/5be7cfa8-4e8c-479d-9524-4ef01d9513c8.png");pictureMap.put("urlPicture", "http://file.static.fijo.com.cn/SFTM/SFTM-XZFW/STFA0003/jpg/9fd2e553-b87b-4ec0-acd2-1adf2ce6c504.jpg");String[] header = { "学号", "姓名", "性别", "班级", "成绩" };//表头String watermarkText = "管理员";//水印名称String positionParam = "table";//表格所在位置标签//列表数据String[][] data ={new String[]{"0105", "赵晓晓", "男", "1", "88"},new String[]{"0106", "李雯雯", "女", "7", "92"},new String[]{"0107", "陈聪聪", "女", "11", "91"},new String[]{"0108", "王明明", "男", "4", "95"},new String[]{"0109", "韩梅梅", "女", "5", "94"},};try {//执行前先删除文件,以防止占用或覆盖情况File dirFile=new File(newTextFilePath);dirFile.delete();//添加表格addFileTable(tempPath,newTextFilePath,positionParam,header,data);//替换内容replaceFileDate(newTextFilePath,newTextFilePath,watermarkText,dataMap,pictureMap);} catch (Exception e) {e.printStackTrace();}} 创建表格方法 /*** 创建表格* @param fileCatalogue 文件所在目录* @param newFilePath 生成新文件目录* @param positionParam 表格所在位置* @param header 列表表头* @param tableArr 数据集*/public static void addFileTable(String fileCatalogue ,String newFilePath,String positionParam, String[] header, String[][] tableArr){//创建一个Document对象Document document = new Document(fileCatalogue);//添加一个节点Section section = document.getSections().get(0);//获取表格所在节点TextSelection textSelection = document.findString(positionParam, true, true);//获取关键字符串所在段落的索引TextRange range2 = textSelection.getAsOneRange();Paragraph paragraph = range2.getOwnerParagraph();Body body = paragraph.ownerTextBody();int index = body.getChildObjects().indexOf(paragraph);//添加表格Table table = section.addTable(true);//生成行(数据数组长度+表头) 列(表头长度)table.resetCells(tableArr.length + 1, header.length);//将第一行设置为表格标题TableRow row = table.getRows().get(0);row.isHeader(true);row.setHeight(20);row.setHeightType(TableRowHeightType.Exactly);row.getRowFormat().setBackColor(Color.gray);for (int i = 0; i TableRow dataRow = table.getRows().get(r + 1);dataRow.setHeight(25);dataRow.setHeightType(TableRowHeightType.Exactly);dataRow.getRowFormat().setBackColor(Color.white);for (int c = 0; c if (j % 2 == 0){TableRow row2 = table.getRows().get(j);for (int f = 0; f //插入图片if (null != pictureMap){addFileRender(filePath,newFilePath,pictureMap);}//替换文件内容replaceText(newFilePath,newFilePath,dataMap,text);} /*** 在文件对应位置插入图片* @param fileCatalogue 文件地址* @param newFilePath 生成新文件地址* @param pictureMap 图片数据* @throws Exception*/public static void addFileRender(String fileCatalogue ,String newFilePath, Map pictureMap) throws Exception {//构建内容Map datas = new HashMap() {{//遍历图片数据pictureMap.forEach((key,value) -> {String filePath = String.valueOf(value);String suffix = filePath.substring(filePath.lastIndexOf(".") );//本地图片// put("localPicture", new PictureRenderData(100, 120, "officefile/tom.png"));//构建网络图片 设置宽高put(String.valueOf(key), new PictureRenderData(100, 100, suffix, BytePictureUtils.getUrlByteArray(String.valueOf(value))));});}};//插入图片XWPFTemplate template = XWPFTemplate.compile(fileCatalogue).render(datas);FileOutputStream out = new FileOutputStream(newFilePath);template.write(out);out.flush();out.close();template.close();} 替换文档中的文本以及添加水印 /*** 替换文档中的制定文字** @param filePath 文件存放全路径* @param newFilePath 产生的新文件存放地址* @param dataMap 要替换的内容(key为要替换的内容,value为要替换成的内容)* @param text 水印内容* @return* @throws FileNotFoundException*/public static void replaceText(String filePath,String newFilePath, Map dataMap,String text) throws FileNotFoundException {Document doc = new Document(filePath);//替换内容for (Map.Entry entry : dataMap.entrySet()) {String k = entry.getKey();String v = entry.getValue();doc.replace(k, v, true, false);}//添加水印 艺术字并设置大小ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);shape.setWidth(60);shape.setHeight(20);//设置艺术字文本内容、位置及样式shape.setVerticalPosition(30);shape.setHorizontalPosition(20);shape.setRotation(315);shape.getWordArt().setFontFamily("黑体");shape.getWordArt().setText(text);shape.setFillColor(new Color(128,128,128));shape.setLineStyle(ShapeLineStyle.Single);shape.setStrokeColor(new Color(192, 192, 192, 255));shape.setStrokeWeight(1);Section section;HeaderFooter header;for (int n = 0; n //如果页眉有段落,取它第一个段落paragraph = header.getParagraphs().get(0);} else {//否则新增加一个段落到页眉paragraph = header.addParagraph();}for (int i = 0; i //复制艺术字并设置多行多列位置shape = (ShapeObject) shape.deepClone();shape.setVerticalPosition(50 + 150 * i);shape.setHorizontalPosition(20 + 160 * j);paragraph.getChildObjects().add(shape);}}}//删除文件File dirFile=new File(newFilePath);dirFile.delete();//根据生成文件后缀,生成docx或者pdfString suffix = newFilePath.substring(newFilePath.lastIndexOf(".") +1);if (suffix.equals("doc")||suffix.equals("docx")){doc.saveToFile(newFilePath, FileFormat.Docx);}else if (suffix.equals("pdf")){doc.saveToFile(newFilePath, FileFormat.PDF);}}

总结 以上主要完成了对java替换模板内容,以及增加表格、水印、保存其他格式做了方法的封装,并且已在项目中作为公共方法使用; 如果以上内容对小伙伴有帮助,请关注支持,如有疑问可私信我,欢迎指教! 创作不易如有打赏,感激不尽


比丘资源网 » Java替换Word模板中的文字、图片、表格、水印

发表回复

提供最优质的资源集合

立即查看 了解详情