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

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

问题描述

我需要使用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 doc文件中.然后,我们:在文档级别添加要插入的替换图像,使用图像的大小作为准则查找包含占位符图像的运行,然后获取并替换掉该运行的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;
}

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

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