POI 在Word中添加图章--浮于文字或在文字下方的图片(文字环绕)

1、添加依赖


	     org.apache.poi
	     poi-ooxml
	     3.15


	    org.apache.poi
	    poi-scratchpad
	    3.15

2、工具类


import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
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.CTDrawing;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Random;
 
public class WordSealUtil {
    public static void main(String[] args) throws Exception {
        sealInWord("D:\\test\\test.docx","D:\\test\\SealInWord.docx","D:\\test\\hh01.png","(签字/盖章)",0,0,300,-30,false);
    }
    /**
    *  Word中添加图章
    * 

Description : * String srcPath, 源Word路径 * String storePath, 添加图章后的路径 * String sealPath, 图章路径(即图片) * tString abText, 在Word中盖图章的标识字符串,如:(签字/盖章) * int width, 图章宽度 * int height, 图章高度 * int leftOffset, 图章在编辑段落向左偏移量 * int topOffset, 图章在编辑段落向上偏移量 * boolean behind,图章是否在文字下面 * @return void *

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 paragraphs = document.getParagraphs(); XWPFRun targetRun = null; for(XWPFParagraph paragraph : paragraphs){ if(!"".equals(paragraph.getText()) && paragraph.getText().contains(tabText)){ List 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);//删除行内属性 } document.write(new FileOutputStream(storePath)); document.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) { System.out.println(">>width>>"+width+"; >>height>>>>"+height); String anchorXML = "" + "" + "" + "" + leftOffset + "" + "" + "" + "" + topOffset + "" + "" + "" + "" + "" + "" + ""; CTDrawing drawing = null; try { drawing = CTDrawing.Factory.parse(anchorXML); } catch (XmlException e) { e.printStackTrace(); } CTAnchor anchor = drawing.getAnchorArray(0); anchor.setGraphic(ctGraphicalObject); return anchor; } }

你可能感兴趣的:(word,poi)