Exception: Index and length must refer to a location within the string. in UpdateFields method

I have a user who is getting the above exception message when attempting to run the UpdateFields method on a aspose.words.document. This document is generated primarily through using the InsertHtml method on the docbuilder object and getting the source Html from user input. I’ve removed all the Html except for that which generates the error and have come up with the simple link, iron

(source code is iron)

Any idea why this would be generating that error?

Thanks.

Laurie

FYI: The full error message is:

**Error: Index and length must refer to a location within the string.

Parameter name: length: at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
at x6c95d9cf46ff5f25.x0d299f323d241756.x3b67e683e83cab62(String xe4115acdf4fbfccc, Int32 x961016a387451f05)
at x4adf554d20d941a6.x50e6e6d123fa4a35.x4469479613bec4d8(Node xda5bf54deb817e37)
at x4adf554d20d941a6.x50e6e6d123fa4a35.x699caa1974f95d1a(Node xda5bf54deb817e37)
at x4adf554d20d941a6.x50e6e6d123fa4a35.x21e4bd206d6231f8(Object xa860e35844c20ac7)
at x4adf554d20d941a6.x50e6e6d123fa4a35.xb5ef8a04e65b87e6(String x1f25abf5fb75e795, Object xa860e35844c20ac7)
at x59d6a4fc5007b7a4.x487cdc969fefe3d6.xf3a1cfbffa44728c()
at x59d6a4fc5007b7a4.x487cdc969fefe3d6.x47f176deff0d42e2()
at x59d6a4fc5007b7a4.xcde671c53995c411.xd9db07500873ae98(Document x3664041d21d73fdc, xdeb77ea37ad74c56 xdfde339da46db651)
at xfbd1009a0cbb9842.x2de8d443b21560d5.xa20689e5bf116840()
at xfbd1009a0cbb9842.xbf9ddf72e1283af9.x18dfca7c5fd2402f()
at xfbd1009a0cbb9842.xfedf115fd9c03862.xdd6cf0348a23f220(xcf417e2db4fe9ed3 xe00c282e1a49fcfb)
at xfbd1009a0cbb9842.xfedf115fd9c03862.x384c03e4298b53bf()
at Aspose.Words.Document.UpdateFields()
at user_PrintPlan.PrintBusinessPlan(Object sender, EventArgs e)

Hi Laurie,
Thanks for your request. I cannot reproduce the problem on my side. I used the latest version of Aspose.Words for testing. You can download the latest version from here:
https://releases.aspose.com/words/net
I used the following code for testing:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertHtml("iron");
doc.UpdateFields();
doc.Save(@"Test001\out.doc");

Best regards,

Here’s my source code in a test file I used to recreate the error. It may have something to do with the fact that I’m using headers and generating a TOC.

aspx file:

<% @ Page Language = "C#"
AutoEventWireup = "true"
CodeFile = "TestPrintError.aspx.cs"
Inherits = "TestPrintError"
Title = "Test Print Error"
ValidateRequest = "false" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="radE" %>
AgPlan
<asp:ScriptManager ID="ScriptManager2" runat="server" />
Enter the text below, then click Print.
<asp: TextBox ID = "tbContent" runat = "server" Height = "300px" TextMode = "MultiLine" Width="400px">This is where the error happens: [iron](http://en.wikipedia.org/wiki/Iron "Iron")</asp:TextBox>
<asp:Button ID="btnPrint" runat="server" OnClick="btnPrint_Click" Text="Print" />

aspx.cs file:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using `System.IO`;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Aspose.Words;

public partial class TestPrintError: System.Web.UI.Page
{
    protected Aspose.Words.Document myDocument;
    protected Aspose.Words.DocumentBuilder myDocBuilder;
    protected string myBodyFont, myHeaderFont;
    protected int myBodyFontSize, myHeaderFontSize, mySubHeadFontSize;

    protected void Page_Load(object sender, EventArgs e)
    {
        tbContent.Focus();
    }
    protected void btnPrint_Click(object sender, EventArgs e)
    {
        myBodyFont = "Times";
        myHeaderFont = "Arial";
        myBodyFontSize = 14;
        myHeaderFontSize = 24;
        mySubHeadFontSize = 18;

        Aspose.Words.License myAsposeWordsLicense = new Aspose.Words.License();
        myAsposeWordsLicense.SetLicense("Aspose.Words.lic");

        myDocument = new Document();
        myDocBuilder = new DocumentBuilder(myDocument);

        SetTOCHeader(myDocBuilder);
        myDocBuilder.InsertHtml("Table of Contents");
        myDocBuilder.InsertBreak(BreakType.ParagraphBreak);
        SetTOCBody(myDocBuilder);
        myDocBuilder.InsertTableOfContents("\o "
            1 - 2 " \h \z \u");
        myDocBuilder.InsertBreak(BreakType.SectionBreakNewPage);

        SetHeader1(myDocBuilder);
        myDocBuilder.StartBookmark("bookmark1");
        myDocBuilder.Write("This Header1 Section");
        myDocBuilder.EndBookmark("bookmark1");

        myDocBuilder.InsertParagraph();

        SetBodyFont(myDocBuilder);
        myDocBuilder.InsertHtml(tbContent.Text);

        // Update fields for TOC
        myDocument.UpdateFields();

        string strMyWordDoc = @"C:\temp\testfile.doc";
        string strMyPdfDoc = @"C:\temp\testfile.pdf";

        // Delete files if exist
        if (File.Exists(strMyWordDoc))
            File.Delete(strMyWordDoc);
        if (File.Exists(strMyPdfDoc))
            File.Delete(strMyPdfDoc);

        // Save word document
        myDocument.Save(strMyWordDoc);

        // Save PDF Document myDocument.Save(strMyPdfDoc,SaveFormat.Pdf);

    }
    protected void SetTOCBody(Aspose.Words.DocumentBuilder myDocBuilder)
    {
        myDocBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Toc1;
        myDocBuilder.ParagraphFormat.Style.Font.Name = myBodyFont;
        myDocBuilder.ParagraphFormat.Style.Font.Size = myBodyFontSize;
        myDocBuilder.ParagraphFormat.Style.Font.Bold = false;
        myDocBuilder.ParagraphFormat.Style.Font.Italic = false;
        myDocBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Toc2;
        myDocBuilder.ParagraphFormat.Style.Font.Name = myBodyFont;
        myDocBuilder.ParagraphFormat.Style.Font.Size = myBodyFontSize;
        myDocBuilder.ParagraphFormat.Style.Font.Bold = false;
        myDocBuilder.ParagraphFormat.Style.Font.Italic = false;
    }
    protected void SetBodyFont(Aspose.Words.DocumentBuilder myDocBuilder)
    {
        myDocBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
        myDocBuilder.ParagraphFormat.Style.Font.Name = myBodyFont;
        myDocBuilder.ParagraphFormat.Style.Font.Size = myBodyFontSize;
        myDocBuilder.ParagraphFormat.Style.Font.Bold = false;
        myDocBuilder.ParagraphFormat.Style.Font.Italic = false;
    }
    protected void SetHeader1(Aspose.Words.DocumentBuilder myDocBuilder)
    {
        myDocBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
        myDocBuilder.ParagraphFormat.Style.Font.Name = myHeaderFont;
        myDocBuilder.ParagraphFormat.Style.Font.Size = myHeaderFontSize;
        myDocBuilder.ParagraphFormat.Style.Font.Bold = true;
        myDocBuilder.ParagraphFormat.Style.Font.Italic = false;
    }
    protected void SetTOCHeader(Aspose.Words.DocumentBuilder myDocBuilder)
    {
        myDocBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.TocHeading;
        myDocBuilder.ParagraphFormat.Style.Font.Name = myHeaderFont;
        myDocBuilder.ParagraphFormat.Style.Font.Size = myHeaderFontSize;
        myDocBuilder.ParagraphFormat.Style.Font.Bold = true;
        myDocBuilder.ParagraphFormat.Style.Font.Italic = false;
    }

}

Hi
Thank you for additional information. I managed to reproduce the problem on my side. Your request has been linked to the appropriate issue. You will be notified as soon as it is resolved.
Best regards,

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

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