用word中的图像替换占位符? [英] Replace a placeholder with image in word?

查看:163
本文介绍了用word中的图像替换占位符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 Apache POI 将 Word 文档中的占位符替换为图像.我可以使用 Apache poi 在 word 文档中插入图片.但我不知道如何用图像替换占位符.任何人都可以帮忙吗?

I need to replace the placeholder with image in the word document using Apache POI. I am able to insert the picture in the word document using Apache poi. But i don't know how to replace the placeholder with image. Can anyone please help in this?

我知道如果我们通过 docx4j 或其他一些 API 来做会很容易,我只能使用 Apache poi.

I know it will be easy if we do it through docx4j or some other API, i am allowed to use only the Apache poi.

推荐答案

它可以做到,但我相信您目前必须插入原始 XML 才能完成它.这个链接问题在word文档中插入图片"有基本思路.您可以只使用 POI 需要的库,而不是 dom4j.如果您查看 XWPFRun 上添加图片的方法的源代码,它也在尝试添加原始 XML.但是,如果您使用该方法,它会在写回磁盘时使您的文档不可读.因此,您必须使用 XWPFDocument 级别方法将图片添加到文档中,该方法返回为图片生成的 ID.然后将原始 XML 添加到包含该 ID 的运行中,如示例链接所示.
我们解决这个问题的方法是让我们的用户在他们的 Word 文档文件中插入一个占位符图像而不是文本.然后我们:添加要在文档级别插入的替换图像,使用图像的大小作为标准找到包含占位符图像的运行,然后获取并替换该运行的 XML,并使用新图像的 ID 交换.作为只要占位符和替换图像的大小相同,就可以使用.如果您需要在替换后调整图像的大小,您可以以相同的方式操作 XML 大小值.我更喜欢我们的解决方案,因为它不太容易受到 Word doc XML 格式更改的影响,然后为图片插入您自己的完整 XML.干杯

It can be done but I believe you must insert raw XML to accomplish it currently. This linked question "Insert picture in word document" has the basic idea. You can do it using only the libraries that POI requires, not dom4j. If you look at the source for the method on the XWPFRun that adds a picture it too is trying to add raw XML. But if you use that method it renders your doc unreadable when written back to disk. So you have to add the picture to the document using the XWPFDocument level method, which returns a generated ID for the picture. And then add raw XML to the run with that ID in it, as the example link does.
The way we solved the problem was to instead have our users insert a placeholder image into their Word doc file instead of text. We then: add the replacement image to be inserted at the document level, find the run that contains the placeholder image using the size of the image as criteria, then get and replace the XML for that run with the new image's ID swapped in. As long as the placeholder and the replacement image are the same size this works. If you need to adjust the size of the image after replacing, you could manipulate the XML size values in the same manner. I like our solution better because it is less susceptible to changes in the Word doc XML format then inserting your own full XML for the picture. Cheers

    InputStream newImageIS = getImageForCorporation(corporationID);

    String relationID = run.getParagraph().getDocument().addPictureData(newImageIS, Document.PICTURE_TYPE_GIF);
    replaceRunImageData(run, relationID);

private void replaceRunImageData(XWPFRun run, String relationID) {
    CTGraphicalObjectData graph = run.getCTR().getDrawingArray(0).getInlineArray(0).getGraphic()
            .getGraphicData();

    String currentGraphicXML = graph.toString();

    String originalID = RegularExpressionUtil.capture("<a:blip r:embed=\"(\\w+)\"", currentGraphicXML);

    String newXML = StringUtils.replace(currentGraphicXML, originalID, relationID);

    try {
        graph.set(XmlToken.Factory.parse(newXML));
    } catch (XmlException e) {
        throw new RuntimeException(e);
    }

    replaced = true;
}

我们通过搜索满足以下条件的每个运行的嵌入图片列表来确定要替换的图像运行.我们尝试使用图像的名称作为标准,但我们发现如果将占位符图像从一个 Word 文档复制到另一台 PC 上的另一个 Word 文档,名称就会丢失.

We identified the run of the image to replace by search each run's list of embedded pictures which met the below criteria. We tried using the name of the image as the criteria, but we found that if the placeholder image was copied from one Word doc to another Word doc on a different PC, the name was lost.

private boolean isRunForExistingImage(XWPFPicture pic) {
    if (pic == null || pic.getCTPicture() == null || pic.getCTPicture().getSpPr() == null
            || pic.getCTPicture().getSpPr().getXfrm() == null
            || pic.getCTPicture().getSpPr().getXfrm().getExt() == null) {
        return false;
    }

    long x = pic.getCTPicture().getSpPr().getXfrm().getExt().getCx();
    long y = pic.getCTPicture().getSpPr().getXfrm().getExt().getCy();

    return x == 2066925 && y == 590550;
}

这篇关于用word中的图像替换占位符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