'POI XWPF insert content from another docx in a bookmark

APACHE POI XWPF problem:

in a main .docx document I have a cell with a bookmark:

<w:bookmarkStart w:id="8" w:name="PAR_1"/> 
   <w:r>
        <w:rPr>
            <w:rFonts w:cs="Arial"/>
                <w:lang w:val="fr-FR"/>
                    </w:rPr>
                <w:t>[[DOCX_CONTENT]]</w:t>
        </w:r>
<w:bookmarkEnd w:id="8"/>

I need to replace the string [[DOCX_CONTENT]] (placed between bookmarkStart and bookmarkEnd) with content of an external .docx.

The external .docx contains also references like:

<w:r w:rsidR="00E520CA">
  <w:rPr>
    <w:rFonts w:cs="Arial"/>
  </w:rPr>
  <w:instrText xml:space="preserve"> REF \* CHARFORMAT refName \* MERGEFORMAT</w:instrText>
</w:r>

All the solutions I tried copy only the external .docx text but not the reference.

This is sample of an external .docx i need to include between bookmarkStart and bookmarkEnd of my main document:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
            xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex"
            xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex"
            xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex"
            xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex"
            xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex"
            xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex"
            xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex"
            xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex"
            xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink"
            xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d"
            xmlns:o="urn:schemas-microsoft-com:office:office"
            xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
            xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
            xmlns:v="urn:schemas-microsoft-com:vml"
            xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
            xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
            xmlns:w10="urn:schemas-microsoft-com:office:word"
            xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
            xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
            xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml"
            xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex"
            xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid"
            xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml"
            xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash"
            xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex"
            xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
            xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
            xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
            xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"
            mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14">
    <w:body>
        <w:p w14:paraId="075E9F39"
             w14:textId="6116E8CC"
             w:rsidR="00E520CA"
             w:rsidRDefault="00516D45"
             w:rsidP="00516D45">
            <w:pPr>
                <w:rPr>
                    <w:rFonts w:cs="Arial"/>
                </w:rPr>
            </w:pPr>
            <w:proofErr w:type="spellStart"/>
            <w:r>
                <w:t>bancadatilegali</w:t>
            </w:r>
            <w:proofErr w:type="spellEnd"/>
            <w:r>
                <w:t xml:space="preserve">: </w:t>
            </w:r>
            
            <w:bookmarkStart w:id="0"
                             w:name="Cess_CR_Art1"/>

            
            <w:r w:rsidR="00E520CA">
                <w:rPr>
                    <w:rFonts w:cs="Arial"/>
                </w:rPr>
                <w:fldChar w:fldCharType="begin"/>
            </w:r>
            
            <w:r w:rsidR="00E520CA">
                <w:rPr>
                    <w:rFonts w:cs="Arial"/>
                </w:rPr>
                <w:instrText xml:space="preserve"> REF \* CHARFORMAT refName \* MERGEFORMAT </w:instrText>
            </w:r>
            <w:r w:rsidR="00E520CA">
                <w:rPr>
                    <w:rFonts w:cs="Arial"/>
                </w:rPr>
                <w:fldChar w:fldCharType="separate"/>
            </w:r>
            
            <w:r w:rsidR="00E520CA">
                <w:rPr>
                    <w:rFonts w:cs="Arial"/>
                    <w:b/>
                    <w:bCs/>
                </w:rPr>
                <w:t>Errore. origine riferimento non è stata trovata.</w:t>
            </w:r>
            <w:r w:rsidR="00E520CA">
                <w:rPr>
                    <w:rFonts w:cs="Arial"/>
                </w:rPr>
                <w:fldChar w:fldCharType="end"/>
            </w:r>
        </w:p>
        <w:p w14:paraId="60D800C9"
             w14:textId="77777777"
             w:rsidR="00E520CA"
             w:rsidRPr="00273050"
             w:rsidRDefault="00E520CA"
             w:rsidP="00273050">
            <w:pPr>
                <w:tabs>
                    <w:tab w:val="left"
                           w:pos="0"/>
                </w:tabs>
                <w:spacing w:line="240"
                           w:lineRule="atLeast"/>
                <w:rPr>
                    <w:rFonts w:cs="Arial"/>
                </w:rPr>
            </w:pPr>
        </w:p>

        <w:bookmarkEnd w:id="0"/>

        
        <w:p w14:paraId="3DB7024E"
             w14:textId="4274EB2A"
             w:rsidR="000D5B22"
             w:rsidRPr="00273050"
             w:rsidRDefault="000D5B22"
             w:rsidP="00273050"/>
        <w:sectPr w:rsidR="000D5B22"
                  w:rsidRPr="00273050">
            <w:pgSz w:w="11906"
                    w:h="16838"/>
            <w:pgMar w:top="1417"
                     w:right="1134"
                     w:bottom="1134"
                     w:left="1134"
                     w:header="708"
                     w:footer="708"
                     w:gutter="0"/>
            <w:cols w:space="708"/>
            <w:docGrid w:linePitch="360"/>
        </w:sectPr>
    </w:body>
</w:document>

Thanks for any help.



Solution 1:[1]

I modified to use, as suggested, the XML and all content of external par is copied (included references and formatting).

But still a problem: copied pars are before my bookmark

...
...
XWPFDocument  mainDoc = new XWPFDocument (new FileInputStream(pathMainDoc));

//get cell with bookmark
XWPFTable table = doc.getTables().get(0);
XWPFTableRow row = griglia.getRow(rowNum);
XWPFTableCell cell = row.getCell(cellNum);

//here is contained the bookmark:
XWPFParagraph cellPar = cell.getParagraphs().get(0);

XWPFDocument externalDoc= new XWPFDocument (new FileInputStream(pathExternalDoc));

 for (XWPFParagraph externalPar : externalDoc.getParagraphs()) {

        XWPFParagraph newPar = cell.addParagraph();
        newPar.getCTPPr().addNewRPr().set(externalPar.getCTP());

} //for pars

XWPFParagraph newPar = cell.addParagraph(); create a new par at beginning of cell, before the bookmark.

How create a new par inside a bookmark ?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1