We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Formatting HeaderFooter

Is it possible to have different fonts and styles between merge fields in a particular template including header and footer?

Like there total 10 merge fields in word template, in that 4 merge fields in header part. When generating final document with merged values, i want to apply specific font only to header merge fields(4 merge fields).

Where we have option with Run.Font , but it applies the font to entire document including all 10 merge fields and document static content.

Please let me know if Aspose have any option for my requirement. Thanks.

Hi Prashanth,

Thanks for your inquiry. In your case, I suggest you to use IFieldMergingCallback interface
to achieve your requirements as shown in following code example. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");
doc.getMailMerge().setFieldMergingCallback(new HandleMergeFieldFonts());
String[] input_names = new String[]{"MyFieldName", "MyFieldNameHeader"};
Object[] input_data = new Object[]{"My value", "Header value"};
doc.getMailMerge().execute(input_names, input_data);
doc.save(*MyDir* + "Out.docx");
private class HandleMergeFieldFonts implements IFieldMergingCallback
     * This handler is called for every mail merge field found in the document,
     * for every record found in the data source.
    public void fieldMerging(FieldMergingArgs args) throws Exception
        if(args.getField().getStart().getAncestor(NodeType.*HEADER_FOOTER*) != null)
            // Insert the text for this merge field using DocumentBuilder.
            DocumentBuilder builder = new DocumentBuilder(args.getDocument());
            if (args.getFieldValue() != null)
                // The HTML text itself should not be inserted.
                // We have already inserted it as an HTML.
    public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception
        // Do nothing.
    private DocumentBuilder mBuilder;