Page 1 of 1

How can I replace marked text to some another text

PostPosted: Thu Aug 23, 2012 1:02 am
by bfgsuperk
Hi!

I need help, does docx4j contain API that can replace some marked text to another? For example:
Word Text: "My name is {FirstName} {LastName}. I am living in Moscow."
I want to change marked fields: {FirstName} and {LastName} to some specific text, what is the easist way to do it?

I am trying to look at Field Codes way (How you can use it "Insert > Quick Parts > Field > Field Codes" ).
The XML will contain following tag w:instrText:
Code: Select all
            <w:r w:rsidRPr="00B22DC3">
                <w:rPr>
                    <w:b/>
                    <w:u w:val="single"/>
                    <w:lang w:val="en-US"/>
                </w:rPr>
                <w:instrText>MERGEFORMAT</w:instrText>
            </w:r>

P.S. The technology how marked fields will be dedicated doesn't matter.

Sorry for my language:)

Re: How can I replace marked text to some another text

PostPosted: Thu Aug 23, 2012 9:35 am
by jason
As explained in Getting Started, the simplest - but most brittle - is to use VariableReplace with something like ${FirstName} as a simple string in the document.

From https://github.com/plutext/docx4j/blob/ ... place.java:

Code: Select all
/**
* There are at least 3 approaches for replacing variables in
* a docx.
*
* 1. as shows in this example
* 2. using Merge Fields (see org.docx4j.model.fields.merge.MailMerger)
* 3. binding content controls to an XML Part (via XPath)
*
* Approach 3 is the recommended one when using docx4j. See the
* ContentControl* examples, Getting Started, and the subforum.
*
* Approach 1, as shown in this example, works in simple cases
* only.  It won't work if your KEY is split across separate
* runs in your docx (which often happens), or if you want
* to insert images, or multiple rows in a table.
*
* You're encouraged to investigate binding content controls
* to an XML part.
*
*/


If you want to use MergeFields, see now https://github.com/plutext/docx4j/blob/ ... Merge.java

Re: How can I replace marked text to some another text

PostPosted: Thu Aug 23, 2012 6:02 pm
by bfgsuperk
The example https://github.com/plutext/docx4j/blob/ ... place.java doesn't work. Because the text "${FirstName}" can be split to several XML element:
Code: Select all
            <w:r w:rsidR="00351400" w:rsidRPr="00351400">
                <w:rPr>
                    <w:u w:val="single"/>
                </w:rPr>
                <w:t>$</w:t>
            </w:r>
            <w:r w:rsidR="008461D6" w:rsidRPr="008461D6">
                <w:rPr>
                    <w:b/>
                    <w:sz w:val="22"/>
                    <w:szCs w:val="22"/>
                    <w:u w:val="single"/>
                </w:rPr>
                <w:t>{.</w:t>
            </w:r>
            <w:r w:rsidR="008461D6" w:rsidRPr="008461D6">
                <w:rPr>
                    <w:b/>
                    <w:noProof/>
                    <w:sz w:val="22"/>
                    <w:szCs w:val="22"/>
                    <w:u w:val="single"/>
                    <w:lang w:val="en-US"/>
                </w:rPr>
                <w:t>SomethingValue</w:t>
            </w:r>
            <w:r w:rsidR="008461D6" w:rsidRPr="008461D6">
                <w:rPr>
                    <w:b/>
                    <w:sz w:val="22"/>
                    <w:szCs w:val="22"/>
                    <w:u w:val="single"/>
                </w:rPr>
                <w:t>2}</w:t>
            </w:r>


I will taste to use MergeFields.

Re: How can I replace marked text to some another text

PostPosted: Thu Aug 23, 2012 6:55 pm
by bfgsuperk
This is "https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/samples/FieldsMailMerge.java" working but unfortunately for me it does not support mergefield like ".Employee.LastName". This field will be splited up several XML elemenents.

Thank's Jason for your help.

Re: How can I replace marked text to some another text

PostPosted: Thu Aug 23, 2012 11:34 pm
by jason
Could you please explain in more detail what you mean, including posting sample XML? thanks