Aspose.Words: Index was outside the bounds of the array

Hello,
We are having occasional errors in our component (CCDocumentos using Aspose.Words 9.1). When this error happens ALL the SAVE methods that we have in our source code fail. Then a 0Kb document is generated by Aspose.Words.
When we kill W3WP.exe process running Aspose.Words the errors dissappear.
What is happening? This kind of errors are very critical for us.

Error: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Ó".ਙ.BinarySearch(Int32[] array, Int32 index, Int32 length, Int32 value)
at Ó".਑.set_Item(Int32 key, Object value)
at ඞ.ᐳ.ᐷ(Int32 ਘ)
at ⚰.⨶.WriteCore()
at ⚰.⡏.ࣙ(ᐮ ഷ)
at â™—.â™–.âš„()
at â™—.â™–.VisitRowEnd(Row row)
at Aspose.Words.Tables.Row.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.Ꭸ(DocumentVisitor կ)
at Aspose.Words.Tables.Table.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.Ꭸ(DocumentVisitor կ)
at Aspose.Words.Body.Accept(DocumentVisitor visitor)
at ♗.♖.♿(Section ①)
at ♗.♖.♽()
at ♗.♖.♻(♂ ♁)
at Aspose.Words.Document.፲(Stream ॣ, String ॡ, SaveFormat ፳)
at Aspose.Words.Document.Save(String fileName, SaveFormat fileFormat)
at CCDocumentos.clsAWord.TratarEmitirDocumento(String pstrCodDocum, String pstrCodExtpr, String pstrIDEXPEDI, String pstrREFERENCE, String pstrAnoExped, String pstrNumExped, String strConexionDB2, String strConexionSQL, String strOwnerDB2, String strOwnerSQL, String pstrCodEntid, String pstrCodFamil, String pstrCODTRAAD, Object pArrayDatos, String strRutaDocumento, String strRutaBloque, String strRutaEmision, String strCodDocCM, String strVersion, String strPlantDoc, String pIdentificador, Collection pcolVars, String pSoloEmitir, String pstrIdentificador, String pstrCodAplicacion, String pstrDataBaseName, String pstrNombreDocumento)

An additional info:
Our documents have DOCVARIABLES, tables and JPGs.

Hello,
We are having occasional errors in our component (CCDocumentos using Aspose.Words 9.1). When this error happens ALL the SAVE methods that we have in our source code fail. Then a 0Kb document is generated by Aspose.Words.
Our documents have DocVariables, tables, jpgs…
When we kill W3WP.exe process running Aspose.Words the errors dissappear.
What is happening? This kind of errors are very critical for us.

Error: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Ó".ਙ.BinarySearch(Int32[] array, Int32 index, Int32 length, Int32 value)
at Ó".਑.set_Item(Int32 key, Object value)
at ඞ.ᐳ.ᐷ(Int32 ਘ)
at ⚰.⨶.WriteCore()
at ⚰.⡏.ࣙ(ᐮ ഷ)
at â™—.â™–.âš„()
at â™—.â™–.VisitRowEnd(Row row)
at Aspose.Words.Tables.Row.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.Ꭸ(DocumentVisitor կ)
at Aspose.Words.Tables.Table.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.Ꭸ(DocumentVisitor կ)
at Aspose.Words.Body.Accept(DocumentVisitor visitor)
at ♗.♖.♿(Section ①)
at ♗.♖.♽()
at ♗.♖.♻(♂ ♁)
at Aspose.Words.Document.፲(Stream ॣ, String ॡ, SaveFormat ፳)
at Aspose.Words.Document.Save(String fileName, SaveFormat fileFormat)
at CCDocumentos.clsAWord.TratarEmitirDocumento(String pstrCodDocum, String pstrCodExtpr, String pstrIDEXPEDI, String pstrREFERENCE, String pstrAnoExped, String pstrNumExped, String strConexionDB2, String strConexionSQL, String strOwnerDB2, String strOwnerSQL, String pstrCodEntid, String pstrCodFamil, String pstrCODTRAAD, Object pArrayDatos, String strRutaDocumento, String strRutaBloque, String strRutaEmision, String strCodDocCM, String strVersion, String strPlantDoc, String pIdentificador, Collection pcolVars, String pSoloEmitir, String pstrIdentificador, String pstrCodAplicacion, String pstrDataBaseName, String pstrNombreDocumento)

Hi

