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

Free Support Forum - aspose.com

Highlighting Form Field


I am trying to highlight form fields in my Word document and it's not working. Below is my code snipets. Is there anything else I need to do?

field(DocumentFieldName).Font.Shading.ForegroundPatternColor = System.Drawing.Color.Yellow

field(DocumentFieldName).Font.Shading.Texture = TextureIndex.TextureSolid

Basically, our users want the form field to be highlighted yellow if data does not exist. I tried highlight and background property and still did not work. If I used the document builder object then it works fine, however, I need form field hightlighted, not text. Can you help? Thanks.

You currently cannot use FormField.Font properties to do highlighting. I have logged it as a defect. It will be corrected in one of our future releases.

FormField is just one of the nodes of the complete form field. A complete form field consists of: FieldStart, BookmarkStart, Run (field code), FormField, FieldSeparator, Run (field result), BookmarkEnd, FieldEnd.

To set color of text in a form field you need to set that color on all runs from the FieldSeparator of the form field to the FieldEnd.

You can get the field separator using FormField.NextSibling, then keep going through NextSibling until you reach FieldEnd. Everything in between, if its a Run, set its font. If its a BookmarkEnd, ignore it.

Try the following approach:

FormField ff = doc.Range.FormFields["Text1"];
Node node = ff;
while(node.NodeType != NodeType.FieldStart)
node = node.PreviousSibling;
if(node is Inline)
Aspose.Words.Font font = ((Inline)node).Font;
font.Color = Color.Orange;
font.HighlightColor = Color.Blue;
node = node.NextSibling;

I can only add that setting shading does not work because MS Word uses the gray background for form fields anyway on top of the shading. You can easily try that in MS Word itself, select a form field and set shading - you will still see a normal form fields. Therefore you need to use highlighting like shown above.

We admit that handling FormField formatting is a non-trivial task with the current API. But we plan to improve it eventually.

I have logged it in our defect base as Issue #830.

The issues you have found earlier (filed as WORDSNET-522) have been fixed in this Aspose.Words for .NET 20.9 update and this Aspose.Words for Java 20.9 update.