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

Free Support Forum - aspose.com

How to work with mustache fields in a document using C#

Is there an easy way to get a list of all the available “moustache” fields in a document? If not what is the technique that Aspose.Words uses to find them?

Hi Bruce,

Thanks for your query. There is no such method available at the moment in Aspose.Words to get list of “moustache” fields. Please use the following workaround to get “moustache” fields. Hope this helps you.

private class FieldNameList : IFieldMergingCallback

{

public List<string> fieldname = new List<string>();

void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)

{

if (args.FieldName != null)

{

fieldname.Add(args.FieldName);

}

}

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)

{

}

}

Document doc = new Document(MyDir + "TestExecuteTemplate.doc");

FieldNameList namelist = new FieldNameList();

doc.MailMerge.FieldMergingCallback = namelist;

DataSet ds = new DataSet();

ds.ReadXml(MyDir + "TestOrders.xml");

doc.MailMerge.UseNonMergeFields = true;

doc.MailMerge.ExecuteWithRegions(ds);

doc.UpdateFields();

foreach (string name in namelist.fieldname)

{

MessageBox.Show(name);

}

Hi Bruce,


Thanks for your inquiry.

Tahir is on the right track, however there some issues with his code. You would need a full data source for the document prepared in advanced in order to extract the field names from the document. Secondly after merging the fields would be replaced with data so you would need to reload the document.

Please see the code below which expands on Tahir’s idea while solving the two issues above. Also note you can combine the string array with MailMerge.GetFieldNames() in order to get the names of both types of fields in a document.

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:blue;mso-no-proof:yes”>string<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”>[]
fieldNames = MoustacheFieldNamesFinder.FindFieldNames(doc);<o:p></o:p>

public class MoustacheFieldNamesFinder : IMailMergeDataSourceRoot, IMailMergeDataSource

{

private MoustacheFieldNamesFinder(ArrayList list)

{

mFieldNames = list;

}

public IMailMergeDataSource GetDataSource(string tableName)

{

AddField(string.Format("foreach {0}", tableName));

return new MoustacheFieldNamesFinder(mFieldNames);

}

public IMailMergeDataSource GetChildDataSource(string tableName)

{

AddField(string.Format("foreach {0}", tableName));

return new MoustacheFieldNamesFinder(mFieldNames);

}

public bool GetValue(string fieldName, out object fieldValue)

{

mFieldNames.Add(fieldName);

fieldValue = null;

return false;

}

public bool MoveNext()

{

if (mIsFirstRecord)

{

mIsFirstRecord = false;

return true;

}

else

{

return false;

}

}

public string TableName

{

get { return string.Empty; }

}

public static string[] FindFieldNames(Document doc)

{

Document tempDoc = doc.Clone();

tempDoc.MailMerge.UseNonMergeFields = true;

ArrayList list = new ArrayList();

MoustacheFieldNamesFinder fieldFinder = new MoustacheFieldNamesFinder(list);

tempDoc.MailMerge.Execute(fieldFinder as IMailMergeDataSource);

tempDoc.MailMerge.ExecuteWithRegions(fieldFinder as IMailMergeDataSourceRoot);

return (string[])list.ToArray(typeof(string));

}

private void AddField(string fieldName)

{

if (!mFieldNames.Contains(fieldName))

mFieldNames.Add(fieldName);

}

private bool mIsFirstRecord = true;

private ArrayList mFieldNames;

}


Furthermore we have logged a request for retrieving both types of fields names directly from the Aspose.Words API. We will keep you informed as soon as there are any developments.

Thanks,

Thanks for the info, I'll check it out.

Having it built into the API would be great too. Cheers.

Hi Adam,

You wouldn’t happen to have a Java equivalent of this lying around, do you?

It would be so great if doc.getMailMerge().getFieldNames() worked for “mustache fields” in the Java version of Words too.

~~ Michael

Well, now that I’ve started to work more closely with “mustache” fields, I’m finding that they’re not a complete equivalent to normal merge fields. For example, I tried to use:

builder.moveToMergeField(fieldname)

and that doesn’t appear to treat mustache fields the same as regular fields.

At this point, I would love a way to dynamically convert mustache fields into normal merge fields. It probably wouldn’t be too hard to do a regex search across a document to find the fields and replace them with merge fields.

If you all happened to have that chunk of code lying around – or if Words can already do it and I just missed it – please let me know.

Thanks!

~~ Michael

Hi Michael,

Thanks for your inquiry. Yes, the DocumentBuilder.moveToMergeField do not work with "mustache" fields. I have asked for the information about "mustache" support with Aspose.Words from our development team. As soon as any information is shared by them, I will be more than happy to share that with you.

Denver Mike:
It would be so great if doc.getMailMerge().getFieldNames() worked for "mustache fields" in the Java version of Words too.
I will share the Java code equivalent of MailMerge.GetFieldNames() soon.

Thank you, Tahir.

This is a low priority for me as I have written an IReplacingCallback handler that converts mustache fields into “real” MergeFields, which solves a ton of problems for me, but still allows my users to use mustache fields.

~~ Michael

Hi Michael,


Thanks for sharing the details. It is nice to hear from you that you have already written IReplacingCallback handler. I have received response from our development team and like to share with you that “Moustache” fields have a limited support at the moment. So neither GetFieldNames nor MoveToMergeField would currently work for “Moustache” fields. However, we will improve support for these fields in future releases.


The issues you have found earlier (filed as WORDSNET-6194) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(3)

The issues you have found earlier (filed as WORDSNET-6194) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(1)