Thanks for your request. Could you please reattach your document? The document you have attached earlier is zero-bytes file.
Best regards.

It is not a problem of a type of document. When there is a problem no document is generated. When we kill the process containing Aspose.Words, documents are generated without problems.

Can you change this post to public? Im going on holidays and other people of my company are going to read this post.

We have also this exception:

Error: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index
at Ó".਑.RemoveAt(Int32 index)
at ⚰.⨶.WriteCore()
at ⚰.⡏.ࣙ(ᐮ ഷ)
at â™—.â™–.âš„()
at â™—.â™–.VisitRowEnd(Row row)
at Aspose.Words.Tables.Row.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.Ꭸ(DocumentVisitor կ)
at Aspose.Words.Tables.Table.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.Ꭸ(DocumentVisitor կ)
at Aspose.Words.Body.Accept(DocumentVisitor visitor)
at ♗.♖.♿(Section ①)
at ♗.♖.♽()
at ♗.♖.♻(♂ ♁)
at Aspose.Words.Document.፲(Stream ॣ, String ॡ, SaveFormat ፳)
at Aspose.Words.Document.Save(String fileName, SaveFormat fileFormat)
at CCDocumentos.clsAWord.TratarAnexarFirmaDigital(String strFileDestDoc, String strPlantillaBloque, String Documento, Collection colCabecera, Collection colValores, String pstrComponer, String RutaFirma, Int32 pintNumFirma, String pstrCodEntid, String pstrCodFamil, Boolean pblnFirmado)

Hi

Thank you for additional information. I made this thread public. Could you please create a simple application, which will allow me to reproduce the problem on my side? I will investigate the issue and provide you more information.
Best regards,

We can’t reproduce the problem. It happens randomly.

Hi

Thank you for additional information. It is difficult to tell what the problem is without reproducing it. Maybe you can try to isolate the problem and provide us with a simple application or code example, which will allow us to analyze the problem.
Best regards,

I attach a base document, two document to insert, a image to be inserted and the code simplified treatment is failing us randomly.

