Our application needs to generate word forms programmatically. To do this we have a mail merge template document (template) and use the Aspose.Words.DocumentBuilder to add form fields to the template.
Document template = new Document(“TestExample.doc”);
DocumentBuilder builder = new DocumentBuilder(template);
We use the DocumentBuilder to move to various mail merge fields and insert form fields (checkboxes and text fields) using the InsertCheckBox and InsertTextInput functions.
builder.InsertTextInput(“test”, TextFormFieldType.RegularText, string.Empty, string.Empty, 0);
Once all the fields have been added and the document has been formatted we then tidy up the mail merge fields and protect the document before saving to a new location.
template.MailMerge.DeleteFields();
template.Protect(ProtectionType.AllowOnlyFormFields);
When a user receives the document and populates the fields, our application can read the data back in by parsing through the Document.Range.FormFields collection:
foreach(FormField field in doc.Range.FormFields)
{
// read contents of field.result
}
This works OK except when the contents of the text field has a leading space. In this case, the field.result is always an empty string.
As an experiment and to allow us to examine the generated document I removed the line protecting the document:
// template.Protect(ProtectionType.AllowOnlyFormFields);
Now we re-generate the document and open it in Word. Everything looks OK so we use the Protect Form button to protect the document and allow us to fill in the form fields. This time when we read the form into the application it reads the contents of the text field even though it has a leading space.
Can you shed any light on this for us.
Many thanks.
PS: We are using Aspose.Words version 3.5.1
Thanks for reporting this problem to us,
I will research the issue in 1-2 days and let you know. Meanwhile, if you can provide any sample documents illustrating the problem, that would be helpful, too.
Best regards,
When text input form field is created with empty field value it is actually field with 5 special characters, which look like a small 'o' signs if you turn formatting marks on in MS Word.
Currently Aspose.Words analyzes text field content and if it has any leading 'o' signs then the field is considered not filled and empty string is returned by FormField.Result.
Maybe we should change this behavior and check the entire text input field for normal text characters and if they are found, then return the text input contents substituting 'o' signs with spaces. I have logged this suggestion to our defect base as issue #1022. We will consider including it in our next hotfix which will be released in 2 weeks.
Meanwhile, as a workaround you can insert text field with the non-empty value, like " " for example:
builder.InsertTextInput("TextInput", TextFormFieldType.RegularText, "", " ", 0);
Best regards,
Thanks for the prompt reply.
I’ve introduced the workaround which should be sufficient for our current development but I think it would be beneficial if you were able to introduce the solution as you have suggested.
Thanks again for your help, and for an excellent product.
We have fixed this in Aspose.Words 3.7.
Best regards,
Does the java version of aspose words contain this fix and if so which version?
Thanks