如何将具有形状的Word文档复制到另一个Word文档-Apache POI

时间:2019-09-04 10:01:32

标签: java apache-poi

我正在尝试使用Apache POI库将Word文档复制到另一个Word文档。 当段落具有形状时,结果词无法正确打开,或者结果词可以正确打开,但不显示该段落。

我尝试从原始单词中复制所有元素,但是形状没有出现在最终单词中。

现在,我尝试使用XmlCursor复制形状以从段落中检索所有图形并复制新段落中的所有图形,但是结果字未显示该形状。

public XWPFDocument insertDocumentIntoAnother(XWPFDocument documento, CTStyles estilo)
            throws XmlException, IOException {
        XWPFDocument documentoFinal = new XWPFDocument();
        if (estilo != null)
            documentoFinal.createStyles().setStyles(estilo);

        List<IBodyElement> elementList = documento.getBodyElements();

        for (IBodyElement elemento : elementList) {
            if (elemento instanceof XWPFParagraph) {

                XWPFParagraph parrafo = (XWPFParagraph) elemento;
                XWPFParagraph parrafo2 = documentoFinal.createParagraph();

                XmlCursor cursor = parrafo.getCTP().newCursor();
                cursor.selectPath(
                        "declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//*/w:txbxContent/w:p/w:r");

                List<XmlObject> ctrsintxtbx = new ArrayList<XmlObject>();
                while (cursor.hasNextSelection()) {
                    cursor.toNextSelection();
                    XmlObject obj = cursor.getObject();
                    ctrsintxtbx.add(obj);
                }

                for (XmlObject obj : ctrsintxtbx) {
                    CTR ctr = CTR.Factory.parse(obj.xmlText());
                    XWPFRun bufferrun = new XWPFRun(ctr, (IRunBody) parrafo);
                    parrafo2.addRun(bufferrun);
                }
            } else if (elemento instanceof XWPFTable) {
                XWPFTable tablaObjeto = (XWPFTable) elemento;
                documentoFinal.createTable();
                int indice = documentoFinal.getTables().size() - 1;
                documentoFinal.setTable(indice, tablaObjeto);
            }
        }

        return documentoFinal;
    }

有人知道最终文档未显示形状的原因吗?

0 个答案:

没有答案