POI XWPF 在书签中插入另一个 docx 的内容

发布于 2025-01-18 17:51:52 字数 7464 浏览 4 评论 0原文

apache poi xwpf问题:

在主 .docx 文档中,我有一个带有 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"/>

我需要替换字符串 [ [docx_content]] (放置在书签start和bookmarkend之间),外部.docx 的内容

外部.docx还包含参考文献,例如:

<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>

我尝试的所有解决方案仅复制外部.docx text ,但不复制引用。

这是我需要的外部.docx的示例要在我的主要文件的书签开始和书签之间包括:

<?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>

感谢您的任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

執念 2025-01-25 17:51:52

我修改为按照建议使用 XML 并复制外部 par 的所有内容
(包括参考文献和格式)。

但还是有一个问题:
复制的部分位于我的书签之前

...
...
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();
在单元格开头、书签之前创建一个新的 par。

如何在书签内创建新的标准?

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 ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文