We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Adding form fields into new table rows

Hello,



I am trying to add new rows with form fields into a table, but it ends with “Stack empty” error.



I use Aspose.Words for .Net 13.3.0.0.



Thanks for help.



Best regards



Libor

Hi Libor,


Thanks for your inquiry. After an initial test with Aspose.Words for .NET 15.10.0 and using your code, we were unable to reproduce this exception on our end. We would suggest you please upgrade to the latest version of Aspose.Words. Hope, this helps.

Secondly, could you please attach your expected Word document here for our reference. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document using Microsoft Word. We will then provide you code to achieve the same using Aspose.Words.

Best regards,

Hello,



I have tested it with the newest version 15.10.0, it does not throw the error, but it does not create the form fields either.



I am attaching the project, could you observe it, please?



Thanks



Libor

Hi Libor,


Thanks for your inquiry. Could you please attach your “expected Word document” here for our reference. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document using Microsoft Word. We will then provide you code to achieve the same using Aspose.Words.

PS: please find attached “out.docx” which was generated from “Document1.docx” using the latest code you provided.

Best regards,

I am adding the desires result.


Basically I nees to add form field dynamically into new rows of a table.

Best regarsa

Libor

Hi Libor,


Thanks for the additional information. Please use the following code to get expected output:

Dim Word1 As New Document(MyDir & “Document1.docx”)<o:p></o:p>

Word1.Range.FormFields("txtOrderNr").Result = "123"

'I want to add new row(s) into the teble with the items

Dim TableWithItems As Aspose.Words.Tables.Table = DirectCast(Word1.GetChild(Aspose.Words.NodeType.Table, 0, True), Aspose.Words.Tables.Table)

Dim DocumentBuilder1 As New Aspose.Words.DocumentBuilder(Word1)

Dim intCountOfInsertedRows As Integer

'Now I want to add 10 rows and form fields in each cell, in Aspose.Words 15.10.0.0 it does not throw the error, but doesn't create any fields anyway

For intInsertedRow As Integer = 1 To 10

Dim NewTableRow As New Aspose.Words.Tables.Row(Word1)

For intCellNumber As Integer = 1 To 4

'the table row will have four cells, each will have a paragraph with a form field

'creation of a new cell and insertion of the paragraph with the form field into the cell

Dim Cell1 As New Aspose.Words.Tables.Cell(Word1)

'creation of a new paragraph and its insertion into the cell

Dim Paragraph1 As New Aspose.Words.Paragraph(Word1)

Cell1.AppendChild(Paragraph1)

' move cursor to inside cell

DocumentBuilder1.MoveTo(Cell1.FirstParagraph)

'creation of a form field for each cell

Dim FormField1 As Aspose.Words.Fields.FormField

FormField1 = DocumentBuilder1.InsertTextInput("txtNewField" & intInsertedRow & intCellNumber, Aspose.Words.Fields.TextFormFieldType.Regular, "", "Hello, I am the new field!", 0)

'insertion of the cell to the table row

NewTableRow.Cells.Add(Cell1)

Next

'insertion of the newly created row into the table behind the previously created row

TableWithItems.Rows.Insert(1 + intCountOfInsertedRows, NewTableRow)

intCountOfInsertedRows += 1

Next

'locking the form

Word1.Protect(Aspose.Words.ProtectionType.AllowOnlyFormFields)


Word1.Save(MyDir &
“out.docx”)


Hope, this helps.

Best regards,

Hi,


thanks for help. That works. But unfortunately not in my version 13.3.0.0.

It crashes on the line…

DocumentBuilder1.MoveTo(Cell1.FirstParagraph)

Object reference not set to an intance of an object.

When I change the reference to ver. 15.10.0.0 it suddenly works.

Do you think there is a way how to make it works without an upgrade?

Best regards

Libor

Hi Libor,


Thanks for your inquiry. Please note that we don’t provide support for older released versions of Aspose.Words and we do not provide any fixes or patches for old versions of Aspose products either. We always encourage our customers to use the latest release versions of Aspose.Words (to date it’s 15.10.0) as it contains newly introduced features, enhancements and fixes for issues reported earlier. If we can help you with anything else, please feel free to ask.

Best regards,

Dear Hafeez,

I understand, thank you for you kind help anyway.

Best regards

Libor