Merge field in Header & Footer is not accessible by the code

How to access added merge field in Header & Footer by the java code

@kalpeshAspose1997 You can access the merge fields in header and footer exactly the same way as you access merge fields in the mail document body.
Could you please provide your sample document along with code that will demonstrate the issue? We will check and provide you more information.

Yes sure, I have tried but unable to access it, so could you pls send me an example

@kalpeshAspose1997 Could you please provide your sample document along with code that will demonstrate the issue? We will check and provide you more information. Also, please describe your scenario on more details.

I have added merge fields in header and footer, for reference append template file I want to add those merge field value through the java code, let me know if have any doubt
testHeaderFooter_temp.docx (15.1 KB)

@kalpeshAspose1997 You can simply execute mail merge to fill the merge field with data, just like if the merge fields are in the main body:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().execute(new String[] { "testHeader", "testFooter" }, new String[] { "Header value", "Footer value" });
doc.save("C:\\Temp\\out.docx");

out.docx (10.7 KB)

1 Like

@alexey.noskov if the value contain html content (for field that added in header) then it will showing html script not actual out of html content
like this,

Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().execute(new String[] { "testHeader", "testFooter" }, new String[] { "<img width=50 height=50 src='../test.png'>", "Footer value" });
doc.save("C:\\Temp\\out.docx");

@kalpeshAspose1997 This is an expected behavior. You can use IFieldMergingCallback and DocumentBuilder.insertHtml method to insert HTML value at mergefield:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().setFieldMergingCallback(new InsertHtmlFieldMergingCallback());
doc.getMailMerge().execute(new String[] { "testHeader", "testFooter" }, new String[] { "<img width=50 height=50 src='C:/Temp/test.png'>", "Footer value" });
doc.save("C:\\Temp\\out.docx");
private static class InsertHtmlFieldMergingCallback implements IFieldMergingCallback
{
    @Override
    public void fieldMerging(FieldMergingArgs args) throws Exception {
        if(args.getFieldName().equals("testHeader"))
        {
            DocumentBuilder builder = new DocumentBuilder(args.getDocument());
            builder.moveToField(args.getField(), true);
            builder.insertHtml(args.getFieldValue().toString());
            args.setFieldValue(null);
        }
    }

    @Override
    public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {
        // do nothing
    }
}

@alexey.noskov forgot to inform you, but I have already added above code and still same issue

@kalpeshAspose1997 Could you please elaborate your problem in more derails? The code I have provided in my previous post works fine on my side with your template.

@alexey.noskov even with the html content too right ?
If I tried same but showing html script instead of show the html image
image.png (3.5 KB)
Thanks for support,

@kalpeshAspose1997 Yes, the provided code properly inserts HTML into the template. Here is the output document produced on my side using your template and code I have provided: out.docx (15.6 KB)

Okay, thank you @alexey.noskov

1 Like