开发者

关于使用POI向word中添加图片的问题

目录
  • 使用POI向word中添加图片
  • 使用POI给Word添加水印
  • 总结

使用POI向word中添加图片

由于一次需要向word中添加多张图片,其中有图片存在重复,一开始使用的创建图片代码为:

xwpf.createPicture(xwpf.getAllPictures().size()-1, 80, 30,pargraph);
public void createPicture(int id, int width, int height,XWPFParagraph paragraph) { 
    final int EMU = 9525; 
    width *= EMU; 
    height *= EMU; 
    String blipId = getAllPictures().get(id).getPackageRelationship().getId(); 
    CTInline inline = paragraph.createRun().getCTR(编程).addNewDrawing().addNewInline(); 

    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=\"" 
        + id 
        + "\" 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>"; 

    // CTGraphicalObjectData graphicData = 
    inline.addNewGraphic().addNewGraphicData(); 
    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(js"Picture" + id); 
    docPr.setDescr("Generated"); 
  } 

上述代码对于重复的图片流不会第二次生成id,因此会造成第二次出现的图片被后续图片覆盖的情况。

因此,修改为如下处理方式,解决了重复图片的问题:

String ind = xwpf.addPictureData(is, XWPFDocument.PICTURE_TYPE_GIF);
int id = xwpf.getNextPicNameNumber(XWPFDocument.PICTURE_TYPE_GIF);
xwpf.createPicture(ind, id, 80, 30,pargraph);
public void createPicture(String blipId, int id, int width, int height,XWPFParagraph paragraph) { 
    final int EMU = 9525; 
    width *= wlbZLmEMU; 
    height *= EMU; 
    //String blipId = getAllPictures().get(id).getPackageRelationship().getId(); 
    CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline(); 

    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=\"" + id + "\" name=\"Generated\"/>" + 
        "      <pic:cNvPicPr/>" + 
        "     </pic:nvPicPr>" + 
        "     <pic:blipFill>" + 
        "      <a:blip r:embed=\"" + blipId + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" + 
        "      <a:strethttp://www.devze.comch>" + 
        "        <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>"; 

    // CTGraphicalObjectData graphicData = 
    inline.addNewGraphic().addNewGraphicData(); 
    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); 

    CTNonVisuajavascriptlDrawingProps docPr = inline.addNewDocPr(); 
    docPr.setId(id); 
    docPr.setName("Picture" + id); 
    docPr.setDescr("Generated"); 
  } 

使用POI给Word添加水印

Maven 引入依赖

   <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.17</version>
    </dependency>
   
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.17</version>
    </dependency>

Java 代码:

package com.daydayup.study001.watermark;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFHeader;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class WatermarkForWord {

    public static void main(String[] args) throws FileNotFoundException, IOException {
        XWPFDocument doc= new XWPFDocument();

          // the body content
          XWPFParagraph paragraph = doc.createParagraph();
          XWPFRun run=paragraph.createRun();  
          run.setText("The Body:");

          // create header-footer
          XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
          if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy();

          // create default Watermark - fill color black and not rotated
          headerFooterPolicy.createWatermark("Watermark");

          // get the default header
          // Note: createWatermark also sets FIRST and EVEN headers 
          // but this code does not updating those other headers
          XWPFHeader header = headerFooterPolicy.getHeader(XWPFHeaderFooterPolicy.DEFAULT);
          paragraph = header.getParagraphArray(0);

          // get com.microsoft.schemas.vml.CTShape where fill color and rotation is set
          org.apache.xmlbeans.XmlObject[] xmlobjects = paragraph.getCTP().getRArray(0).getPictArray(0).selectChildren(
            new javax.xml.namespace.QName("urn:schemas-microsoft-com:vml", "shape"));

          if (xmlobjects.length > 0) {
           com.microsoft.schemas.vml.CTShape ctshape = (com.mic开发者_JS培训rosoft.schemas.vml.CTShape)xmlobjects[0];
           // set fill color
           ctshape.setFillcolor("#d8d8d8");
           // set rotation
           ctshape.setStyle(ctshape.getStyle() + ";rotation:315");
           //System.out.println(ctshape);
          }

          doc.write(new FileOutputStream("CreateWordHeaderFooterWatermark.docx"));
          doc.close();

    }

}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