Page 1 of 1

Replace merge field inside header/footer

PostPosted: Mon Jun 13, 2016 10:28 pm
by borja
Hi,

I succesfuly replace text using the mail merge fields method, its easy and works great.

Now, i'm trying to do the same but with the field inside the header, because i want to replace text in the header of all pages generated in docs of variable length.

I tried to do with only one page and failed. Text is not replaced when merge field is inside header.

Is there a way to do this?

Thanks.

Re: Replace merge field inside header/footer

PostPosted: Mon Jun 13, 2016 11:39 pm
by jason
What code are you using?

MailMerger contains:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
       
        /**
         * A "poor man's" approach, which generates the mail merge  
         * results as a single docx, and just hopes for the best.
         * Images and hyperlinks should be ok. But numbering
         * will continue, as will footnotes/endnotes.
         * [Advert:] If this isn't working for you, the commercial Enterprise Edition of docx4j
         * (MergeDocx component) will solve your problems.
         * @param input
         * @param data
         * @param processHeadersAndFooters process headers and footers in FIRST section only.
         * If you have multiple sections in your input docx, performMerge is a better approach
         * @return
         * @throws Docx4JException
         * @ since 2.8.1
         */

        public static WordprocessingMLPackage getConsolidatedResultCrude(WordprocessingMLPackage input,
                        List<Map<DataFieldName, String>> data, boolean processHeadersAndFooters) throws Docx4JException

        /**
         * Perform merge on a single instance.
         *
         * This is the best approach, if your input has headers/footers in
         * multiple sections.
         *
         * If you are using MergeDocx, you can use that to join the
         * instances into a single docx.
         *
         * WARNING: The input docx will be modified, so input a copy if that is a problem.
         * This is left to the user, since that can potentially be more efficient, than
         * doing it here.
         *
         * @param input
         * @param data
         * @param processHeadersAndFooters
         * @return
         * @throws Docx4JException
         */

        public static void performMerge(WordprocessingMLPackage input,
                        Map<DataFieldName, String> data, boolean processHeadersAndFooters ) throws Docx4JException
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


(note the last argument processHeadersAndFooters)

Speaking more generally, one way to access the header/footer parts:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                        RelationshipsPart rp = wordMLPackage.getMainDocumentPart().getRelationshipsPart();
                        for (Relationship r : rp.getRelationships().getRelationship()) {
       
                                if (r.getType().equals(Namespaces.HEADER)) {
                                        HeaderPart hp = ((HeaderPart) rp.getPart(r));
                                        // now do something with it
                                } else if (r.getType().equals(Namespaces.FOOTER)) {
                                        FooterPart fp = ((FooterPart) rp.getPart(r));
                                        // now do something with it
                                }
                        }
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4

Re: Replace merge field inside header/footer

PostPosted: Tue Jun 14, 2016 7:29 pm
by borja
This is my code:

Code: Select all
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(docxFile));

Map<DataFieldName, String> map1 = new HashMap<DataFieldName, String>();
map1.put(new DataFieldName("dir"), DIR);
map1.put(new DataFieldName("dircli"), DIRCLI);

org.docx4j.model.fields.merge.MailMerger.setMERGEFIELDInOutput(OutputField.KEEP_MERGEFIELD);


org.docx4j.model.fields.merge.MailMerger.performMerge(wordMLPackage, map1, true);


Im not sure how can i fit your code with mine. Can i use header/footer as argument for WordprocessingMLPackage.load method?

Thank you

Re: Replace merge field inside header/footer

PostPosted: Tue Jun 14, 2016 10:16 pm
by jason
The third arg true in the last line of your code should cause headers/footers to be processed.

To see what is happening, you could turn on debug level logging for org.docx4j.model.fields.merge.MailMerger

Or you could attach your input docx

Re: Replace merge field inside header/footer

PostPosted: Wed Jun 15, 2016 3:46 am
by borja
This is interesting...

If i create the fields inside header/footer, my code ignore the fields. But i created the fields outside the header, check the code is ok, then cut the fields and paste inside the header and is working. (office 2016). This is solved.

Thank you and congrats for your work. :D