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

博客讲述了在使用Java的poi库操作word文档替换图片时遇到的问题,即图片在word中显示正常,但通过libreoffice转为PDF后显示不全。问题根源在于图片被默认设置为嵌入型。通过调整图片替换方法,解决了转换后PDF的显示问题。

说明

最近遇到一个比较坑的问题,就是使用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 = "" +
            "<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>";

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

    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");
}
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值