使⽤POI进⾏word标签替换及添加印章及签名图⽚⽅案
1.POI引⽤:
    <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.15</version>
</dependency>
2.替换段落标签
功能代码如下:
File fileTem = new File(officeWordPath() + File.separator + WordPath());
InputStream iss = new FileInputStream(fileTem);
XWPFDocument doc = new XWPFDocument(iss);
position标签属性DateFormat dateFormat = new SimpleDateFormat("yyyy 年 MM ⽉ dd ⽇");
List<XWPFParagraph> xwpfParagraphList = Paragraphs();
//基本内容替换
for (XWPFParagraph xwpfParagraph : xwpfParagraphList) {
        CTP ctp = CTP();
for (int dwI = 0; dwI < ctp.sizeOfBookmarkStartArray(); dwI++) {
CTBookmark bookmark = BookmarkStartArray(dwI);
if (Name().equals("year")) {
XWPFRun run = ateRun();
run.setText(dateFormat.SignDate()));
Node firstNode = DomNode();
Node nextNode = NextSibling();
while (nextNode != null) {
// 循环查结束符
String nodeName = NodeName();
if (nodeName.equals(BOOKMARK_END_TAG)) {
break;
}
// 删除中间的⾮结束节点,即删除原书签内容
Node delNode = nextNode;
nextNode = NextSibling();
}
if (nextNode == null) {
// 始终不到结束标识的,就在书签前⾯添加
} else {
// 到结束符,将新内容添加到结束符之前,即内容写⼊bookmark中间
}
}
}
}
参考代码:
3.添加印章及签名图⽚
在参考原有代码基础上,更进⼀步,同时⼀位置插⼊2个图⽚:⼀个印章图⽚,⼀个签字图⽚。考虑到印章图⽚⼀般为⾮透明,采⽤居于⽂字下⽅⽅式展⽰,签字⼀般为透明图⽚,采⽤浮于⽂字上⽅⽅式展现。
⽰例代码如下(⾮⽣产环境使⽤):
  public void  creatSealDocument()throws IOException,InvalidFormatException
{
XWPFParagraph currentParagraph = null;
File fileTem = new File("d:\\关于在全市开展活动的通知.docx");
InputStream iss = new FileInputStream(fileTem);
XWPFDocument doc = new XWPFDocument(iss);
List<XWPFParagraph> xwpfParagraphList = Paragraphs();
for (int i = 0; i < xwpfParagraphList.size(); i++) {
XWPFParagraph x = (i);
List<CTBookmark> bookmarkList = x.getCTP().getBookmarkStartList();
for (int j = 0; j < bookmarkList.size(); j++) {
System.out.(j).getName());
if ((j).getName().equals("sign")) {
currentParagraph = x;
break;
}
}
}
if (currentParagraph != null) {
//添加印章图⽚
XWPFRun run = ateRun();
String imgFile1 = "d:\\t2.jpg";
FileInputStream is1 = new FileInputStream(imgFile1);
run.addPicture(is1, XWPFDocument.PICTURE_TYPE_JPEG, imgFile1, EMU(60), EMU(60));
is1.close();
CTDrawing drawing1 = CTR().getDrawingArray(0);
CTGraphicalObject graphicalobject1 = InlineArray(0).getGraphic();
Random random = new Random();
int number = Int(999) + 1;
//拿到新插⼊的图⽚替换添加CTAnchor 设置浮动属性删除inline属性
CTAnchor anchor1 = getAnchorWithGraphic(graphicalobject1, "Seal" + number,
drawing1.setAnchorArray(new CTAnchor[]{anchor1});//添加浮动属性
//添加签名图⽚
run = ateRun();
imgFile1 = "d:\\t1.jpg";
FileInputStream is2 = new FileInputStream(imgFile1);
run.addPicture(is2, XWPFDocument.PICTURE_TYPE_JPEG, imgFile1, EMU(60), EMU(60));
is2.close();
random = new Random();
CTDrawing drawing2 = CTR().getDrawingArray(0);
CTGraphicalObject graphicalobject2 = InlineArray(0).getGraphic();
number = Int(999) + 1;
CTAnchor anchor2 = getAnchorWithGraphic(graphicalobject2, "Seal" + number,
drawing2.setAnchorArray(new CTAnchor[]{anchor2});//添加浮动属性
doc.write(new FileOutputStream("d:\\关于在全市开展活动的通知2.docx"));
iss.close();
}
doc.close();
}
getAnchorWithGraphic函数代码:
public static CTAnchor getAnchorWithGraphic(CTGraphicalObject ctGraphicalObject,
String deskFileName, int width, int height,
int leftOffset, int topOffset, boolean behind) {
String anchorXML =
"<wp:anchor xmlns:wp=\"/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 = AnchorArray(0);
anchor.setGraphic(ctGraphicalObject);
return anchor;
}
效果如下:
参考:
4.输出word代码
    String fileName = de("关于在全市开展活动的通知", "UTF8") + ".docx";
String reqBrowser = Header("User-Agent");
if (LowerCase().indexOf("firefox") > 0) {
fileName = new String("关于在全市开展活动的通知".getBytes("UTF-8"), "ISO8859-1") + ".docx";
}
String headStr = "attachment; filename=\"" + fileName + "\"";
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
response.setHeader("Content-Disposition", headStr);
OutputStream out = OutputStream();
out.flush();
doc.write(out);
out.flush();
doc.close();
out.close();

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。