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

Free Support Forum - aspose.com

Java Words merge field fonts

I have been working with Words in Java and I’m using mail merge to put Webding characters ( Check boxes and radio buttons ) into the document. I have tried the various way described (Font Style for MailMerge Fields) to change the font but I can’t get the font change to work in the merge process. I have ended up needing to change the font for the entire merge tag to Webdings in the template (which makes the word template very ugly to work with).

Is there a sample anyone can point me at where the font of the merge data is changed (IE: Changed to Webdings ) during the merge process rather than needing to be set to Webdings in the template?

Thanks.
Bart.

@BMorse

Could you please ZIP and attach your input and expected output documents here for our reference? We will then provide you more information about your query.

I have attached a zip with a template file, sample java code and the resulting pdf.

What I’m wanting to do is, in Java, set the font for the merge field to Wingdings. As you will see from the template, setting the merge fields to Wingdings makes the template pretty untidy, more so when there are many fields on a page.

TickBox.zip (112.7 KB)

Regards
Bart

@BMorse

Thanks for sharing the detail. Please use the following code example to get the desired output.

Document doc = new Document(MyDir + "in.docx");

String[] fieldNames = new String[]{"Q1_YTB","Q1_NTB"};
String[] fieldValues = new String[]{"",  ""};

doc.getMailMerge().setFieldMergingCallback(new IFieldMergingCallback() {

    public void fieldMerging(FieldMergingArgs e) throws Exception {
        if (mBuilder == null)
            mBuilder = new DocumentBuilder(e.getDocument());

        if (e.getFieldName().equals("Q1_YTB")) {
            mBuilder.moveToMergeField("Q1_YTB", true, true);
            mBuilder.getFont().setName("Wingdings 2");
            mBuilder.write("\u0052");
        }

        if (e.getFieldName().equals("Q1_NTB")) {
            mBuilder.moveToMergeField("Q1_NTB", true, true);
            mBuilder.getFont().setName("Wingdings 2");
            mBuilder.write("\u00A3");
        }
    }

    public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {
        // Do nothing.
    }

    private DocumentBuilder mBuilder;
});
doc.getMailMerge().execute(fieldNames, fieldValues);

doc.save(MyDir + "19.2.pdf",SaveFormat.PDF);

Fantastic, that works. Thank you for the quick response.

I think the code here is pretty much the same as I had tried ( as found via the link I first posted ) but my initial mistake was naming the font “Wingdings” rather than “Wingdings 2” and of course, using the correct Unicode value helps :grinning:.

Regards
Bart.