前言:在日常的工作当中,避免不了会涉及到一些Word文件方面的操作,这篇博客将使用SpringBoot整合开源Apache来操作Word,分享的都是目前实际当中会经常用到的一些功能代码都实际测试过,只分享干货,大家一键复制使用就可以了。
目录
一、引入pom依赖
二、工具类
2.1、POIUtil操作Word工具类
2.2、SealUtil工具类
2.3、SealCircle实体类
2.4、SealConfiguration配置类
2.5、SealFont字体类
三、使用方法
3.1、替换Word当中的通配符
3.2、替换Word当中的书签
3.3、Word转Pdf
3.4、通过Word书签在段落中插入图片
3.5、生成公章
3.6、Word盖章
四、Gitee源码
一、引入pom依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>
二、工具类
这边需要提前准备一些工具类和配置文件,源码不做讲解,知道如何使用工具即可。
2.1、POIUtil操作Word工具类
package com.ithuang.office.utils;
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.w3c.dom.Node;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class POIUtil {
/**
* 用一个docx文档作为模板,然后替换其中的内容,再写入目标文档中。
* @throws Exception
*/
public static void templateWrite(String filePath,String outFilePath,Map<String, Object> params) throws Exception {
InputStream is = new FileInputStream(filePath);
XWPFDocument doc = new XWPFDocument(is);
//替换段落里面的变量
replaceInPara(doc, params);
//替换表格里面的变量
replaceInTable(doc, params);
OutputStream os = new FileOutputStream(outFilePath);
doc.write(os);
close(os);
close(is);
}
/**
* 替换段落里面的变量
* @param doc 要替换的文档
* @param params 参数
*/
public static void replaceInPara(XWPFDocument doc, Map<String, Object> params) {
Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
XWPFParagraph para;
while (iterator.hasNext()) {
para = iterator.next();
replaceInPara(para, params);
}
}
/**
* 替换段落里面的变量
*
* @param para 要替换的段落
* @param params 参数
*/
public static void replaceInPara(XWPFParagraph para, Map<String, Object> params) {
List<XWPFRun> runs;
Matcher matcher;
String runText = "";
int fontSize = 0;
UnderlinePatterns underlinePatterns = null;
if (matcher(para.getParagraphText()).find()) {
runs = para.getRuns();
if (runs.size() > 0) {
int j = runs.size();
for (int i = 0; i < j; i++) {
XWPFRun run = runs.get(0);
if (fontSize == 0) {
fontSize = run.getFontSize();
}
if(underlinePatterns==null){
underlinePatterns=run.getUnderline();
}
String i1 = run.toString();
runText += i1;
para.removeRun(0);
}
}
matcher = matcher(runText);
if (matcher.find()) {
while ((matcher = matcher(runText)).find()) {
runText = matcher.replaceFirst(String.valueOf(params.get(matcher.group(1))));
}
//直接调用XWPFRun的setText()方法设置文本时,在底层会重新创建一个XWPFRun,把文本附加在当前文本后面,
//所以我们不能直接设值,需要先删除当前run,然后再自己手动插入一个新的run。
//para.insertNewRun(0).setText(runText);//新增的没有样式
XWPFRun run = para.createRun();
run.setText(runText,0);
run.setFontSize(fontSize);
run.setUnderline(underlinePatterns);
run.setFontFamily("宋体");//字体
run.setFontSize(10);//字体大小
//run.setBold(true); //加粗
//run.setColor("FF0000");
//默认:宋体(wps)/等线(office2016) 5号 两端对齐 单倍间距
//run.setBold(false);//加粗
//run.setCapitalized(false);//我也不知道这个属性做啥的
//run.setCharacterSpacing(5);//这个属性报错
//run.setColor("BED4F1");//设置颜色--十六进制
//run.setDoubleStrikethrough(false);//双删除线
//run.setEmbossed(false);//浮雕字体----效果和印记(悬浮阴影)类似
//run.setFontFamily("宋体");//字体
//run.setFontFamily("华文新魏", FontCharRange.cs);//字体,范围----效果不详
//run.setFontSize(14);//字体大小
//run.setImprinted(false);//印迹(悬浮阴影)---效果和浮雕类似
//run.setItalic(false);//斜体(字体倾斜)
//run.setKerning(1);//字距调整----这个好像没有效果
//run.setShadow(true);//阴影---稍微有点效果(阴影不明显)
//run.setSmallCaps(true);//小型股------效果不清楚
//run.setStrike(true);//单删除线(废弃)
//run.setStrikeThrough(false);//单删除线(新的替换Strike)
//run.setSubscript(VerticalAlign.SUBSCRIPT);//下标(吧当前这个run变成下标)---枚举
//run.setTextPosition(20);//设置两行之间的行间距
//run.setUnderline(UnderlinePatterns.DASH_LONG);//各种类型的下划线(枚举)
//run0.addBreak();//类似换行的操作(html的 br标签)
//run0.addTab();//tab键
//run0.addCarriageReturn();//回车键
//注意:addTab()和addCarriageReturn() 对setText()的使用先后顺序有关:比如先执行addTab,再写Text这是对当前这个Text的Table,反之是对下一个run的Text的Tab效果
}
}
}
/**
* 替换表格里面的变量
* @param doc 要替换的文档
* @param params 参数
*/
public static void replaceInTable(XWPFDocument doc, Map<String, Object> params) {
Iterator<XWPFTable> iterator = doc.getTablesIterator();
XWPFTable table;
List<XWPFTableRow> rows;
List<XWPFTableCell> cells;
List<XWPFParagraph> paras;
while (iterator.hasNext()) {
table = iterator.next();
rows = table.getRows();
for (XWPFTableRow row : rows) {
cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
paras = cell.getParagraphs();
for (XWPFParagraph para : paras) {
replaceInPara(para, params);
}
}
}
}
}
/**
* 正则匹配字符串
* @param str
* @return
*/
public static Matcher matcher(String str) {
Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
return matcher;
}
/**
* 关闭输入流
* @param is
*/
public static void close(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 关闭输出流
* @param os
*/
public static void close(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* word转换成pdf
* @param fileName
* @param pdfName
* @throws IOException
*/
public static void wordToPdf(String fileName,String pdfName) throws IOException {
InputStream in = Files.newInputStream(Paths.get(fileName));
XWPFDocument document = new XWPFDocument(in);
PdfOptions options = PdfOptions.create();
OutputStream out = Files.newOutputStream(Paths.get(pdfName));
PdfConverter.getInstance().convert(document,out,options);
}
/**
* 替换书签
*
* @param document
* @param bookTagMap 书签map
*/
public static void replaceBookTag(XWPFDocument document, Map<String, Object> bookTagMap) {
List<XWPFParagraph> paragraphList = document.getParagraphs();
for (XWPFParagraph xwpfParagraph : paragraphList) {
CTP ctp = xwpfParagraph.getCTP();
for (int dwI = 0; dwI < ctp.sizeOfBookmarkStartArray(); dwI++) {
CTBookmark bookmark = ctp.getBookmarkStartArray(dwI);
if (bookTagMap.containsKey(bookmark.getName())) {
XWPFRun run = xwpfParagraph.createRun();
run.setText(bookTagMap.get(bookmark.getName()).toString());
Node firstNode = bookmark.getDomNode();
Node nextNode = firstNode.getNextSibling();
while (nextNode != null) {
// 循环查找结束符
String nodeName = nextNode.getNodeName();
if (nodeName.equals("w:bookmarkEnd")) {
break;
}
// 删除中间的非结束节点,即删除原书签内容
Node delNode = nextNode;
nextNode = nextNode.getNextSibling();
ctp.getDomNode().removeChild(delNode);
}
if (nextNode == null) {
// 始终找不到结束标识的,就在书签前面添加
ctp.getDomNode().insertBefore(run.getCTR().getDomNode(), firstNode);
} else {
// 找到结束符,将新内容添加到结束符之前,即内容写入bookmark中间
ctp.getDomNode().insertBefore(run.getCTR().getDomNode(), nextNode);
}
}
}
}
}
/**
* <b> Word中添加图章
* </b><br><br><i>Description</i> :
* String srcPath, 源Word路径
* String storePath, 添加图章后的路径
* String sealPath, 图章路径(即图片)
* tString abText, 在Word中盖图章的标识字符串,如:(签字/盖章)
* int width, 图章宽度
* int height, 图章高度
* int leftOffset, 图章在编辑段落向左偏移量
* int topOffset, 图章在编辑段落向上偏移量
* boolean behind,图章是否在文字下面
* @return void
* <br><br>Date: 2019/12/26 15:12
*/
public static void sealInWord(String srcPath, String storePath,String sealPath,String tabText,
int width, int height, int leftOffset, int topOffset, boolean behind) throws Exception {
File fileTem = new File(srcPath);
InputStream is = new FileInputStream(fileTem);
XWPFDocument document = new XWPFDocument(is);
List<XWPFParagraph> paragraphs = document.getParagraphs();
XWPFRun targetRun = null;
for(XWPFParagraph paragraph : paragraphs){
if(!"".equals(paragraph.getText()) && paragraph.getText().contains(tabText)){
List<XWPFRun> runs = paragraph.getRuns();
targetRun = runs.get(runs.size()-1);
}
}
if(targetRun != null){
InputStream in = new FileInputStream(sealPath);//设置图片路径
if(width <= 0){
width = 100;
}
if(height <= 0){
height = 100;
}
//创建Random类对象
Random random = new Random();
//产生随机数
int number = random.nextInt(999) + 1;
targetRun.addPicture(in, Document.PICTURE_TYPE_PNG, "Seal"+number, Units.toEMU(width), Units.toEMU(height));
in.close();
// 2. 获取到图片数据
CTDrawing drawing = targetRun.getCTR().getDrawingArray(0);
CTGraphicalObject graphicalobject = drawing.getInlineArray(0).getGraphic();
//拿到新插入的图片替换添加CTAnchor 设置浮动属性 删除inline属性
CTAnchor anchor = getAnchorWithGraphic(graphicalobject, "Seal"+number,
Units.toEMU(width), Units.toEMU(height),//图片大小
Units.toEMU(leftOffset), Units.toEMU(topOffset), behind);//相对当前段落位置 需要计算段落已有内容的左偏移
drawing.setAnchorArray(new CTAnchor[]{anchor});//添加浮动属性
drawing.removeInline(0);//删除行内属性
}
FileOutputStream outputStream = new FileOutputStream(storePath);
document.write(outputStream);
document.close();
outputStream.close();
is.close();
}
/**
* @param ctGraphicalObject 图片数据
* @param deskFileName 图片描述
* @param width 宽
* @param height 高
* @param leftOffset 水平偏移 left
* @param topOffset 垂直偏移 top
* @param behind 文字上方,文字下方
* @return
* @throws Exception
*/
public static CTAnchor getAnchorWithGraphic(CTGraphicalObject ctGraphicalObject,
String deskFileName, int width, int height,
int leftOffset, int topOffset, boolean behind) {
String anchorXML =
"<wp:anchor xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" "
+ "simplePos=\"0\" relativeHeight=\"0\" behindDoc=\"" + ((behind) ? 1 : 0) + "\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\">"
+ "<wp:simplePos x=\"0\" y=\"0\"/>"
+ "<wp:positionH relativeFrom=\"column\">"
+ "<wp:posOffset>" + leftOffset + "</wp:posOffset>"
+ "</wp:positionH>"
+ "<wp:positionV relativeFrom=\"paragraph\">"
+ "<wp:posOffset>" + topOffset + "</wp:posOffset>" +
"</wp:positionV>"
+ "<wp:extent cx=\"" + width + "\" cy=\"" + height + "\"/>"
+ "<wp:effectExtent l=\"0\" t=\"0\" r=\"0\" b=\"0\"/>"
+ "<wp:wrapNone/>"
+ "<wp:docPr id=\"1\" name=\"Drawing 0\" descr=\"" + deskFileName + "\"/><wp:cNvGraphicFramePr/>"
+ "</wp:anchor>";
CTDrawing drawing = null;
try {
drawing = CTDrawing.Factory.parse(anchorXML);
} catch (XmlException e) {
e.printStackTrace();
}
CTAnchor anchor = drawing.getAnchorArray(0);
anchor.setGraphic(ctGraphicalObject);
return anchor;
}
/**
* 通过书签替换段落中的图片
* @param doc
* @param dataMap
* @throws IOException
* @throws InvalidFormatException
*/
public static void replacePicByTag(XWPFDocument doc, Map<String, InputStream> dataMap) throws IOException, InvalidFormatException {
List<XWPFParagraph> paragraphs = doc.getParagraphs();
for (XWPFParagraph xwpfParagraph : paragraphs) {
CTP ctp = xwpfParagraph.getCTP();
for (int dwI = 0; dwI < ctp.sizeOfBookmarkStartArray(); dwI++) {
CTBookmark bookmark = ctp.getBookmarkStartArray(dwI);
InputStream picIs = dataMap.get(bookmark.getName());
if(picIs != null){
XWPFRun run = xwpfParagraph.createRun();
//bus.png为鼠标在word里选择图片时,图片显示的名字,400,400则为像素单元,根据实际需要的大小进行调整即可。
run.addPicture(picIs,XWPFDocument.PICTURE_TYPE_PNG,"bus.png,", Units.toEMU(400), Units.toEMU(400));
}
}
}
}
}
2.2、SealUtil工具类
package com.ithuang.office.utils;
import com.ithuang.office.utils.configuration.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
public abstract class SealUtil {
/**
* 默认从10x10的位置开始画,防止左上部分画布装不下
*/
private final static int INIT_BEGIN = 10;
/**
* 生成私人印章图片,并保存到指定路径
*
* @param lineSize 边线宽度
* @param font 字体对象
* @param addString 追加字符
* @param fullPath 保存全路径
*
* @throws Exception 异常
*/
public static void buildAndStorePersonSeal(int imageSize, int lineSize, SealFont font, String addString,
String fullPath) throws Exception {
storeBytes(buildBytes(buildPersonSeal(imageSize, lineSize, font, addString)), fullPath);
}
/**
* 生成印章图片,并保存到指定路径
*
* @param conf 配置文件F
* @param fullPath 保存全路径
*
* @throws Exception 异常
*/
public static void buildAndStoreSeal(SealConfiguration conf, String fullPath) throws Exception {
storeBytes(buildBytes(buildSeal(conf)), fullPath);
}
/**
* 生成印章图片的byte数组
*
* @param image BufferedImage对象
*
* @return byte数组
*
* @throws IOException 异常
*/
public static byte[] buildBytes(BufferedImage image) throws Exception {
try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
//bufferedImage转为byte数组
ImageIO.write(image, "png", outStream);
return outStream.toByteArray();
}
}
/**
* 生成印章图片
*
* @param conf 配置文件
*
* @return BufferedImage对象
*
* @throws Exception 异常
*/
public static BufferedImage buildSeal(SealConfiguration conf) throws Exception {
//1.画布
BufferedImage bi = new BufferedImage(conf.getImageSize(), conf.getImageSize(), BufferedImage.TYPE_4BYTE_ABGR);
//2.画笔
Graphics2D g2d = bi.createGraphics();
//2.1抗锯齿设置
//文本不抗锯齿,否则圆中心的文字会被拉长
RenderingHints hints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
//其他图形抗锯齿
hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHints(hints);
//2.2设置背景透明度
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 0));
//2.3填充矩形
g2d.fillRect(0, 0, conf.getImageSize(), conf.getImageSize());
//2.4重设透明度,开始画图
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1));
//2.5设置画笔颜色
g2d.setPaint(conf.getBackgroudColor());
//3.画边线圆
if (conf.getBorderCircle() != null) {
drawCicle(g2d, conf.getBorderCircle(), INIT_BEGIN, INIT_BEGIN);
} else {
throw new Exception("BorderCircle can not null!");
}
int borderCircleWidth = conf.getBorderCircle().getWidth();
int borderCircleHeight = conf.getBorderCircle().getHeight();
//4.画内边线圆
if (conf.getBorderInnerCircle() != null) {
int x = INIT_BEGIN + borderCircleWidth - conf.getBorderInnerCircle().getWidth();
int y = INIT_BEGIN + borderCircleHeight - conf.getBorderInnerCircle().getHeight();
drawCicle(g2d, conf.getBorderInnerCircle(), x, y);
}
//5.画内环线圆
if (conf.getInnerCircle() != null) {
int x = INIT_BEGIN + borderCircleWidth - conf.getInnerCircle().getWidth();
int y = INIT_BEGIN + borderCircleHeight - conf.getInnerCircle().getHeight();
drawCicle(g2d, conf.getInnerCircle(), x, y);
}
//6.画弧形主文字
if (borderCircleHeight != borderCircleWidth) {
drawArcFont4Oval(g2d, conf.getBorderCircle(), conf.getMainFont(), true);
} else {
drawArcFont4Circle(g2d, borderCircleHeight, conf.getMainFont(), true);
}
//7.画弧形副文字
if (borderCircleHeight != borderCircleWidth) {
drawArcFont4Oval(g2d, conf.getBorderCircle(), conf.getViceFont(), false);
} else {
drawArcFont4Circle(g2d, borderCircleHeight, conf.getViceFont(), false);
}
//8.画中心字
drawFont(g2d, (borderCircleWidth + INIT_BEGIN) * 2, (borderCircleHeight + INIT_BEGIN) * 2,
conf.getCenterFont());
//9.画抬头文字
drawFont(g2d, (borderCircleWidth + INIT_BEGIN) * 2, (borderCircleHeight + INIT_BEGIN) * 2, conf.getTitleFont());
g2d.dispose();
return bi;
}
/**
* 生成私人印章图片
*
* @param lineSize 线条粗细
* @param font 字体对象
* @param addString 是否添加文字,如“印”
*
* @return BufferedImage对象
*
* @throws Exception 异常
*/
public static BufferedImage buildPersonSeal(int imageSize, int lineSize, SealFont font, String addString)
throws Exception {
if (font == null || font.getFontText().length() < 2 || font.getFontText().length() > 4) {
throw new Exception("FontText.length illegal!");
}
int fixH = 18;
int fixW = 2;
//1.画布
BufferedImage bi = new BufferedImage(imageSize, imageSize / 2, BufferedImage.TYPE_4BYTE_ABGR);
//2.画笔
Graphics2D g2d = bi.createGraphics();
//2.1设置画笔颜色
g2d.setPaint(Color.RED);
//2.2抗锯齿设置
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//3.写签名
int marginW = fixW + lineSize;
float marginH;
FontRenderContext context = g2d.getFontRenderContext();
Rectangle2D rectangle;
Font f;
if (font.getFontText().length() == 2) {
if (addString != null && addString.trim().length() > 0) {
bi = drawThreeFont(bi, g2d, font.setFontText(font.getFontText() + addString), lineSize, imageSize, fixH,
fixW, true);
} else {
f = new Font(font.getFontFamily(), Font.BOLD, font.getFontSize());
g2d.setFont(f);
rectangle = f.getStringBounds(font.getFontText().substring(0, 1), context);
marginH = (float) (Math.abs(rectangle.getCenterY()) * 2 + marginW) + fixH - 4;
g2d.drawString(font.getFontText().substring(0, 1), marginW, marginH);
marginW += Math.abs(rectangle.getCenterX()) * 2 + (font.getFontSpace() == null ?
INIT_BEGIN :
font.getFontSpace());
g2d.drawString(font.getFontText().substring(1), marginW, marginH);
//拉伸
BufferedImage nbi = new BufferedImage(imageSize, imageSize, bi.getType());
Graphics2D ng2d = nbi.createGraphics();
ng2d.setPaint(Color.RED);
ng2d.drawImage(bi, 0, 0, imageSize, imageSize, null);
//画正方形
ng2d.setStroke(new BasicStroke(lineSize));
ng2d.drawRect(0, 0, imageSize, imageSize);
ng2d.dispose();
bi = nbi;
}
} else if (font.getFontText().length() == 3) {
if (addString != null && addString.trim().length() > 0) {
bi = drawFourFont(bi, font.setFontText(font.getFontText() + addString), lineSize, imageSize, fixH,
fixW);
} else {
bi = drawThreeFont(bi, g2d, font.setFontText(font.getFontText()), lineSize, imageSize, fixH, fixW,
false);
}
} else {
bi = drawFourFont(bi, font, lineSize, imageSize, fixH, fixW);
}
return bi;
}
/**
* 将byte数组保存为本地文件
*
* @param buf byte数组
* @param fullPath 文件全路径
*
* @throws IOException 异常
*/
private static void storeBytes(byte[] buf, String fullPath) throws IOException {
File file = new File(fullPath);
try (FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos)) {
//1.如果父目录不存在,则创建
File dir = file.getParentFile();
if (!dir.exists()) {
dir.mkdirs();
}
//2.写byte数组到文件
bos.write(buf);
}
}
/**
* 画三字
*
* @param bi 图片
* @param g2d 原画笔
* @param font 字体对象
* @param lineSize 线宽
* @param imageSize 图片尺寸
* @param fixH 修复膏
* @param fixW 修复宽
* @param isWithYin 是否含有“印”
*/
private static BufferedImage drawThreeFont(BufferedImage bi, Graphics2D g2d, SealFont font, int lineSize,
int imageSize, int fixH, int fixW, boolean isWithYin) {
fixH -= 9;
int marginW = fixW + lineSize;
//设置字体
Font f = new Font(font.getFontFamily(), Font.BOLD, font.getFontSize());
g2d.setFont(f);
FontRenderContext context = g2d.getFontRenderContext();
Rectangle2D rectangle = f.getStringBounds(font.getFontText().substring(0, 1), context);
float marginH = (float) (Math.abs(rectangle.getCenterY()) * 2 + marginW) + fixH;
int oldW = marginW;
if (isWithYin) {
g2d.drawString(font.getFontText().substring(2, 3), marginW, marginH);
marginW += rectangle.getCenterX() * 2 + (font.getFontSpace() == null ? INIT_BEGIN : font.getFontSpace());
} else {
marginW += rectangle.getCenterX() * 2 + (font.getFontSpace() == null ? INIT_BEGIN : font.getFontSpace());
g2d.drawString(font.getFontText().substring(0, 1), marginW, marginH);
}
//拉伸
BufferedImage nbi = new BufferedImage(imageSize, imageSize, bi.getType());
Graphics2D ng2d = nbi.createGraphics();
ng2d.setPaint(Color.RED);
ng2d.drawImage(bi, 0, 0, imageSize, imageSize, null);
//画正方形
ng2d.setStroke(new BasicStroke(lineSize));
ng2d.drawRect(0, 0, imageSize, imageSize);
ng2d.dispose();
bi = nbi;
g2d = bi.createGraphics();
g2d.setPaint(Color.RED);
g2d.setFont(f);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (isWithYin) {
g2d.drawString(font.getFontText().substring(0, 1), marginW, marginH += fixH);
rectangle = f.getStringBounds(font.getFontText(), context);
marginH += Math.abs(rectangle.getHeight());
g2d.drawString(font.getFontText().substring(1), marginW, marginH);
} else {
g2d.drawString(font.getFontText().substring(1, 2), oldW, marginH += fixH);
rectangle = f.getStringBounds(font.getFontText(), context);
marginH += Math.abs(rectangle.getHeight());
g2d.drawString(font.getFontText().substring(2, 3), oldW, marginH);
}
return bi;
}
/**
* 画四字
*
* @param bi 图片
* @param font 字体对象
* @param lineSize 线宽
* @param imageSize 图片尺寸
* @param fixH 修复膏
* @param fixW 修复宽
*/
private static BufferedImage drawFourFont(BufferedImage bi, SealFont font, int lineSize, int imageSize, int fixH,
int fixW) {
int marginW = fixW + lineSize;
//拉伸
BufferedImage nbi = new BufferedImage(imageSize, imageSize, bi.getType());
Graphics2D ng2d = nbi.createGraphics();
ng2d.setPaint(Color.RED);
ng2d.drawImage(bi, 0, 0, imageSize, imageSize, null);
//画正方形
ng2d.setStroke(new BasicStroke(lineSize));
ng2d.drawRect(0, 0, imageSize, imageSize);
ng2d.dispose();
bi = nbi;
Graphics2D g2d = bi.createGraphics();
g2d.setPaint(Color.RED);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
FontRenderContext context = g2d.getFontRenderContext();
Font f = new Font(font.getFontFamily(), Font.BOLD, font.getFontSize());
g2d.setFont(f);
Rectangle2D rectangle = f.getStringBounds(font.getFontText().substring(0, 1), context);
float marginH = (float) (Math.abs(rectangle.getCenterY()) * 2 + marginW) + fixH;
g2d.drawString(font.getFontText().substring(2, 3), marginW, marginH);
int oldW = marginW;
marginW +=
Math.abs(rectangle.getCenterX()) * 2 + (font.getFontSpace() == null ? INIT_BEGIN : font.getFontSpace());
g2d.drawString(font.getFontText().substring(0, 1), marginW, marginH);
marginH += Math.abs(rectangle.getHeight());
g2d.drawString(font.getFontText().substring(3, 4), oldW, marginH);
g2d.drawString(font.getFontText().substring(1, 2), marginW, marginH);
return bi;
}
/**
* 绘制圆弧形文字
*
* @param g2d 画笔
* @param circleRadius 弧形半径
* @param font 字体对象
* @param isTop 是否字体在上部,否则在下部
*/
private static void drawArcFont4Circle(Graphics2D g2d, int circleRadius, SealFont font, boolean isTop) {
if (font == null) {
return;
}
//1.字体长度
int fontTextLen = font.getFontText().length();
//2.字体大小,默认根据字体长度动态设定 TODO
int fontSize = font.getFontSize() == null ? (55 - fontTextLen * 2) : font.getFontSize();
//3.字体样式
int fontStyle = font.isBold() ? Font.BOLD : Font.PLAIN;
//4.构造字体
Font f = new Font(font.getFontFamily(), fontStyle, fontSize);
FontRenderContext context = g2d.getFontRenderContext();
Rectangle2D rectangle = f.getStringBounds(font.getFontText(), context);
//5.文字之间间距,默认动态调整
double fontSpace;
if (font.getFontSpace() != null) {
fontSpace = font.getFontSpace();
} else {
if (fontTextLen == 1) {
fontSpace = 0;
} else {
fontSpace = rectangle.getWidth() / (fontTextLen - 1) * 0.9;
}
}
//6.距离外圈距离
int marginSize = font.getMarginSize() == null ? INIT_BEGIN : font.getMarginSize();
//7.写字
double newRadius = circleRadius + rectangle.getY() - marginSize;
double radianPerInterval = 2 * Math.asin(fontSpace / (2 * newRadius));
double fix = 0.04;
if (isTop) {
fix = 0.18;
}
double firstAngle;
if (!isTop) {
if (fontTextLen % 2 == 1) {
firstAngle = Math.PI + Math.PI / 2 - (fontTextLen - 1) * radianPerInterval / 2.0 - fix;
} else {
firstAngle = Math.PI + Math.PI / 2 - ((fontTextLen / 2.0 - 0.5) * radianPerInterval) - fix;
}
} else {
if (fontTextLen % 2 == 1) {
firstAngle = (fontTextLen - 1) * radianPerInterval / 2.0 + Math.PI / 2 + fix;
} else {
firstAngle = (fontTextLen / 2.0 - 0.5) * radianPerInterval + Math.PI / 2 + fix;
}
}
for (int i = 0; i < fontTextLen; i++) {
double theta;
double thetaX;
double thetaY;
if (!isTop) {
theta = firstAngle + i * radianPerInterval;
thetaX = newRadius * Math.sin(Math.PI / 2 - theta);
thetaY = newRadius * Math.cos(theta - Math.PI / 2);
} else {
theta = firstAngle - i * radianPerInterval;
thetaX = newRadius * Math.sin(Math.PI / 2 - theta);
thetaY = newRadius * Math.cos(theta - Math.PI / 2);
}
AffineTransform transform;
if (!isTop) {
transform = AffineTransform.getRotateInstance(Math.PI + Math.PI / 2 - theta);
} else {
transform = AffineTransform.getRotateInstance(Math.PI / 2 - theta + Math.toRadians(8));
}
Font f2 = f.deriveFont(transform);
g2d.setFont(f2);
g2d.drawString(font.getFontText().substring(i, i + 1), (float) (circleRadius + thetaX + INIT_BEGIN),
(float) (circleRadius - thetaY + INIT_BEGIN));
}
}
/**
* 绘制椭圆弧形文字
*
* @param g2d 画笔
* @param circle 外围圆
* @param font 字体对象
* @param isTop 是否字体在上部,否则在下部
*/
private static void drawArcFont4Oval(Graphics2D g2d, SealCircle circle, SealFont font, boolean isTop) {
if (font == null) {
return;
}
float radiusX = circle.getWidth();
float radiusY = circle.getHeight();
float radiusWidth = radiusX + circle.getLineSize();
float radiusHeight = radiusY + circle.getLineSize();
//1.字体长度
int fontTextLen = font.getFontText().length();
//2.字体大小,默认根据字体长度动态设定
int fontSize = font.getFontSize() == null ? 25 + (10 - fontTextLen) / 2 : font.getFontSize();
//3.字体样式
int fontStyle = font.isBold() ? Font.BOLD : Font.PLAIN;
//4.构造字体
Font f = new Font(font.getFontFamily(), fontStyle, fontSize);
//5.总的角跨度
float totalArcAng = (float) (font.getFontSpace() * fontTextLen);
//6.从边线向中心的移动因子
float minRat = 0.90f;
double startAngle = isTop ? -90f - totalArcAng / 2f : 90f - totalArcAng / 2f;
double step = 0.5;
int alCount = (int) Math.ceil(totalArcAng / step) + 1;
double[] angleArr = new double[alCount];
double[] arcLenArr = new double[alCount];
int num = 0;
double accArcLen = 0.0;
angleArr[num] = startAngle;
arcLenArr[num] = accArcLen;
num++;
double angR = startAngle * Math.PI / 180.0;
double lastX = radiusX * Math.cos(angR) + radiusWidth;
double lastY = radiusY * Math.sin(angR) + radiusHeight;
for (double i = startAngle + step; num < alCount; i += step) {
angR = i * Math.PI / 180.0;
double x = radiusX * Math.cos(angR) + radiusWidth, y = radiusY * Math.sin(angR) + radiusHeight;
accArcLen += Math.sqrt((lastX - x) * (lastX - x) + (lastY - y) * (lastY - y));
angleArr[num] = i;
arcLenArr[num] = accArcLen;
lastX = x;
lastY = y;
num++;
}
double arcPer = accArcLen / fontTextLen;
for (int i = 0; i < fontTextLen; i++) {
double arcL = i * arcPer + arcPer / 2.0;
double ang = 0.0;
for (int p = 0; p < arcLenArr.length - 1; p++) {
if (arcLenArr[p] <= arcL && arcL <= arcLenArr[p + 1]) {
ang = (arcL >= ((arcLenArr[p] + arcLenArr[p + 1]) / 2.0)) ? angleArr[p + 1] : angleArr[p];
break;
}
}
angR = (ang * Math.PI / 180f);
Float x = radiusX * (float) Math.cos(angR) + radiusWidth;
Float y = radiusY * (float) Math.sin(angR) + radiusHeight;
double qxang = Math.atan2(radiusY * Math.cos(angR), -radiusX * Math.sin(angR));
double fxang = qxang + Math.PI / 2.0;
int subIndex = isTop ? i : fontTextLen - 1 - i;
String c = font.getFontText().substring(subIndex, subIndex + 1);
//获取文字高宽
FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(f);
int w = fm.stringWidth(c), h = fm.getHeight();
if (isTop) {
x += h * minRat * (float) Math.cos(fxang);
y += h * minRat * (float) Math.sin(fxang);
x += -w / 2f * (float) Math.cos(qxang);
y += -w / 2f * (float) Math.sin(qxang);
} else {
x += (h * minRat ) * (float) Math.cos(fxang);
y += (h * minRat) * (float) Math.sin(fxang);
x += w / 2f * (float) Math.cos(qxang);
y += w / 2f * (float) Math.sin(qxang);
}
// 旋转
AffineTransform affineTransform = new AffineTransform();
affineTransform.scale(0.8, 1);
if (isTop)
affineTransform.rotate(Math.toRadians((fxang * 180.0 / Math.PI - 90)), 0, 0);
else
affineTransform.rotate(Math.toRadians((fxang * 180.0 / Math.PI + 180 - 90)), 0, 0);
Font f2 = f.deriveFont(affineTransform);
g2d.setFont(f2);
g2d.drawString(c, x.intValue() + INIT_BEGIN, y.intValue() + INIT_BEGIN);
}
}
/**
* 画文字
*
* @param g2d 画笔
* @param circleWidth 边线圆宽度
* @param circleHeight 边线圆高度
* @param font 字体对象
*/
private static void drawFont(Graphics2D g2d, int circleWidth, int circleHeight, SealFont font) {
if (font == null) {
return;
}
//1.字体长度
int fontTextLen = font.getFontText().length();
//2.字体大小,默认根据字体长度动态设定
int fontSize = font.getFontSize() == null ? (55 - fontTextLen * 2) : font.getFontSize();
//3.字体样式
int fontStyle = font.isBold() ? Font.BOLD : Font.PLAIN;
//4.构造字体
Font f = new Font(font.getFontFamily(), fontStyle, fontSize);
g2d.setFont(f);
FontRenderContext context = g2d.getFontRenderContext();
String[] fontTexts = font.getFontText().split("\n");
if (fontTexts.length > 1) {
int y = 0;
for (String fontText : fontTexts) {
y += Math.abs(f.getStringBounds(fontText, context).getHeight());
}
//5.设置上边距
float marginSize = INIT_BEGIN + (float) (circleHeight / 2 - y / 2);
for (String fontText : fontTexts) {
Rectangle2D rectangle2D = f.getStringBounds(fontText, context);
g2d.drawString(fontText, (float) (circleWidth / 2 - rectangle2D.getCenterX() + 1), marginSize);
marginSize += Math.abs(rectangle2D.getHeight());
}
} else {
Rectangle2D rectangle2D = f.getStringBounds(font.getFontText(), context);
//5.设置上边距,默认在中心
float marginSize = font.getMarginSize() == null ?
(float) (circleHeight / 2 - rectangle2D.getCenterY()) :
(float) (circleHeight / 2 - rectangle2D.getCenterY()) + (float) font.getMarginSize();
g2d.drawString(font.getFontText(), (float) (circleWidth / 2 - rectangle2D.getCenterX() + 1), marginSize);
}
}
/**
* 画圆
*
* @param g2d 画笔
* @param circle 圆配置对象
*/
private static void drawCicle(Graphics2D g2d, SealCircle circle, int x, int y) {
if (circle == null) {
return;
}
//1.圆线条粗细默认是圆直径的1/35
int lineSize = circle.getLineSize() == null ? circle.getHeight() * 2 / (35) : circle.getLineSize();
//2.画圆
g2d.setStroke(new BasicStroke(lineSize));
g2d.drawOval(x, y, circle.getWidth() * 2, circle.getHeight() * 2);
}
}
2.3、SealCircle实体类
package com.ithuang.office.utils.configuration;
/**
* 印章圆圈类
*/
public class SealCircle {
public SealCircle(Integer lineSize, Integer width,Integer height) {
this.lineSize = lineSize;
this.width = width;
this.height = height;
}
/**
* 线宽度
*/
private Integer lineSize;
/**
* 半径
*/
private Integer width;
/**
* 半径
*/
private Integer height;
public Integer getLineSize() {
return lineSize;
}
public Integer getHeight() {
return height;
}
public Integer getWidth() {
return width;
}
}
2.4、SealConfiguration配置类
package com.ithuang.office.utils.configuration;
import java.awt.*;
/**
* 印章配置类
*/
public class SealConfiguration {
/**
* 主文字
*/
private SealFont mainFont;
/**
* 副文字
*/
private SealFont viceFont;
/**
* 抬头文字
*/
private SealFont titleFont;
/**
* 中心文字
*/
private SealFont centerFont;
/**
* 边线圆
*/
private SealCircle borderCircle;
/**
* 内边线圆
*/
private SealCircle borderInnerCircle;
/**
* 内线圆
*/
private SealCircle innerCircle;
/**
* 背景色,默认红色
*/
private Color backgroudColor = Color.RED;
/**
* 图片输出尺寸,默认300
*/
private Integer imageSize = 300;
public SealConfiguration setMainFont(SealFont mainFont) {
this.mainFont = mainFont;
return this;
}
public SealConfiguration setViceFont(SealFont viceFont) {
this.viceFont = viceFont;
return this;
}
public SealConfiguration setTitleFont(SealFont titleFont) {
this.titleFont = titleFont;
return this;
}
public SealConfiguration setCenterFont(SealFont centerFont) {
this.centerFont = centerFont;
return this;
}
public SealConfiguration setBorderCircle(SealCircle borderCircle) {
this.borderCircle = borderCircle;
return this;
}
public SealConfiguration setBorderInnerCircle(SealCircle borderInnerCircle) {
this.borderInnerCircle = borderInnerCircle;
return this;
}
public SealConfiguration setInnerCircle(SealCircle innerCircle) {
this.innerCircle = innerCircle;
return this;
}
public SealConfiguration setBackgroudColor(Color backgroudColor) {
this.backgroudColor = backgroudColor;
return this;
}
public SealConfiguration setImageSize(Integer imageSize) {
this.imageSize = imageSize;
return this;
}
public SealFont getMainFont() {
return mainFont;
}
public SealFont getViceFont() {
return viceFont;
}
public SealFont getTitleFont() {
return titleFont;
}
public SealFont getCenterFont() {
return centerFont;
}
public SealCircle getBorderCircle() {
return borderCircle;
}
public SealCircle getBorderInnerCircle() {
return borderInnerCircle;
}
public SealCircle getInnerCircle() {
return innerCircle;
}
public Color getBackgroudColor() {
return backgroudColor;
}
public Integer getImageSize() {
return imageSize;
}
}
2.5、SealFont字体类
package com.ithuang.office.utils.configuration;
import java.awt.*;
/**
* 印章字体类
*/
public class SealFont {
public SealFont(String fontText) {
this.fontText = fontText;
}
public SealFont() {
}
/**
* 字体内容
*/
private String fontText;
/**
* 是否加粗
*/
private Boolean isBold = true;
/**
* 字形名,默认为宋体
*/
private String fontFamily = "宋体";
/**
* 字体大小
*/
private Integer fontSize;
/**
* 字距
*/
private Double fontSpace;
/**
* 边距(环边距或上边距)
*/
private Integer marginSize;
/**
* 获取系统支持的字形名集合
*/
public static String[] getSupportFontNames() {
return GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
}
public SealFont setFontSpace(Double fontSpace) {
this.fontSpace = fontSpace;
return this;
}
public SealFont setMarginSize(Integer marginSize) {
this.marginSize = marginSize;
return this;
}
public SealFont setFontFamily(String fontFamily) {
this.fontFamily = fontFamily;
return this;
}
public SealFont setFontText(String fontText) {
this.fontText = fontText;
return this;
}
public SealFont setFontSize(Integer fontSize) {
this.fontSize = fontSize;
return this;
}
public SealFont setBold(Boolean bold) {
isBold = bold;
return this;
}
public String getFontText() {
return fontText;
}
public String getFontFamily() {
return fontFamily;
}
public Integer getFontSize() {
return fontSize;
}
public Double getFontSpace() {
return fontSpace;
}
public Integer getMarginSize() {
return marginSize;
}
public Boolean isBold() {
return isBold;
}
}
三、使用方法
3.1、替换Word当中的通配符
controller层代码
@GetMapping("/replaceWordText")
public String replaceWordText(HttpServletResponse response) throws Exception {
String filePath = "word通配符替换测试.docx";
String outFilePath = "result.docx";
Map<String,Object> dataMap = new HashMap<>();
dataMap.put("name","张三");
dataMap.put("password","123456");
InputStream is = new FileInputStream(filePath);
XWPFDocument doc = new XWPFDocument(is);
//替换段落里面的变量
replaceInPara(doc, dataMap);
//替换表格里面的变量
replaceInTable(doc, dataMap);
response.setCharacterEncoding("UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("result.docx", "UTF-8"));
response.setContentType("multipart/form-data");
OutputStream os = new BufferedOutputStream(response.getOutputStream());
doc.write(os);
close(os);
close(is);
return "Word通配符替换成功!";
}
准备测试文件:word通配符替换测试.docx
浏览器输入:http://localhost:8080/replaceWordText
3.2、替换Word当中的书签
controller层代码
@GetMapping("/replaceWordTag")
public String replaceWordTag(HttpServletResponse response) throws IOException {
String filePath = "word书签替换测试.docx";
String outFilePath = "result.docx";
//读文件
InputStream is = new FileInputStream(filePath);
XWPFDocument document = new XWPFDocument(is);
//书签数据
Map<String, Object> bookTagMap = new HashMap<>();
bookTagMap.put("name", "张三");
bookTagMap.put("password", "123456");
replaceBookTag(document,bookTagMap);
//返回流
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + new String("result.docx".getBytes("utf-8"), "ISO-8859-1"));
OutputStream outputStream = response.getOutputStream();
document.write(outputStream);
outputStream.flush();
outputStream.close();
document.close();
is.close();
return "Word书签替换成功!";
}
准备测试文件:word书签替换测试.docx
浏览器输入:http://localhost:8080/replaceWordTag
3.3、Word转Pdf
controller层代码
@GetMapping("/wordToPdf")
public String wordToPdf(HttpServletResponse response) throws IOException {
String filePath = "word转换pdf测试.docx";
String outFilePath = "result.pdf";
InputStream in = Files.newInputStream(Paths.get(filePath));
XWPFDocument document = new XWPFDocument(in);
PdfOptions options = PdfOptions.create();
OutputStream out = Files.newOutputStream(Paths.get(outFilePath));
PdfConverter.getInstance().convert(document,out,options);
return "pdf转换成功!";
}
准备测试文件:word转换pdf测试.docx
浏览器输入:http://localhost:8080/wordToPdf
3.4、通过Word书签在段落中插入图片
controller层代码
@GetMapping("/replacePic")
public String replacePic() throws IOException, InvalidFormatException {
FileInputStream is = new FileInputStream("段落中插入图片测试.docx");
XWPFDocument doc = new XWPFDocument(is);
Map<String,InputStream> dataMap = new HashMap<>();
dataMap.put("tag",new FileInputStream("公章.png"));
dataMap.put("pic",new FileInputStream("公章.png"));
replacePicByTag(doc,dataMap);
FileOutputStream outputStream = new FileOutputStream("result.docx");
doc.write(outputStream);
outputStream.close();
doc.close();
is.close();
return "插入成功!";
}
准备测试文件:段落中插入图片测试.docx
浏览器输入:http://localhost:8080/replacePic
3.5、生成公章
controller层代码
@GetMapping("createSeal")
public String createSeal(){
/**
* 印章配置文件
*/
SealConfiguration configuration = new SealConfiguration();
/**
* 主文字
*/
SealFont mainFont = new SealFont();
mainFont.setBold(true);
mainFont.setFontFamily("宋体");
mainFont.setMarginSize(10);
/**************************************************/
mainFont.setFontText("环境测试网络有限公司");
mainFont.setFontSize(25);
mainFont.setFontSpace(25.0);
/**
* 中心文字
*/
SealFont centerFont = new SealFont();
centerFont.setBold(true);
centerFont.setFontFamily("宋体");
centerFont.setFontText("★");
centerFont.setFontSize(100);
/**
* 抬头文字
*/
SealFont titleFont = new SealFont();
titleFont.setBold(true);
titleFont.setFontFamily("宋体");
titleFont.setFontSize(22);
titleFont.setFontText("合同章");
titleFont.setMarginSize(68);
titleFont.setFontSpace(10.0);
/**
* 添加主文字
*/
configuration.setMainFont(mainFont);
/**
* 添加中心文字
*/
configuration.setCenterFont(centerFont);
/**
* 添加抬头文字
*/
configuration.setTitleFont(titleFont);
/**
* 图片大小
*/
configuration.setImageSize(300);
/**
* 背景颜色
*/
configuration.setBackgroudColor(Color.RED);
/**
* 边线粗细、半径
*/
configuration.setBorderCircle(new SealCircle(3, 140, 140));
//1.生成公章
try {
String path = System.getProperty("user.dir");
SealUtil.buildAndStoreSeal(configuration, path+"/公章.png");
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return "印章生成成功!";
}
浏览器输入:http://localhost:8080/createSeal
3.6、Word盖章
controller层代码
@GetMapping("/createWordSeal")
public String createSeal() throws Exception {
sealInWord("word盖章测试.docx","result.docx","公章.png","盖章",0,0,0,-30,false);
return "盖章成功!";
}
准备测试文件:word盖章测试.docx
浏览器输入:http://localhost:8080/createWordSeal
文章来源:https://www.toymoban.com/news/detail-493131.html
四、Gitee源码
使用开源的Apache操作Word和Pdf等文档: 通过传统的Apache提供的POI流操作word文章来源地址https://www.toymoban.com/news/detail-493131.html
到了这里,关于SpringBoot操作Word实现文字替换和盖章(提供Gitee源码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!