简体   繁体   English

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

[英]How to add merge field in word with docx4j?

I am able to add text and paragraphs to word and also replace any merge fields with text but I need to add a new merge field using docx4j to create a word template. 我可以向word添加文本和段落,也可以用文本替换任何合并字段,但是我需要使用docx4j添加一个新的合并字段来创建单词模板。 How can I do this? 我怎样才能做到这一点?

Do I use wordMLPackage.getMainDocumentPart().addTargetPart(targetpart); 我是否使用wordMLPackage.getMainDocumentPart().addTargetPart(targetpart); to add merge field. 添加合并字段。 If so how? 如果可以,怎么办?

Edit: 编辑:

After uploading a sample word to http://webapp.docx4java.org/OnlineDemo/PartsList.html and object representing parts I probably need to add org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart object? 在将示例词上传到http://webapp.docx4java.org/OnlineDemo/PartsList.html和代表部件的对象之后,我可能需要添加org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart对象?

But I still don't understand how to add the Mergefield. 但是我仍然不明白如何添加Mergefield。 I also generated the following xml to find any clue I could get: 我还生成了以下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>

From this I can see that the MergeField is in xml element w:fldSimple so I probably need to create this xml perhaps like this: 从中我可以看到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);

Any help would be great Thanks! 任何帮助将是巨大的谢谢!

Finally solved my own problem! 终于解决了我自己的问题! Just adding here so than anyone else stuck in the same problem can find the answer. 只需在此处添加内容,即可使陷入同一问题的其他任何人都能找到答案。

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

You need to add the relevant object to an existing paragraph, or a new paragraph. 您需要将相关对象添加到现有段落或新段落中。

To do that, add it to the relevant content list. 为此,请将其添加到相关的内容列表中。 Please read docx4j's Getting Started on how to do that, then update your question with what you've tried if you have difficulties. 请阅读docx4j的入门指南,了解如何执行此操作,如果遇到困难,请使用尝试过的内容更新问题。

As to the relevant object, you can upload a short docx containing the relevant field to webapp.docx4java.org/OnlineDemo/PartsList.html then click through to see the XML, and code to generate it. 对于相关对象,可以将包含相关字段的简短docx上传到webapp.docx4java.org/OnlineDemo/PartsList.html,然后单击以查看XML,并进行代码生成。 Note, in this case you'll want the run containing field "start", through to the field "end". 注意,在这种情况下,您将需要包含“开始”字段到“结束”字段的运行。

If you have problems, you can update your question to show the XML, and the generated code. 如果遇到问题,可以更新问题以显示XML和生成的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM