Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用docx4j在word中添加合并字段?_Java_Docx4j - Fatal编程技术网

Java 如何使用docx4j在word中添加合并字段?

Java 如何使用docx4j在word中添加合并字段?,java,docx4j,Java,Docx4j,我可以向word添加文本和段落,也可以用文本替换任何合并字段,但我需要使用docx4j添加一个新的合并字段来创建word模板。我该怎么做 我是否使用wordMLPackage.getMainDocumentPart().addTargetPart(targetpart)以添加合并字段。如果是,怎么做 编辑: 将示例单词上载到表示部件的对象和对象后,我可能需要添加org.docx4j.openpackaging.parts.WordprocessingML.main文档部件对象 但我仍然不明白如何

我可以向word添加文本和段落,也可以用文本替换任何合并字段,但我需要使用docx4j添加一个新的合并字段来创建word模板。我该怎么做

我是否使用
wordMLPackage.getMainDocumentPart().addTargetPart(targetpart)以添加合并字段。如果是,怎么做

编辑:

将示例单词上载到表示部件的对象和对象后,我可能需要添加org.docx4j.openpackaging.parts.WordprocessingML.main文档部件对象

但我仍然不明白如何添加合并字段。我还生成了以下xml,以找到任何线索:

<w:document xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:ns21="urn:schemas-microsoft-com:office:powerpoint" xmlns:ns23="http://schemas.microsoft.com/office/2006/coverPageProps" xmlns:dsp="http://schemas.microsoft.com/office/drawing/2008/diagram" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:odx="http://opendope.org/xpaths" xmlns:odgm="http://opendope.org/SmartArt/DataHierarchy" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:ns17="urn:schemas-microsoft-com:office:excel" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:odi="http://opendope.org/components" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:ns32="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:ns30="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" xmlns:ns12="http://schemas.openxmlformats.org/drawingml/2006/chartDrawing" xmlns:ns31="http://schemas.openxmlformats.org/drawingml/2006/compatibility" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:odq="http://opendope.org/questions" xmlns:ns8="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:odc="http://opendope.org/conditions" xmlns:oda="http://opendope.org/answers">
    <w:body>
        <w:p w:rsidR="00F66879" w:rsidP="00E80FA3" w:rsidRDefault="003B0B3A">
            <w:r>
                <w:t>Dear</w:t>
            </w:r>
            <w:r w:rsidR="00E80FA3">
                <w:t xml:space="preserve"> </w:t>
            </w:r>
            <w:fldSimple w:instr=" MERGEFIELD  lastName  \* MERGEFORMAT ">
                <w:r w:rsidR="005C0283">
                    <w:rPr>
                        <w:noProof/>
                    </w:rPr>
                    <w:t>«lastName»</w:t>
                </w:r>
            </w:fldSimple>
            <w:r w:rsidR="00E80FA3">
                <w:t>,</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="00532EBF" w:rsidP="00E80FA3" w:rsidRDefault="00532EBF">
            <w:r>
                <w:t>On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your current document look.</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="00532EBF" w:rsidP="00E80FA3" w:rsidRDefault="00532EBF">
            <w:r>
                <w:t>You can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You can also format text directly by using the other controls on the Home tab. Most controls offer a choice of using the look from the current theme or using a format that you specify directly.
</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="001E4C13" w:rsidP="00E80FA3" w:rsidRDefault="001E4C13">
            <w:r>
                <w:t>Thank you</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidR="001E4C13" w:rsidP="00E80FA3" w:rsidRDefault="001E00E0">
            <w:r>
                <w:t>Sincerely,</w:t>
            </w:r>
        </w:p>
        <w:p w:rsidRPr="00F66879" w:rsidR="001E00E0" w:rsidP="00E80FA3" w:rsidRDefault="00C3747B">
            <w:fldSimple w:instr=" MERGEFIELD  sender  \* MERGEFORMAT ">
                <w:r w:rsidR="001E00E0">
                    <w:rPr>
                        <w:noProof/>
                    </w:rPr>
                    <w:t>«sender»</w:t>
                </w:r>
            </w:fldSimple>
        </w:p>
        <w:p w:rsidR="00E80FA3" w:rsidRDefault="00E80FA3"/>
        <w:sectPr w:rsidR="00E80FA3" w:rsidSect="00961253">
            <w:pgSz w:w="12240" w:h="15840"/>
            <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/>
            <w:cols w:space="720"/>
            <w:docGrid w:linePitch="360"/>
        </w:sectPr>
    </w:body>
</w:document>