We have three environments (development, testing and production) and only gave us this error in the production environment. The development server has a processor with two cores, the test server has a processor with one core and the production is a multiprocessor server (8 processors).

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const cDOCVARIABLE_FECHACAST As String = "FIRMA_FECHACAST"
Const cDOCVARIABLE_FECHAEUSK As String = "FIRMA_FECHAEUSK"
Dim objBuilder As DocumentBuilder
Dim objBookmark As Bookmark
Dim cont As Integer
Dim Buscar As DictionaryEntry
Dim blnExiste As Boolean
Dim strNUMFirma As String
Dim strFechaCast As String
Dim strFechaEusk As String
Dim objDocDest As Document
Dim shapesDocumentos As NodeCollection
Dim Documento As String = "C:\PROYECTOS\PruebaASPOSE\ErrorSave\O028.doc"
Dim objWordDocumento As Document
Dim strFileDestDoc As String = "C:\PROYECTOS\PruebaASPOSE\ErrorSave\FIR030_2.DOC"
Dim RutaFirma As String = "C:\PROYECTOS\PruebaASPOSE\ErrorSave\S10035PA.JPG"
Try
InsertarBloqueMultiplesFirmas("C:\PROYECTOS\PruebaASPOSE\ErrorSave", "FIR000", _
Documento)
strNUMFirma = "FIRMA1"
objWordDocumento = New Aspose.Words.Document(Documento)
fProtegerDocumento(objWordDocumento, False)
objBookmark = objWordDocumento.Range.Bookmarks(strNUMFirma)
objWordDocumento.Variables.Add("CARGCAST", Nothing)
objWordDocumento.Variables.Add("CARGEUSK", Nothing)
objWordDocumento.Variables.Add("DELSUSTC", Nothing)
objWordDocumento.Variables.Add("DELSUSTE", Nothing)
objWordDocumento.Variables.Add("SERVICIOCAST", Nothing)
objWordDocumento.Variables.Add("SERVICIOEUSK", Nothing)
objWordDocumento.Variables.Add("FIRMA_FECHACAST", "Bilbao, a 27 de Julio de 2010")
objWordDocumento.Variables.Add("FIRMA_FECHAEUSK", "Bilbon, 2010(e)ko Uztailaren 27(e)an")
objDocDest = New Document(strFileDestDoc)
InsertarDocumento(strNUMFirma, objWordDocumento, objDocDest)
ActualizarMarcadores(objWordDocumento.Range.Bookmarks)
objBuilder = New DocumentBuilder(objWordDocumento)
If Not objBuilder.MoveToBookmark("ImagenFirma1") Then
Throw New Exception("En el bloque " & Documento & " no existe el marcador 'ImagenFirma1'")
End If
If Trim(RutaFirma) <> "" Then
objBuilder.InsertImage(RutaFirma)
Else
objBuilder.InsertParagraph()
objBuilder.InsertParagraph()
objBuilder.InsertParagraph()
objBuilder.InsertParagraph()
End If
objWordDocumento.Range.Bookmarks.Remove("ImagenFirma1")
shapesDocumentos = objWordDocumento.GetChildNodes(NodeType.Shape, True, False)
For Each shape As Shape In shapesDocumentos
If InStr(shape.GetText, "FECACUSE") > 0 Then
shape.Remove()
End If
Next shape
fProtegerDocumento(objWordDocumento, True)
objWordDocumento.Save(Documento, SaveFormat.Doc)
Catch Ex As Exception
Dim strError As String
strError = vbTab & "Error: " & Ex.Message
logErrores(strError)
Throw New Exception(Ex.Message)
Finally
objWordDocumento = Nothing
objBuilder = Nothing
objBookmark = Nothing
Buscar = Nothing
objDocDest = Nothing
shapesDocumentos = Nothing
End Try
End Sub
Friend Sub ActualizarMarcadores(ByVal bkMarcadores As BookmarkCollection)
Try
For Each bkMarcadorActual As Bookmark In bkMarcadores
If bkMarcadorActual.Name.IndexOf("ASPOSE") > -1 Then
ActualizarContenidoMarcador(bkMarcadorActual)
End If
Next
Catch ex As Exception
Dim strError As String
strError = vbTab & "Error: " & ex.Message
logErrores(strError)
Finally
End Try
End Sub
Friend Sub ActualizarContenidoMarcador(ByVal bkMarcador As Bookmark)
Dim bookmarkStart As BookmarkStart = bkMarcador.BookmarkStart 
Dim bookmarkEnd As BookmarkEnd = bkMarcador.BookmarkEnd
Dim nodo As Node = bookmarkStart
Dim doc As Document = nodo.Document
Try
Do While Not nodo Is bookmarkEnd
If nodo.NodeType = NodeType.Paragraph Then
Try
nodo.Range.UpdateFields()
Catch ExUpdate As Exception
'Si el nodo contiene un campo "IF" se produce un error.
'Se sigue comprobando el resto de nodos.
End Try
End If
nodo = nodo.NextPreOrder(doc)
Loop
Catch ex As Exception
Dim strError As String
strError = vbTab & "Error: " & ex.Message
logErrores(strError)
Finally
End Try
End Sub
Friend Sub logErrores(ByVal cadena As String, _
Optional ByVal strInfoAdicional As String = "")
Try
Dim strError As String
Dim strRutaFichero As String = "C:\PROYECTOS\PruebaASPOSE\ErrorSave\Log_" & Format(Today).Replace("/", "") & ".txt"
Dim strFichero As New System.IO.StreamWriter(strRutaFichero, True)
strError = CStr(Now) & vbCrLf & CStr(cadena)
If strInfoAdicional <> "" Then strError &= vbTab & "Informacion Adicional : " & strInfoAdicional
strFichero.WriteLine(strError & vbCrLf)
strFichero.Close()
Catch ex As Exception
Throw New Exception("Error en la Gestión de Logs : " & ex.Message.ToString)
End Try
End Sub
Friend Sub InsertarDocumento(ByVal strMarcador As String, ByVal dstDoc As Document, ByVal srcDoc As Document)
Dim objBuilder As DocumentBuilder
Dim insertAfterNode As Node
Dim dstStory As CompositeNode
Dim srcSection As Section
Try
objBuilder = New DocumentBuilder(dstDoc)
objBuilder.MoveToBookmark(strMarcador)
objBuilder.Writeln()
insertAfterNode = objBuilder.CurrentParagraph.PreviousSibling
dstStory = insertAfterNode.ParentNode
While (Not srcDoc.LastSection.Body.LastParagraph.HasChildNodes)
srcDoc.LastSection.Body.LastParagraph.Remove()
End While
For Each srcSection In srcDoc.Sections
Dim srcNode As Node
For Each srcNode In srcSection.Body
Dim newNode As Node = dstDoc.ImportNode(srcNode, True, ImportFormatMode.KeepSourceFormatting)
dstStory.InsertAfter(newNode, insertAfterNode)
insertAfterNode = newNode
Next
Next
Catch ex As Exception
Dim strError As String
strError = vbTab & "Error: " & ex.Message
logErrores(strError)
Throw New Exception(ex.Message)
Finally
objBuilder = Nothing
insertAfterNode = Nothing
dstStory = Nothing
srcSection = Nothing
End Try
End Sub
Friend Sub InsertarBloqueMultiplesFirmas(ByVal pstrRutaBloque As String, _
ByVal pstrBloque As String, _
ByVal pstrDocumento As String)
Dim objWordDocumento As Document
Dim strBloque As String
Dim strMarcaFirma As String
Dim objDocDest As Document
Try
strBloque = pstrRutaBloque & "\" & pstrBloque & ".doc"
strMarcaFirma = "FIRMA"
objWordDocumento = New Document(pstrDocumento)
fProtegerDocumento(objWordDocumento, False)
objDocDest = New Document(strBloque)
InsertarDocumento(objWordDocumento.Range.Bookmarks(strMarcaFirma).BookmarkStart.ParentNode, objDocDest)
fProtegerDocumento(objWordDocumento, True)
objWordDocumento.Save(pstrDocumento)
Catch ex As Exception
Dim strError As String
strError = vbTab & "Error: " & ex.Message
logErrores(strError)
Throw New Exception(ex.Message)
Finally
objWordDocumento = Nothing
objDocDest = Nothing
End Try
End Sub
Friend Sub InsertarDocumento(ByVal insertAfterNode As Node, _
ByVal srcDoc As Document)
Dim dstStory As CompositeNode
Dim importer As NodeImporter
Dim para As Paragraph
Dim newNode As Node
Try
If Not ((insertAfterNode.NodeType = NodeType.Paragraph) OrElse (insertAfterNode.NodeType = NodeType.Table)) Then
Throw New ArgumentException("El nodo destino debe ser de tipo paragraph o table.")
End If
dstStory = insertAfterNode.ParentNode
importer = New NodeImporter(srcDoc, insertAfterNode.Document, _
ImportFormatMode.KeepSourceFormatting)
For Each srcSection As Section In srcDoc.Sections
For Each srcNode As Node In srcSection.Body
If (Not para Is Nothing) AndAlso para.IsEndOfSection AndAlso (Not para.HasChildNodes) Then
Exit For
End If
newNode = importer.ImportNode(srcNode, True)
dstStory.InsertAfter(newNode, insertAfterNode)
insertAfterNode = newNode
Next srcNode
Next srcSection
Catch ex As Exception
Dim strError As String
strError = vbTab & "Error: " & ex.Message
logErrores(strError)
Throw New Exception(ex.Message)
Finally
End Try
End Sub
Friend Sub fProtegerDocumento(ByVal objDocProteger As Aspose.Words.Document, _
ByVal blnProteger As Boolean, _
Optional ByVal pblnPorSecciones As Boolean = False)
Dim strClave As String
Dim strSqlCon As String
Dim rsdClave As IDataReader
Dim blnCerrarConexion As Boolean
Try
strClave = "PRUEBA"
objDocProteger.AcceptAllRevisions()
objDocProteger.TrackRevisions = False
objDocProteger.RemoveSmartTags()
If blnProteger = True Then
If pblnPorSecciones Then
objDocProteger.Protect(Aspose.Words.ProtectionType.AllowOnlyFormFields, strClave)
Else
objDocProteger.Protect(Aspose.Words.ProtectionType.ReadOnly, strClave)
End If
Else
objDocProteger.Unprotect()
End If
Catch Ex As Exception
Dim strError As String
strError = vbTab & "Error: " & Ex.Message
logErrores(strError)
Throw New Exception(Ex.Message)
Finally
If Not rsdClave Is Nothing Then
rsdClave.Close()
rsdClave.Dispose()
End If
rsdClave = Nothing
If Not objDocProteger Is Nothing Then
objDocProteger = Nothing
End If
End Try
End Sub

