Free Support Forum - aspose.com

Should embedded fonts work in merge fields after performing a Mail Merge?

Hello,

I have a Word template that has an embedded TrueType font. After performing a merge, the document looks great except that the merged data isn’t in the same font… I’ve attached the template and output for your review.

Should this work?

Thanks!

Mike

Are you using latest version of Aspose.Words? I think this issue was resolved some time ago. We certainly have tests exactly for this scenario.

It should work with an embedded TrueType font as long as the whole font is embedded (not a subset of used characters only). In your case it should be alright anyway, at least in the example as most of the characters inserted during mail merge seem to be already used by the surrounding text.

Hi Roman,

This was tested with Aspose.Words 4.4.2.0. I’ll put together a sample project and attach it later today.

The entire font is embedded in the template and it’s an “installable” font, so I don’t think there’s a license issue.

More in a bit.

Mike

Hi Roman,

I didn’t manage to get a project together but I wanted to let you know that this font issue is still happening for me. Would a project help? It sounds like you probably already have one for testing this.

Let me know if I should provide more information on this.

Mike

Hi Mike,

I figured out the reason of this problem. The First Run after FieldSeparator has Times New Roman font that’s why inserted value has Times New Roman font too. There is strange issue in MSWord – you can’t set Magik font for whole mergefield. I found two workarounds of this problem.

First solution. Set Magik font for all Runs in document. See the code.

Document doc = new Document(@"479_107641_DenverMike\in.doc");

NodeCollection nodes = doc.GetChildNodes(NodeType.Run, true);

foreach (Run run in nodes)

{

run.Font.Name = "Magik";

}

string[] names = { "Contact_LastName" };

string[] values = { "McGovern" };

doc.MailMerge.Execute(names, values);

doc.Save(@"479_107641_DenverMike\out.doc");

Second solution. Use MergeField event.

public void TestMailMerge()

{

Document doc = new Document(@"479_107641_DenverMike\in.doc");

string[] names = { "Contact_LastName" };

string[] values = { "McGovern" };

doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField_107641);

doc.MailMerge.Execute(names, values);

doc.Save(@"479_107641_DenverMike\out.doc");

}

void MailMerge_MergeField(object sender, MergeFieldEventArgs e)

{

if(e.FieldName == "Contact_LastName")

{

DocumentBuilder builder = new DocumentBuilder(e.Document);

builder.MoveToMergeField(e.FieldName);

builder.Font.Name = "Magik";

builder.Write((string)e.FieldValue);

}

}

I hope this could help you.

Best regards.

Thank you for this, Alexey. Unfortunately in my case I can’t really use this because my end-users use dozens of different templates with an endless variety of fonts that I can never predict ahead of time. So for a completely dynamic solution, I’ll have to continue to ponder this.

Hi

I have created new issue #4384 in our defect database. Please expect a reply before the next hotfix (within 2-3 weeks). We might just fix it by then or provide more information.

Best regards.

Fixed, will release in 5.0.1 in a few days.

Thank you, Roman!

Hi

We have released a new version of Aspose.Words that contains a fix for your issue.


Issue # 4384 - Font of merge field is changed after mail merge.


The new version of Aspose.Words is available for download from here.

Best regards.

That’s excellent news. Many thanks!