JAVA POI Word模板动态数据填充并下载(包含图片,并可悬浮文字上方)

该博客介绍了如何解决Apache POI库中XWPFDocument类在插入图片时出现的错误,导致Word无法打开或图片不显示的问题。作者创建了一个名为CustomXWPFDocument的类来继承XWPFDocument,并重写了插入图片的方法。通过这个新类,可以正确地在Word文档中插入并显示图片。博客还提供了具体的代码实现,包括处理段落和表格中的文本替换以及图片插入。
摘要由CSDN通过智能技术生成

因为原本的XWPFDocument类插入图片方法有bug,要么打不开word,要么打开了不显示图片,所以新建一个CustomXWPFDocument类继承XWPFDocument,重写插入图片方法。

 /**
     * 
     * Description:获取word 重新赋值并下载信息(下载方法上篇文章)
     * @return void
     * Object 需要替换的实体或者map
     * @throws Exception
     *
     */
	public static ByteArrayOutputStream writeWord(Object obj) throws Exception {
		Map<String, Object> data = new HashMap<>();
        Map<String, Object> pic = new HashMap<>();
 
        data.put("${name}", obj.属性);
        
        pic.put("width", 130);
		pic.put("height", 130);
		pic.put("type", "png");
		pic.put("picPath", "/images/pig.png");
		data.put("{sing}",pic);
		ByteArrayOutputStream baos = null;
		try {
			XWPFDocument document = CustomXWPFDocument.generateWord(data, downloadDto.getFilePath());
			baos = new ByteArrayOutputStream();
			document.write(baos);//临时存储流到内存
	        baos.flush();
		} catch (Exception e) {
			 e.printStackTrace();
        }finally{
        	if(baos != null){
        		baos.close();
        	}
        }
		return baos;
	}

CustomXWPFDocument类

package *;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlToken;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing;
 




import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Map.Entry;

public class CustomXWPFDocument extends XWPFDocument {
	
    public CustomXWPFDocument(InputStream in) throws IOException {
        super(in);
    }
    
    public CustomXWPFDocument() {
        super();
    }
    
    public CustomXWPFDocument(OPCPackage pkg) throws IOException {
        super(pkg);
    }
    
