将一个 Word 文档合并到另一个文档时图像不可见

Image not visible when merging one word doc into another

我正在尝试合并 2 个 docx 文档并成功实现了我的大部分用例。我能够成功合并文本和表格,但如果是 docx 文件中的图像,它会显示占位符而不是图像本身。 这是我的代码片段供参考:

def document
Integer i
Integer j

void mergeDocx(FileInputStream test1, FileInputStream test2, FileOutputStream dest) {
    i = 0
    j = 0
    XWPFDocument doc1 = new XWPFDocument(test1)
    XWPFDocument doc2 = new XWPFDocument(test2)
    document = new XWPFDocument()
    parseElement(doc1)
    parseElement(doc2)
    parseStyle(doc1, doc2)
    OutputStream out = dest
    document.write(out)
    out.close()
}

这是 parseElement(XWPFDocument doc) 的基础版本,我从

开始
void parseElement(XWPFDocument doc) {
    for (IBodyElement e : doc.getBodyElements()) {
        if (e instanceof XWPFParagraph) {
            XWPFParagraph p = (XWPFParagraph) e
            if (p.runs.embeddedPictures.flatten()) {
                p.runs.each { r ->
                    r.embeddedPictures.each { pic ->
                        document.addPictureData(pic.pictureData.data, pic.pictureData.pictureType)
                    }
                }
            } else {
                if (p.getCTP().getPPr() != null && p.getCTP().getPPr().getSectPr() != null) {
                    continue
                } else {
                    document.createParagraph()
                    document.setParagraph(p, i)
                    i++
                }
            }
        } else if (e instanceof XWPFTable) {
            XWPFTable t = (XWPFTable) e
            document.createTable()
            document.setTable(j, t)
            j++
        }
    }
}

这是parseElement(XWPFDocument doc)的替代版本,我用过

void parseElement(XWPFDocument doc) {
    for (IBodyElement e : doc.getBodyElements()) {
        if (e instanceof XWPFParagraph) {
            XWPFParagraph p = (XWPFParagraph) e
            if (p.runs.embeddedPictures.flatten()) {
                p.runs.each { r ->
                    r.embeddedPictures.each { pic ->
                        XWPFParagraph title = document.createParagraph()
                        XWPFRun run = title.createRun()
                        run.setText("Fig.1 A Natural Scene")
                        run.setBold(true)
                        title.setAlignment(ParagraphAlignment.CENTER)
                        run.addBreak()
                        run.addPicture(new ByteArrayInputStream(pic.pictureData.data), XWPFDocument.PICTURE_TYPE_JPEG, pic.pictureData.fileName, Units.toEMU(200), Units.toEMU(200))
                    }
                }
            } else {
                if (p.getCTP().getPPr() != null && p.getCTP().getPPr().getSectPr() != null) {
                    continue
                } else {
                    document.createParagraph()
                    document.setParagraph(p, i)
                    i++
                }
            }
        } else if (e instanceof XWPFTable) {
            XWPFTable t = (XWPFTable) e
            document.createTable()
            document.setTable(j, t)
            j++
        }
    }
}

这里的问题是,无论何时遇到图像,它都会将其视为段落的实例,然后尝试执行 setParagraph(),我知道我不应该在此处使用图像。

这是合并后我的 word docx 的样子 我为此使用 ApachePOI,但我也对使用 docx4j 的解决方案持开放态度。任何指导将不胜感激。

P.S: 编程语言是groovy.

更新我的 parseElement() 方法对我有用:

void parseElement(XWPFDocument doc) {
    for (IBodyElement e : doc.getBodyElements()) {
        if (e instanceof XWPFParagraph) {
            XWPFParagraph p = (XWPFParagraph) e
            if (p.runs.embeddedPictures.flatten()) {
                p.runs.each { r ->
                    r.embeddedPictures.each { pic ->
                        XWPFParagraph p1 = document.createParagraph()
                        XWPFRun r1 = p1.createRun()
                        int width = pic.getCTPicture().getSpPr().getXfrm().getExt().getCx() as int
                        int height = pic.getCTPicture().getSpPr().getXfrm().getExt().getCy() as int
                        int imgFormat1 = getImageFormat(pic.pictureData.fileName)
                        r1.addPicture(new ByteArrayInputStream(pic.pictureData.data), imgFormat1, pic.pictureData.fileName, width, height)
                        i++
                    }
                }
            } else {
                if (p.getCTP().getPPr() != null && p.getCTP().getPPr().getSectPr() != null) {
                    continue
                } else {
                    document.createParagraph()
                    document.setParagraph(p, i)
                    i++
                }
            }
        } else if (e instanceof XWPFTable) {
            XWPFTable t = (XWPFTable) e
            document.createTable()
            document.setTable(j, t)
            j++
        }
    }
}

我缺少的一件事是 i++ 每当我在段落中遇到图像时。