Hi,
We use aspose.net to merge lot of word docs and we use lots of autotext fields in addition to mergefields. Recently we have run into an issue whereby if we have nested if statements which use autotext fields in it, the merge and updatefield call to update the if conditions does not work properly.
I am attaching the example document StaticTable_Aspose.docx file which is my template and the merged document is out_merged.docx. As you can see if instead of using nested IFs, if we simpligy it using Compare syntax (My example document has two different IF condition in it, the first one does not work), it works OK but we have many cases where I can not simplify the multi layer nested Ifs into simple Ifs.
I am also pasting the code I am using in a standalone class for demonstration puprose, so that it is easy for you to reporduce the issue.
I am guessing, that my code to parse and replace the fieldcode and fieldvalue is not correct for nested If conditions, but I am not sure what changes will solve my problem.
I will appreciate if you can look and let me know what the issue is and how to solve it. Please let me now if I can help by providing any more info.
Thx
Thanmal.
The Code:
private static void Test5()
{
Dictionary<string, string> dynamicFieldLabelValues = new Dictionary<string, string>();
dynamicFieldLabelValues.Add("Buyer1Name", "ThanmalB1");
dynamicFieldLabelValues.Add("Buyer1Type", "C");
dynamicFieldLabelValues.Add("Buyer1Agent1NameTitle", "ThanmalB1Agent1");
dynamicFieldLabelValues.Add("Buyer2Name", "ThanmalB2");
dynamicFieldLabelValues.Add("Buyer2Type", "C");
dynamicFieldLabelValues.Add("Buyer2Agent1NameTitle", "ThanmalB2Agent1");
dynamicFieldLabelValues.Add("Buyer3Name", "ThanmalB3");
dynamicFieldLabelValues.Add("Buyer4Name", "ThanmalB4");
dynamicFieldLabelValues.Add("Seller1Name", "ThanmalSeller1");
dynamicFieldLabelValues.Add("Seller1Type", "C");
dynamicFieldLabelValues.Add("Seller1Agent1NameTitle", "ThanmalSeller1Agent1");
dynamicFieldLabelValues.Add("Seller2Name", "ThanmalSeller2");
dynamicFieldLabelValues.Add("Seller2Type", "C");
dynamicFieldLabelValues.Add("Seller2Agent1NameTitle", "ThanmalSeller2Agent1");
dynamicFieldLabelValues.Add("Seller3Name", "ThanmalSeller3");
dynamicFieldLabelValues.Add("Seller4Name", "ThanmalSeller4");
//dynamicFieldLabelValues.Add("Buyer2Type", "C");
license = new License();
license.SetLicense("Aspose.Words.lic");
Document doc = new Document("C:\\StaticTable_Aspose.docx");
//lets merge the autotext fields first
//Create DocuentBuilder. It will help us to modify fields
DocumentBuilder builder = new DocumentBuilder(doc);
//Get collection of FieldStart nodes
NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);
//loop through all field starts and search for AutoText and Ask fields
//as these are the only ones we use currently in our templates
foreach (FieldStart start in starts)
{
if (start.FieldType == FieldType.FieldAutoText || start.FieldType == FieldType.FieldAsk || start.FieldType == FieldType.FieldFillIn)
{
Node fieldSeparator;
Node fieldEnd;
////We should get field code and field value
string fieldCode = string.Empty;
string fieldValue = string.Empty;
string tempValue = string.Empty;
string bookmark = string.Empty;
Node currentNode = start.NextSibling;
////Get Field code
while (currentNode.NodeType != NodeType.FieldSeparator)
{
if (currentNode.NodeType == NodeType.Run)
fieldCode += (currentNode as Run).Text;
currentNode = currentNode.NextSibling;
if (currentNode != null)
currentNode.PreviousSibling.Remove();
if (currentNode == null)
break;
}
fieldSeparator = currentNode;
currentNode = currentNode.NextSibling;
string newValue = "";
if (start.FieldType == FieldType.FieldAutoText)
{
//get the field value
tempValue = ParseAutoTextFields(fieldCode);
Console.WriteLine(tempValue);
//string newValue = "";
if (dynamicFieldLabelValues.ContainsKey(tempValue))
{
newValue = dynamicFieldLabelValues[tempValue].ToString();
Console.WriteLine(newValue);
}
}
////Get field value
if (currentNode != null)
{
while (currentNode.NodeType != NodeType.FieldEnd)
{
if (currentNode.NodeType == NodeType.Run)
fieldValue += (currentNode as Run).Text;
currentNode = currentNode.NextSibling;
if (currentNode != null && currentNode.PreviousSibling != null)
currentNode.PreviousSibling.Remove();
if (currentNode == null)
break;
}
}
fieldEnd = currentNode;
////Insert new field code and field value
builder.MoveTo(fieldSeparator);
builder.Write(fieldCode);
if (fieldEnd != null)
builder.MoveTo(fieldEnd);
builder.Write(newValue);
}
}
//Call the method to update the if field values
try
{
doc.UpdateFields();
}
catch (Exception ex)
{
//this might throw exception, log it and move on
Console.WriteLine(ex.Message + ex.StackTrace);
}
doc.Save("c:/out_Merged.docx", Aspose.Words.SaveFormat.Docx);
//Console.Read();
}
private static string ParseAutoTextFields(string fieldcode)
{
string fieldcodeTemp = fieldcode.Trim().Substring(fieldcode.Trim().IndexOf("Autotext ", StringComparison.InvariantCultureIgnoreCase) + 9).Trim();
return fieldcodeTemp.Replace("\\* Upper", "").Trim();
}