    /**
	 * 根据指定的参数值、模板,生成 word 文档
	 * @param param 需要替换的变量
	 * @param template 模板
	 */
	public static XWPFDocument generateWord(Map<String, Object> param, String template) {
		XWPFDocument doc = null;
		try {
			OPCPackage pack = POIXMLDocument.openPackage(template);
			doc = new CustomXWPFDocument(pack);
			if (param != null && param.size() > 0) {
				
				//处理段落
				List<XWPFParagraph> paragraphList = doc.getParagraphs();
				processParagraphs(paragraphList, param, doc);
				
				//处理表格
				Iterator<XWPFTable> it = doc.getTablesIterator();
				while (it.hasNext()) {
					XWPFTable table = it.next();
					List<XWPFTableRow> rows = table.getRows();
					for (XWPFTableRow row : rows) {
						List<XWPFTableCell> cells = row.getTableCells();
						for (XWPFTableCell cell : cells) {
							List<XWPFParagraph> paragraphListTable =  cell.getParagraphs();
							processParagraphs(paragraphListTable, param, doc);
						}
					}
				}
			}
		} catch(IOException e){
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return doc;
	}
	
	/**
	 * 处理段落
	 * @param paragraphList
	 * @param docPath 
	 * @throws FileNotFoundException 
	 * @throws InvalidFormatException 
	 */
	public static void processParagraphs(List<XWPFParagraph> paragraphList,Map<String, Object> param,XWPFDocument doc) throws InvalidFormatException, FileNotFoundException{
		if(paragraphList != null && paragraphList.size() > 0){
			for(XWPFParagraph paragraph:paragraphList){
				List<XWPFRun> runs = paragraph.getRuns();
				for (XWPFRun run : runs) {
					String text = run.getText(0);
					//System.out.println("text=="+text);
					if(text != null){
						boolean isSetText = false;
						for (Entry<String, Object> entry : param.entrySet()) {
							String key = entry.getKey();
							if(text.indexOf(key) != -1){
								isSetText = true;
								Object value = entry.getValue();
								if (value instanceof String) {//文本替换
									//System.out.println("key=="+key);
									text = text.replace(key, value.toString());
								} else if (value instanceof Map) {	//图片替换
									text = text.replace(text, "");
									Map pic = (Map)value;
									int width = Integer.parseInt(pic.get("width").toString());
									int height = Integer.parseInt(pic.get("height").toString());
									int picType = getPictureType(pic.get("type").toString());
									String picPath = (String) pic.get("picPath");
									CTInline inline = run.getCTR().addNewDrawing().addNewInline();
									insertPicture(doc,picPath,inline, width, height);
									CTDrawing drawing = run.getCTR().getDrawingArray(0);
									CTGraphicalObject graphicalobject = drawing.getInlineArray(0).getGraphic();
									//拿到新插入的图片替换添加CTAnchor 设置浮动属性 删除inline属性
									CTAnchor anchor = getAnchorWithGraphic(graphicalobject, "11",
											Units.toEMU(width), Units.toEMU(height),//图片大小
											Units.toEMU(0), Units.toEMU(0));//相对当前段落位置的偏移位置,左右偏移:负数向左,正数向右,上下偏移:负数向上,正数向下
									drawing.setAnchorArray(new CTAnchor[]{anchor});//添加浮动属性
									drawing.removeInline(0);//删除行内属性
								}
							}
						}
						if(isSetText){
							run.setText(text,0);
						}
					}
				}
			}
		}
	}
	
	/** 
     * 根据图片类型,取得对应的图片类型代码 
     * @param picType 
     * @return int 
     */  
    private static int getPictureType(String picType){  
        int res = CustomXWPFDocument.PICTURE_TYPE_PICT;  
        if(picType != null){  
            if(picType.equalsIgnoreCase("png")){  
                res = CustomXWPFDocument.PICTURE_TYPE_PNG;  
            }else if(picType.equalsIgnoreCase("dib")){  
                res = CustomXWPFDocument.PICTURE_TYPE_DIB;  
            }else if(picType.equalsIgnoreCase("emf")){  
                res = CustomXWPFDocument.PICTURE_TYPE_EMF;  
            }else if(picType.equalsIgnoreCase("jpg") || picType.equalsIgnoreCase("jpeg")){  
                res = CustomXWPFDocument.PICTURE_TYPE_JPEG;  
            }else if(picType.equalsIgnoreCase("wmf")){  
                res = CustomXWPFDocument.PICTURE_TYPE_WMF;  
            }  
        }  
        return res;  
    }
    
    /**
	    * insert Picture
	    * @param document
	    * @param filePath
	    * @param inline
	    * @param width
	    * @param height
	    * @throws InvalidFormatException
	    * @throws FileNotFoundException
	    */
	   private static void insertPicture(XWPFDocument document, String filePath,
	                              CTInline inline, int width,
	                              int height) throws InvalidFormatException,
	                                                 FileNotFoundException {
	       document.addPictureData(new FileInputStream(filePath),XWPFDocument.PICTURE_TYPE_PNG);
	       int id = document.getAllPictures().size() - 1;
	       final int EMU = 9525;
	       width *= EMU;
	       height *= EMU;
	       String blipId =
	           document.getAllPictures().get(id).getPackageRelationship().getId();
	       String picXml = getPicXml(blipId, width, height);
	       XmlToken xmlToken = null;
	       try {
	           xmlToken = XmlToken.Factory.parse(picXml);
	       } catch (XmlException xe) {
	           xe.printStackTrace();
	       }
	       inline.set(xmlToken);
	       inline.setDistT(0);
	       inline.setDistB(0);
	       inline.setDistL(0);
	       inline.setDistR(0);
	       CTPositiveSize2D extent = inline.addNewExtent();
	       extent.setCx(width);
	       extent.setCy(height);
	       CTNonVisualDrawingProps docPr = inline.addNewDocPr();
	       docPr.setId(id);
	       docPr.setName("IMG_" + id);
	       docPr.setDescr("IMG_" + id);
	   }
	   /**
	    * get the xml of the picture
	    * @param blipId
	    * @param width
	    * @param height
	    * @return
	    */
	   private static String getPicXml(String blipId, int width, int height) {
	       String picXml =
	           "" + "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
	           "   <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
	           "      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
	           "         <pic:nvPicPr>" + "            <pic:cNvPr id=\"" + 0 +
	           "\" name=\"Generated\"/>" + "            <pic:cNvPicPr/>" +
	           "         </pic:nvPicPr>" + "         <pic:blipFill>" +
	           "            <a:blip r:embed=\"" + blipId +
	           "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" +
	           "            <a:stretch>" + "               <a:fillRect/>" +
	           "            </a:stretch>" + "         </pic:blipFill>" +
	           "         <pic:spPr>" + "            <a:xfrm>" +
	           "               <a:off x=\"0\" y=\"0\"/>" +
	           "               <a:ext cx=\"" + width + "\" cy=\"" + height +
	           "\"/>" + "            </a:xfrm>" +
	           "            <a:prstGeom prst=\"rect\">" +
	           "               <a:avLst/>" + "            </a:prstGeom>" +
	           "         </pic:spPr>" + "      </pic:pic>" +
	           "   </a:graphicData>" + "</a:graphic>";
	       return picXml;
	   }
    
    /**
	 * @param ctGraphicalObject 图片数据
	 * @param deskFileName      图片描述
	 * @param width             宽
	 * @param height            高
	 * @param leftOffset        水平偏移:负数向左,正数向右
	 * @param topOffset         垂直偏移:负数向上,正数向下
	 * @return CTAnchor
	 * @throws Exception
	 */
	public static CTAnchor getAnchorWithGraphic(CTGraphicalObject ctGraphicalObject,
												String deskFileName, int width, int height,
												int leftOffset, int topOffset) {
		//浮在文字上的设置主要是anchor标签下的behindDoc属性设为0,同时添加一个<wp:wrapNone/>的空标签。
		if (StringUtils.isBlank(deskFileName)) {
			deskFileName = new Random().nextInt(999) + "";//描述不能为空,赋值一个随机数
		}
		String anchorXML =
				"<wp:anchor xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" "
						+ "simplePos=\"0\" relativeHeight=\"0\" behindDoc=\"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;
	}
	
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值