Text form fields in grey are not rendered with background color

When the word document contains test form fields, both blank fields (in grey) and filled fields (in grey) are not rendered with grey background.

MS Word render output

Aspose output

Please note that i’m using doc.renderToSize to get the output and would request you to assist/help in the same. PdfSafeOptions or ImageSaveOptions would not help in our requirement as it would be a two step process and hampers performance

@maheibm It looks like you are talking about MS Word legacy form fields. Most likely they do not actually have highlighting, they are highlighted by the option in MS Word:

You can preserve these form fields as form fields upon exporting document to PDF by specifying PdfSaveOptions.PreserveFormFields option.

Hi @alexey.noskov,
Thanks for the response. As mentioned in the reported description, PdfSaveOptions would be a two step process as we rely on the renderToSize to get a graphics output. Any ways to handle this in the com.aspose.words.Document available apis?

@maheibm You can explicitly highlight form fields before exporting the document to image. For example see the following code:

Document doc = new Document("C:\\Temp\\in.docx");

for(Field f : doc.getRange().getFields())
{
    if(f.getType() == FieldType.FIELD_FORM_TEXT_INPUT)
    {
        Node currentNode = f.getStart();
        while (currentNode!=null && currentNode!=f.getEnd())
        {
            if(currentNode instanceof Inline)
                ((Inline)currentNode).getFont().setHighlightColor(Color.GRAY);
            currentNode = currentNode.getNextSibling();
        }
    }
}
        
doc.save("C:\\Temp\\out.png");

@alexey.noskov, thanks again for the prompt response. I did try the given snippet but it did not work. No changes in output. I did save to png too and tried setting different color, but none worked.

@maheibm Could you please attach your input document here for testing? Unfortunately, using screenshots we can only guess what types of form fields are used in your document.

@alexey.noskov Please find the sample document below
sample.docx (15.3 KB)

@maheibm The code provided above works fine. But your document except of text input also contains dropdown form fields. Please modify the code like this:

Document doc = new Document("C:\\Temp\\in.docx");
    
for(Field f : doc.getRange().getFields())
{
    if(f.getType() == FieldType.FIELD_FORM_TEXT_INPUT ||
            f.getType() == FieldType.FIELD_FORM_DROP_DOWN ||
            f.getType() == FieldType.FIELD_FORM_CHECK_BOX)
    {
        Node currentNode = f.getStart();
        while (currentNode!=null && currentNode!=f.getEnd())
        {
            if(currentNode instanceof Inline)
                ((Inline)currentNode).getFont().setHighlightColor(Color.LIGHT_GRAY);
            currentNode = currentNode.getNextSibling();
        }
    }
}
    
doc.save("C:\\Temp\\out.png");

Here is the produced output: out.png (33.9 KB)

@alexey.noskov thanks for your help on it. I now see it better but still few areas are the same and arent grey yet. I’m using the same snippet as you’ve suggested. Also noticed your output file, not sure whats going on. I’m on 23.5 aspose words since latest versions have error running in J9. Is that the reason or something else?

@maheibm Unfortunately, I cannot reproduce the problem with 23.5 version. The output produced by 23.5 version of Aspose.Words is the same: out.png (33.9 KB)