踩坑日记--poi操作word替换图片显示问题

说明

最近遇到一个比较坑的问题,就是使用poi操作word文档,通过替换书签的方式替换成图片时,图片在word中显示正常,但是通过libreoffice转成pdf时显示不全。经过测试发现,图片插入到word中默认是嵌入型,导致最终转成pdf后的显示问题。因此,对应修改图片的替换方法即可。相关代码如下:

原实现方法

public void createPicture(String blipId, int id, int width, int height, XWPFParagraph paragraph) {
    final int EMU = 9525;
    width *= EMU;
    height *= EMU;
    CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline();

    String picXml = "" +
            "" +
            "   " +
            "      " +
            "         " +
            "             + id + "\" name=\"Generated\"/>" +
            "            " +
            "         " +
            "         " +
            "             + blipId + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" +
            "            " +
            "               " +
            "            " +
            "         " +
            "         " +
            "            " +
            "               " +
            "                + width + "\" cy=\"" + height + "\"/>" +
            "            " +
            "            " +
            "               " +
            "            " +
            "         " +
            "      " +
            "   " +
            "";

    XmlToken xmlToken = null;
    try {
        xmlToken = XmlToken.Factory.parse(picXml);
    } catch (XmlException xe) {
        log.info(xe);
    }
    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("Picture " + id);
    docPr.setDescr("Generated");
}

改造后实现方法

public void createPicture(String blipId, int id, int width, int height, XWPFParagraph paragraph) {
    final int EMU = 9525;
    width *= EMU;
    height *= EMU;

    CTDrawing drawing = paragraph.createRun().getCTR().addNewDrawing();
    CTAnchor anchor = drawing.addNewAnchor();
    anchor.setBehindDoc(false);
    anchor.setLocked(false);
    anchor.setLayoutInCell(true);
    anchor.setAllowOverlap(true);
    CTAnchor inline = anchor;

    String picXml = "" +
            "" +
            "   " +
            "      " +
            "         " +
            "             + id + "\" name=\"Generated\"/>" +
            "            " +
            "         " +
            "         " +
            "             + blipId + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" +
            "            " +
            "               " +
            "            " +
            "         " +
            "         " +
            "            " +
            "               " +
            "                + width + "\" cy=\"" + height + "\"/>" +
            "            " +
            "            " +
            "               " +
            "            " +
            "         " +
            "      " +
            "   " +
            "";

    XmlToken xmlToken = null;
    try {
        xmlToken = XmlToken.Factory.parse(picXml);
    } catch (XmlException xe) {
        log.info(xe);
    }
    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("Picture " + id);
    docPr.setDescr("Generated");
}

你可能感兴趣的:(踩坑日记)