Hi there,
We have an application which opens two word documents, merges it’s contents and then invokes the UpdateFields() method.
Our client reported an error, when using the above functionality. I was (and am) able to reproduce the problem in my dev. machine. The error occurs when invoking UpdateFields()
The complete error stack strace is:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.String.get_Chars(Int32 index)
at x2a6f63b6650e76c4.xd8780e89a96f3f1a.x9f7dc7131ed07aa4(String x137ffa3012d6a67d)
at x2a6f63b6650e76c4.xd8780e89a96f3f1a.x095ca45783003376()
at x2a6f63b6650e76c4.xd8780e89a96f3f1a.x1f490eac106aee12()
at x2a6f63b6650e76c4.xd8780e89a96f3f1a.x1f490eac106aee12(x12e7545fad3ccc9b x0f7b23d1c393aed9)
at x2a6f63b6650e76c4.x6d929209cd800011.x308cb2f3483de2a6(x12e7545fad3ccc9b x0f7b23d1c393aed9)
at x2a6f63b6650e76c4.x6d929209cd800011.x308cb2f3483de2a6(Field xe01ae93d9fe5a880, String xbf5efe8743edba7b)
at xfbd1009a0cbb9842.xef7c7014677e653f.x83bcdf1790545fdb()
at Aspose.Words.Fields.Field.x295cb4a1df7a5add(x5e36356bc92c609b x0f7b23d1c393aed9)
at xfbd1009a0cbb9842.xfedf115fd9c03862.x4e3cfc222c92cda7(Field xe01ae93d9fe5a880, x5e36356bc92c609b x0f7b23d1c393aed9)
at xfbd1009a0cbb9842.xfedf115fd9c03862.x4e3cfc222c92cda7(Field xe01ae93d9fe5a880)
at xfbd1009a0cbb9842.xbf9ddf72e1283af9.x18dfca7c5fd2402f()
at xfbd1009a0cbb9842.xfedf115fd9c03862.xdd6cf0348a23f220(xcf417e2db4fe9ed3 xe00c282e1a49fcfb)
at xfbd1009a0cbb9842.xfedf115fd9c03862.x384c03e4298b53bf()
at Aspose.Words.Range.UpdateFields()
at Aspose.Words.Document.UpdateFields()
at Echiron.ChiToolkit.UserControls.ListWorkspace.Forms.UserControls.FormActionWord.GenWord(String templateCode, String itemCode, String symbol, Dictionary`2 formParams)
at Echiron.ChiToolkit.UserControls.ListWorkspace.Forms.UserControls.FormActionWord.FormButton_Click(Object sender, RoutedEventArgs e)
The source code is simple:
// Merge everything
if (bodyTmplContent != null)
{
// Doc1 - Body
Aspose.Words.Document docBody = new Document(new MemoryStream(bodyTmplContent));
DocumentBuilder docBodyBuilder = new DocumentBuilder(docBody);
Section bodyCurrentSection = docBodyBuilder.CurrentSection;
PageSetup bodyPageSetup = bodyCurrentSection.PageSetup;
if (hfTmplContent != null)
{
// Doc2 - Header/Footer
Aspose.Words.Document docHeaderFooter = new Document(new MemoryStream(hfTmplContent));
DocumentBuilder docHeaderFooterBuilder = new DocumentBuilder(docHeaderFooter);
bodyPageSetup.DifferentFirstPageHeaderFooter =
docHeaderFooterBuilder.PageSetup.DifferentFirstPageHeaderFooter;
// … merge both templates …
docBody.FirstSection.HeadersFooters.Clear();
foreach (HeaderFooter node in docHeaderFooter.GetChildNodes(NodeType.HeaderFooter, true))
{
Node newNode = docBody.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
docBody.FirstSection.HeadersFooters.Add(newNode);
}
}
// … cycle all aspose “Run” nodes and replace tokens with values
foreach (Run run in docBody.GetChildNodes(NodeType.Run, true))
{
run.Text = regex.Replace(run.Text, match => formParams[match.Groups[1].Value]);
}
// update index
docBody.UpdateFields();
// Save new document to database
MemoryStream newFileContent = new MemoryStream();
if (docBody.OriginalLoadFormat == LoadFormat.Docx)
{
var opt = new OoxmlSaveOptions(SaveFormat.Docx);
opt.Compliance = OoxmlCompliance.Iso29500_2008_Transitional;
docBody.Save(newFileContent, opt);
}
else
{
docBody.Save(newFileContent, null);
}
…
}
----
Thanks for any help.
Regards,
Nuno Leong