From field loses Bold

Hallo,

I found a strange behaviour. When I create a form with several form fields, some of them I mark and set Bold. When I try to write over these bold fields it shows bold. When I set a default text on the bold fields it shows bold. But when I fill them in programatically (Word1.Range.FormFields(“Text1”).Result = “something”) then save it (Word1.Save(strFileName, Aspose.Words.SaveFormat.Doc, Aspose.Words.SaveType.OpenInWord, Response)) it shows some of the bold fields which were set bold NOT bold!

It’s also interesting that after I fill in the field by the mentioned command, I also check the .font.bold property and it’s set True, however when the Word shows it is’s not bold.

I use Aspose.Words 4.1.1.0, MS Word 2003 SP2 Czech Version.

Can you help me?

Thanks a lot

Libor

Hi Libor,

Please read the following thread:

https://forum.aspose.com/t/123211

It seems to contain a similar issue and a workaround code. If the thread is not helpful enough, please let me know.

Hallo,

thanks for information. The trick to keep the formating of the form field is to set its’ surrounding to the same formatting (where possible). How long could it take to fix this? I must go live with many forms on Wednesday, is there a change you will fix this by this time?

Thank you a lot

Libor

Libor,

It’s not likely we will fix it until Wednesday. So is the workaround suggested in that thread applicable in your situation or we should try to find out something else?

Hallo,

thanks for response. I’ll try to fix it somehow. But this seems to be a big problem.

Best regards

Libor

Hallo,

I tried to fix it by setting the surrounding text to Bold. But the forms are in table and many fields start as the first thing in the table cell. So no surrounding text is possible. Moreover even when I add a blank space or more spaces before such field and format it to Bold, it never displays these spaces and does NOT turn the form field to bold. Only when I put a bold character (e.g. X) before the form field, then the form field is bold. So in a teble cell there must always be a bold character before the form field to see the form field bold. Unfortunately this for the first form field at the beginning of the cell is not applicable. And this is a very bad situation for me when I know that I must deliver my software this week.

Best regards

Libor

Please check once again the thread

Try using the SetFormFieldText method given there instead of setting FormField.Result. The text inserted using this method should have the correct formatting. Please let me know if you have any troubles using this method.

Best regards,

Hallo, could I ask you to send me the SetFormFieldText in VB? It’s not much readable for me in C#.

Thanks

Libor

Here it is:

Private Sub SetFormFieldText(ByVal formField As FormField, ByVal text As String)
Dim node As Node = formField
Do While Not node Is Nothing AndAlso node.NodeType <> NodeType.FieldSeparator
node = node.NextSibling
Loop
node = node.NextSibling
If TypeOf node Is Run Then
CType(node, Run).Text = text
End If
node = node.NextSibling
Do While Not node Is Nothing AndAlso node.NodeType <> NodeType.FieldEnd
If TypeOf node Is Run Then
node.Remove()
End If
node = node.NextSibling
Loop
End Sub

BTW, I can recommend a nice online resource for conversion of C# code snippets to VB.NET:
http://kamalpatel.net/ConvertCSharp2VB.aspx

Best regards,

Hallo,

thank you for the code in VB. However there is a problem. The form field is empty now despite a text should be there. A good think this field is bold now, but it’s empty.

When the method reaches this…

If TypeOf node Is Run Then
CType(node, Run).Text = text
End If

Then the type of node is SpecialChar (20) and it never insets the text.

Thanks for help

Libor

Yes, that seems to complicate things a bit. You see, when the form field text is not initialized with some text it contains five SpecialChar nodes instead of a Run node. I have not taken it into account before. I will modify the code now, but please attach one of your documents here so that I could test the modified code against your document.

Best regards,

Hallo,

here is the file.

Libor

Thanks. I have put the updated code to the end of the following article:

https://docs.aspose.com/words/net/working-with-form-fields/

The c# code has been tested against your document. The VB.NET code was made using the conversion tool and not tested. So please let me know if it works correctly.

Best regards,

Hallo,

GREAT! thanks a lot. Not it works fine.

Libor

Hallo,

I now use your SetFormFieldText function to fill-in form fields. This works great except one case. When the value is string.empty. Then the form gets mad and all text after the field with string.empty turns into a big form field.

Also when I use SetFormFieldText which accepts a value parametr as Object and I put into this parameter e.g. decimal, than it reports an error. I must convert everything to string first with CStr function in order to use decimals etc. with this function.

Best regads

Libor

Hi,

Thank you for reporting this to us, we will try to make necessary amendments shortly.

Thanks for reporting this problem to us. I have updated the code in the wiki article to handle cases with null objects (Nothing in VB) and empty string.

Concerning passing decimal types. Decimal as well as other numebr types will be correctly accepted only if the form field text input type is number. So you should check the FormField.TextInputType before passing the object to set form field text. For example,

switch (formField.TextInputType)
{
    case TextFormFieldType.DateText:
        // Pass DateTime to SetFormFieldText.
        SetFormFieldText(formField, DateTime.Now);
        break;
    case TextFormFieldType.NumberText:
        // Pass number to SetFormFieldText.
        SetFormFieldText(formField, 123456789);
        break;
    case TextFormFieldType.RegularText:
        // Pass string to SetFormFieldText.
        SetFormFieldText(formField, "Text");
        break;
}

The text input type of the text form field can be set in form field’s context menu Properties | TextFormFieldOptions | TextFormField | Type in MS Word.

Hope this helps,