Search code examples
javagoogle-docsdocxdocx4j

docx4j - replacing a bookmark with text and style


I have a similar problem as XPages - docx4j - replacing a bookmark with text but I would like to keep the style. My bookmark is not on a text (because it's a docx created by Google Docs), I'd like to keep the text after my bookmark to include it in the bookmark and keep its style. For example :

<w:bookmarkStart w:colFirst="0" w:colLast="0" w:name="3znysh7" w:id="2"/><w:bookmarkEnd w:id="2"/>
<w:r w:rsidDel="00000000" w:rsidR="00000000" w:rsidRPr="00000000">
    <w:rPr>
        <w:rFonts w:ascii="Calibri" w:cs="Calibri" w:eastAsia="Calibri" w:hAnsi="Calibri"/>
        <w:i w:val="1"/>
        <w:color w:val="1e4d78"/>
        <w:rtl w:val="0"/>
    </w:rPr>
    <w:t xml:space="preserve">MY_BOOKMARK</w:t>
</w:r>

I found a solution to erase the text after the bookmark, I found a solution to add the text inside the bookmark, but I'm unable to keep the style.

I'm not native English speaker, so do not hesitate to ask me if I'm not clear.


Solution

  • In this case, I create a new R and insert it between bookmarkStart and bookmarkEnd. I had another problem with this kind of example :

    <w:bookmarkStart w:colFirst="0" w:colLast="0" w:name="30j0zll" w:id="1" />
          <w:bookmarkEnd w:id="1" />
          <w:p w:rsidR="00000000" w:rsidDel="00000000" w:rsidP="00000000" w:rsidRDefault="00000000" w:rsidRPr="00000000">
             <w:pPr>
                <w:pBdr />
                <w:tabs>
                   <w:tab w:val="left" w:pos="1560" />
                </w:tabs>
                <w:spacing w:after="0" w:lineRule="auto" />
                <w:contextualSpacing w:val="0" />
                <w:rPr>
                   <w:color w:val="2f5496" />
                   <w:sz w:val="28" />
                   <w:szCs w:val="28" />
                </w:rPr>
             </w:pPr>
             <w:r w:rsidDel="00000000" w:rsidR="00000000" w:rsidRPr="00000000">
                <w:rPr>
                   <w:color w:val="2f5496" />
                   <w:sz w:val="20" />
                   <w:szCs w:val="20" />
                   <w:rtl w:val="0" />
                </w:rPr>
                <w:t xml:space="preserve">MY_BOOKMARK1.</w:t>
             </w:r>
             <w:bookmarkStart w:colFirst="0" w:colLast="0" w:name="1fob9te" w:id="2" />
             <w:bookmarkEnd w:id="2" />
             <w:r w:rsidDel="00000000" w:rsidR="00000000" w:rsidRPr="00000000">
                <w:rPr>
                   <w:color w:val="2f5496" />
                   <w:sz w:val="20" />
                   <w:szCs w:val="20" />
                   <w:rtl w:val="0" />
                </w:rPr>
                <w:t xml:space="preserve">MY_BOOKMARK2</w:t>
             </w:r>
             <w:r w:rsidDel="00000000" w:rsidR="00000000" w:rsidRPr="00000000">
                <w:rPr>
                   <w:rtl w:val="0" />
                </w:rPr>
             </w:r>
          </w:p>
    

    My solution :

    1. Keep the bookmark with the name 0j0zll
    2. Create a new bookmark (id is number of bookmark in the xml file ; id++) and insert it

    Hope that could help others