亲爱的
«姓氏»
,
在“插入”选项卡上,库中包含与文档整体外观协调的项目。您可以使用这些库插入表、页眉、页脚、列表、封面和其他文档构建块。创建图片、图表或图表时,它们还与当前文档外观相协调。
通过从“常用”选项卡上的“快速样式库”中选择选定文本的外观,可以轻松更改文档文本中选定文本的格式。也可以使用“主页”选项卡上的其他控件直接格式化文本。大多数控件提供了使用当前主题的外观或使用直接指定的格式的选择。
非常感谢。
真诚地
«发送者»
从这里,我可以看到MergeField位于xml元素w:fldSimple中,因此我可能需要创建如下xml:

   CTSimpleField ctSimple = factory.createCTSimpleField();
        ctSimple.setInstr(" MERGEFIELD lastname \\* MERGEFORMAT");

        RPr RPr = factory.createRPr();
        RPr.setNoProof(new BooleanDefaultTrue());

        Text t = factory.createText();
        t.setValue("fieldValue");

        R run = factory.createR();
        run.getContent().add(RPr);
        run.getContent().add(t);   

        JAXBElement<CTSimpleField> fldSimple = factory
                .createPFldSimple(ctSimple);
CTSimpleField ctSimple=factory.createCTSimpleField();
ctSimple.setInstr(“MERGEFIELD lastname\\*MERGEFORMAT”);
RPr=factory.createRPr();
RPr.setNoProof(新的booleandaulttrue());
Text t=factory.createText();
t、 设置值(“字段值”);
R run=factory.createR();
run.getContent().add(RPr);
run.getContent().add(t);
JAXBElement fldSimple=工厂
.createPFldSimple(ctSimple);

任何帮助都很好,谢谢

您需要将相关对象添加到现有段落或新段落中

为此,请将其添加到相关内容列表中。请阅读docx4j的入门指南,如果您遇到困难,请用您尝试过的方法更新您的问题

对于相关对象,您可以上传一个包含相关字段的简短docx,然后单击查看XML,并编写代码生成它。注意,在这种情况下,您需要包含字段“开始”的运行,直到字段“结束”


如果您有问题,可以更新您的问题以显示XML和生成的代码。

最终解决了我自己的问题!只需在这里添加,就可以让其他陷入相同问题的人找到答案

ObjectFactory factory = Context.getWmlObjectFactory();


    CTSimpleField ctSimple = factory.createCTSimpleField();
    ctSimple.setInstr(" MergeField fieldValue \\* MERGEFORMAT ");

    RPr RPr = factory.createRPr();
    RPr.setNoProof(new BooleanDefaultTrue());

    Text t = factory.createText();
    t.setValue("fieldValue");

    R run = factory.createR();
    run.getRunContent().add(RPr);
    run.getRunContent().add(t);

    ctSimple.getParagraphContent().add(run);

    JAXBElement<CTSimpleField> fldSimple = factory
            .createPFldSimple(ctSimple);

    // P
    P para = factory.createP();
    para.getParagraphContent().add(fldSimple);


    Body body = factory.createBody();
    body.getEGBlockLevelElts().add(para);
    wordMLPackage.getMainDocumentPart().addObject(body);
ObjectFactory=Context.getWmlObjectFactory();
CTSimpleField ctSimple=factory.createCTSimpleField();
ctSimple.setInstr(“mergefieldvalue\\*MERGEFORMAT”);
RPr=factory.createRPr();
RPr.setNoProof(新的booleandaulttrue());
Text t=factory.createText();
t、 设置值(“字段值”);
R run=factory.createR();
run.getRunContent().add(RPr);
run.getRunContent().add(t);
ctSimple.getParagraphContent().add(运行);
JAXBElement fldSimple=工厂
.createPFldSimple(ctSimple);
//P
P para=factory.createP();
para.getParagraphContent().add(fldSimple);
Body Body=factory.createBody();
body.getEGBlockLevelElts().add(段落);
wordMLPackage.getMainDocumentPart().addObject(正文);

在做了更多的研究之后,我更新了我的问题。谢谢我指给你的webapp应该为你生成类似的代码?@JasonPlutext:你指给你的webapp生成了表示零件和零件内容的对象。我不明白的是合并字段“CTSimpleField”要使用哪些对象,以及如何将其设置为“ctSimple.setInstr()”。我是在使用System.out.println(XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(),true,true))打印出xml后发现的;。这有助于我更好地理解它是如何工作的。谢谢您必须单击document.xml部分,然后单击相关的元素名称,然后才能看到代码?