Inserting

Dear Sir
I create a table in my document and insert TextFormField in it .(code below)

builder.MoveToSection(m_document.IndexOf(objsection))
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left
builder.RowFormat.Bidi = True
builder.Font.Bidi = True
builder.Font.LocaleIdBi = 1025
builder.StartTable()
builder.InsertCell()
builder.InsertCell()
builder.EndRow()
builder.InsertCell()
builder.InsertCell()
builder.EndRow()
builder.EndTable()
tblTo = objsection.Body.Tables(0)
tblTo.Rows(0).RowFormat.Alignment = RowAlignment.Right
tblTo.Rows(1).RowFormat.Alignment = RowAlignment.Right
tblTo.Rows(0).RowFormat.AllowAutoFit = True
tblTo.Rows(0).RowFormat.HeightRule = HeightRule.Exactly
tblTo.Rows(0).RowFormat.Height = 1
builder.MoveToCell(m_document.IndexOf(tblTo), 0, 0, 0)
newField = builder.InsertTextInput("To", Fields.TextFormFieldType.RegularText, "", "", 0)
newField.TextInputType = Fields.TextFormFieldType.RegularText
newField.Result = "T"
newField.Name = "To"
newField.Font.NameBi = PrpFontName
newField.Font.Name = PrpFontName
newField.Font.Size = PrpFontSize
newField.Font.SizeBi = PrpFontSize
newField.Enabled = False
newField.Font.Hidden = True
objCell = tblTo.Rows(0).Cells(0)
para = New Paragraph(m_document)
para.AppendChild(newField)
objCell.AppendChild(para)
tblTo.Rows(1).Cells(0).CellFormat.WrapText = False
builder.MoveToCell(m_document.IndexOf(objsection.Body.Tables(0)), 1, 1, 0)
newField = builder.InsertTextInput("txtTo", Fields.TextFormFieldType.RegularText, "", "", 0)
newField.TextInputType = Fields.TextFormFieldType.RegularText
newField.Name = "txtTo"
newField.Result = " "
newField.Font.NameBi = PrpFontName
newField.Font.Name = PrpFontName
newField.Font.Size = PrpFontSize
newField.Font.SizeBi = PrpFontSize
newField.Enabled = True
objCell = tblTo.Rows(1).Cells(1)
para = New Paragraph(m_document)
para.AppendChild(newField)
objCell.AppendChild(para)
m_Document.Save("c:\out.doc")

But when I want to opent it next time I encounter this errore message :
“The document appears to be corrupted and cannot be loaded.”
Please tell me why this happen.
Thanks
Mehdi Mokhtari

Hi
Thanks for your request. I managed to reproduce this problem. I created new issue # 5321 in our defect database. I will notify you as soon as it is fixed.
Here is code that allows me to reproduce this issue.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert cell
Cell objCell = builder.InsertCell();
// Insert formfield into the cell
FormField newField = builder.InsertTextInput("To", Aspose.Words.Fields.TextFormFieldType.RegularText, "", " ", 0);
// I think that the following 3 lines are the reason of problem
Paragraph para = new Paragraph(doc);
para.AppendChild(newField);
objCell.AppendChild(para);
// End row
builder.EndRow();
builder.EndTable();
// save document
doc.Save("out.doc");
Document doc1 = new Document("out.doc"); //Error occurs here.
I think that you can use the following code. This code do the same but doesn’t cause the exception.
'Move DocumentBuilder cursor to the specified section
builder.MoveToSection(m_document.IndexOf(objsection))
'Set paragraph alignment
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left
'Set RowFormat
builder.RowFormat.Bidi = True
builder.RowFormat.Alignment = RowAlignment.Right
builder.RowFormat.AllowAutoFit = True
builder.RowFormat.HeightRule = HeightRule.Exactly
builder.RowFormat.Height = 20
'Set width of cells
builder.CellFormat.Width = 100
'Set font formating
builder.Font.Bidi = True
builder.Font.LocaleIdBi = 1025
'Start table
builder.StartTable()
'Insert cell
builder.InsertCell()
'Insert formafield into the cell
newField = builder.InsertTextInput("To", Fields.TextFormFieldType.RegularText, "", " ", 0)
'Configure formfield 
newField.Font.NameBi = PrpFontName
newField.Font.Name = PrpFontName
newField.Font.Size = PrpFontSize
newField.Font.SizeBi = PrpFontSize
newField.Enabled = False
newField.Font.Hidden = True
'insert one more cell
builder.InsertCell()
'End row
builder.EndRow()
'Insert first cell of the second row
builder.InsertCell()
'Insert one more cell 
builder.InsertCell()
'Insert formfield
builder.MoveToCell(m_document.IndexOf(objsection.Body.Tables(0)), 1, 1, 0)
newField = builder.InsertTextInput("txtTo", Fields.TextFormFieldType.RegularText, "", " ", 0)
newField.Font.NameBi = PrpFontName
newField.Font.Name = PrpFontName
newField.Font.Size = PrpFontSize
newField.Font.SizeBi = PrpFontSize
newField.Enabled = True
'End row and table
builder.EndRow()
builder.EndTable()
'save document
m_document.Save("out.doc")

Hope this helps
Best regards.

Also, you can replace creation of a new Paragraph with calling DocumentBuilder.Writeln(), which is essentially a shortcut for creating new paragraphs.
In the meantime I will fix what’s wrong with creating and appending a new paragraph in this example.

The problem is caused by the highlighted line:
newField = builder.InsertTextInput(“To”, Fields.TextFormFieldType.RegularText, “”, “”, 0)

para.AppendChild(newField)
The thing is that the form field is already inserted into the document and it actually consists of more than just a FormField node. It consists if several other nodes field start, field code, separator, result, end and bookmark nodes. By calling AppendChild you essentially move just the FormField node into another paragraph and that makes the whole form field invalid. Just don’t do AppendChild in this case.
This is from the documentation:
A complete form field in a Word document is a complex structure represented by several nodes: field start, field code such as FORMTEXT, form field data, field separator, field result, field end and a bookmark. To programmatically create form fields in a Word document use DocumentBuilder.InsertCheckBox, DocumentBuilder.InsertTextInput and DocumentBuilder.InsertComboBox which make sure all of the form field nodes are created in a correct order and in a suitable state.
At this stage I don’t see anything I can fix and I’m closing the defect.