Hi

Thank you for additional information. But unfortunately, I cannot reproduce the problem on my side. I tried running your code in a loop and in multiple threads and no exceptions occur. Also, please try using the latest version of Aspose.Words (9.2.0).
Best regards,

Hi,
Im very interested in knowing what’s exactly the problem asociated to this issue:
17259 Bug DOC ArrayIndexOutOfBoundsException occurs during saving to DOC
We had a problem when saving Documents in Aspose 9.1 and the error was a System.IndexOutOfrangeException as you can see in the thread.
Is it related to the issue 17259?

Hello

Thanks for your inquiry. The issue #17259 has been fixed in the latest version of Aspose.Words (9.3.0). You can download this version from here:
https://releases.aspose.com/words/net
Best regards,

Yes, I know that issue #17259 has been fixed. I need to know what was exactly the problem in that issue because as you can see in this thread we had a very serious problem while saving a document. You can see the exception and the ASPOSE.Words method that raised the exception in this thread.
I only can update Aspose.Words in the real enviroment if I am sure that our problem has been solved so I need to know if it’s related to issue 17259

Hello

Thank you for additional information. I tested your code on my side but was unable to reproduce the problem. You can easily check how it works on your side with the latest version of Aspose.Words.
Best regards,

Hi,
I only need more info about issue #17259 in order to know if its related to my problem:

  • Detailed info about problem detected in issue 17259
  • Method raising the exception detected in issue 17259
  • Cause of the error detected in issue 17259

