UpdateFields() throws IndexOutOfRangeException - Index was outside the bounds of the array

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

Hi Nuno,


Thanks for your request. Could you please attach your input Word documents here for testing? I will investigate the issue on my side and provide you more information.

Best Regards,

Hello, attached are the files. These are simplified files of the originals but the error is still reproducible with them.


Hi,


Thanks for your inquiry. This exception occurs because the Formula Fields e.g. {= * MERGEFORMAT} in your body.doc document are either not well formed or their formula is empty. Please try supplying a valid formula by editing those fields. Moreover, you can view these formula fields by going to page 2 and pressing Alt+F9.

I hope, this will help.

Best Regards,

It worked perfectly!


Thanks for the timely response.

Regards,

Hi Awais,


Thank you for spotting the broken field. I would also ask you to log the issue and assign it to me so that I could take a look at it and add a resiliency later. The point is, if MS Word does not crash on this document, Aspose.Words should not either.

Hi Dmitry,


I have logged this exception in our bug tracking system. Client’s request has also been linked to that issue and he will be notified as soon as it is fixed.

Best Regards,

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


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

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


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