Free Support Forum - aspose.com

Font Style for MailMerge Fields

Hello,

I am creating a PDF document using word template as shown in below code.

How to set the font style for the mail merge fields and regions?

Thanks for your time and help.

Hiren


AccountMailMergeDataSource bankingAccountDataSource = new AccountMailMergeDataSource(bankingAccounts);
LocaleBean localeBean =(LocaleBean) Utilities.getUIBean(“localeBean”);
String downloadDate = localeBean.getCurrentDateTimeForPrintHeader();
String assets = displayAssets();
String liabilities = displayLiabilities();
String netWorth = displayNetWorth();
doc.getMailMerge().execute(
new String[] {“DownloadDate”, “Assets”, “Liabilities”, “NetWorth”},
new Object[] {downloadDate, assets, liabilities,netWorth });
doc.getMailMerge().executeWithRegions(bankingAccountDataSource);
ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
doc.save(dstStream, SaveFormat.PDF);

Hi Hiren,

Thanks for your inquiry. You can achieve your requirements by using one of following approaches.

1) Open your template document in MS Word and format the fields (set font style) according to your requirements and then use Aspose.Words to perform mail merge operation. You can also apply formatting to text using Aspose.Words. Please read following documentation link for your kind reference.
http://www.aspose.com/docs/display/wordsjava/Specifying+Formatting

2) Implement IFieldMergingCallback interface and set font style in IFieldMergingCallback.fieldMerging. Please check code example in following documentation link.
http://www.aspose.com/docs/display/wordsjava/How+to++Apply+Custom+Formatting+during+Mail+Merge

Hope this helps you. If you still face problem, please share following detail for investigation purposes.


  • Please attach your input Word document.
  • Please

    create a standalone/runnable simple Java application that demonstrates the code (Aspose.Words code) you used to generate
    your output document

  • Please attach the output Word file that shows the undesired behavior.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated
    like.
As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Thanks for your response.


I am trying below code in IFieldMergingCallback handler method, but do not see the Font as expected in RED color.
Please let me know what I am missing.

Thanks for your time and help.

public void fieldMerging(FieldMergingArgs e) throws Exception {
if (mBuilder == null){
mBuilder = new DocumentBuilder(e.getDocument());
}
if (e.getFieldName().equals(“investingnickname”) || e.getFieldName().equals(“investingbalance”) || e.getFieldName().equals(“Assets”)) {
mBuilder.getFont().setColor(Color.RED);
}

}

Hi Hiren,

Thanks for your inquiry. Please move the cursor to the mail merge field and write the mail merge field’s value as shown below.

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

if (e.getFieldName().equals(“investingnickname”) || e.getFieldName().equals(“investingbalance”) || e.getFieldName().equals(“Assets”)) {
mBuilder.getFont().setColor(Color.RED);
mBuilder.moveToMergeField(e.getFieldName(), false, false);
mBuilder.write(e.getFieldValue().toString());

}

}


If you still face problem, please share the detail requested in my previous post for investigation purposes. I will investigate the issue on my side and provide you more information.

Thanks a lot for your consistent help.


I was able to get the font style as expected.

Hiren

Hi Hiren,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.