I only can get permission to upgrade Aspose.Words in my company if I can know that my problem is the same of issue 17259 so PLEASE I only need more info about issue 17259. I know that you can not reproduce the problem. I also can not because it was a random problem that only happens in Real Environment not in our Test Environment.

Hello
This issue was in out code, issue is not in the document. We had some problem with internal variables. I cannot say it’s relate to your problem or not, because I cannot reproduce it on my side.
Best regards,

Hi,
Do you know if your problem with internal variables were related to these methods (in red):

Error: System.ArgumentOutOfRangeException: **Specified argument was out of the range of valid values.** Parameter name: index 
at Ó".਑.**RemoveAt**(Int32 index) 
at **⚰.⨶.WriteCore()** 
at ⚰.⡏.ࣙ(ᐮ ഷ) 
at â™—.â™–.âš„() 
at â™—.â™–.VisitRowEnd(Row row) 
at Aspose.Words.Tables.Row.Accept(DocumentVisitor visitor) 
at Aspose.Words.CompositeNode.Ꭸ(DocumentVisitor կ) 
at Aspose.Words.Tables.Table.Accept(DocumentVisitor visitor) 
at Aspose.Words.CompositeNode.Ꭸ(DocumentVisitor կ) 
at Aspose.Words.Body.Accept(DocumentVisitor visitor) 
at ♗.♖.♿(Section ①) 
at ♗.♖.♽() at ♗.♖.♻(♂ ♁) 
at Aspose.Words.Document.፲(Stream ॣ, String ॡ, SaveFormat ፳) 
at **Aspose.Words.Document.Save**(String fileName, SaveFormat fileFormat) 
Error: System.IndexOutOfRangeException: Index **was outside the bounds of the array.** 
at Ó".ਙ.**BinarySearch**(Int32[] array, Int32 index, Int32 length, Int32 value) 
at Ó".਑.**set_Item**(Int32 key, Object value) 
at ඞ.ᐳ.ᐷ(Int32 ਘ) 
at ⚰.⨶.WriteCore() 
at ⚰.⡏.ࣙ(ᐮ ഷ) 
at â™—.â™–.âš„() at â™—.â™–.VisitRowEnd(Row row) 
at Aspose.Words.Tables.Row.Accept(DocumentVisitor visitor) 
at Aspose.Words.CompositeNode.Ꭸ(DocumentVisitor կ) 
at Aspose.Words.Tables.Table.Accept(DocumentVisitor visitor) 
at Aspose.Words.CompositeNode.Ꭸ(DocumentVisitor կ) 
at Aspose.Words.Body.Accept(DocumentVisitor visitor) 
at ♗.♖.♿(Section ①) 
at ♗.♖.♽() 
at ♗.♖.♻(♂ ♁) 
at Aspose.Words.Document.፲(Stream ॣ, String ॡ, SaveFormat ፳) 
at **Aspose.Words.Document.Save**(String fileName, SaveFormat fileFormat)

Hi
Thanks for your inquiry. This exception was occurring during saving to DOC format only. Could you please try saving to any other format and investigate this situation. If the problem will not occur, it can be issue # 17259.
Best regards,