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,
<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;
}
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
Denver Mike:
It would be so great if doc.getMailMerge().getFieldNames() worked for "mustache fields" in the Java version of Words too.
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,
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)