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

Free Support Forum - aspose.com

Background and foreground color for document

if I want to set background and foreground color for document

such that foreground color applies to dividing lines and headers of table

Is it possible to do that? Without setting explicitly for each objects like dividing line, header of table, etc

background color applies only to page of document foreground color applies to any other objects like header row of table

objects which are added on page

This message was posted using Email2Forum by Tahir Manzoor.

Hi Abhishek,

Thanks for your inquiry. Please manually create your expected output Word document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.

Hi Tahir,

Please find sample document.

Hi Abhishek,

Thanks for sharing the detail. Unfortunately, Aspose.Words does not support the requested feature (Update information of Table Styles) at the moment. However, I have logged this feature request as WORDSNET-11334 in our issue tracking system. You will be notified via this forum thread once this feature is available. We apologize for your inconvenience.

As a workaround, please use following code example to achieve your requirements.

Document doc = new Document(MyDir + "Sample.docx");
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
//Change the color of table's header background 
for (Cell cell : (Iterable)table.getFirstRow().getCells())
{
    cell.getCellFormat().getShading().setBackgroundPatternColor(Color.BLUE);
}
//Chagne the color of title style
doc.getStyles().get("Title").getFont().setColor(Color.GRAY);
doc.save(MyDir + "Out.docx");

To change the color of a style, please use Style.Font.Color property. In your document, the text ‘Title’ has direct formatting. In this case, please use Run.Font.Color property to change the color of text.

Please let us know if you have any more queries.

Hi Tahir,

Thanks for information. One quick question. Can we change color of font or background color based on custom style?

Suppose, create custom style on word doc and change color of font and background.

I tried following but style api doesn’t apply changes:

for (Style style : doc.getStyles())
{
    if (style.getName().toLowerCase().equals("customheaderrow") || style.getName().toLowerCase().equals("customtitlecolor"))
    {
        bkColor = templateConfig.getCustomization().getBkColor();
        frColor = templateConfig.getCustomization().getForeGroundColor();
        bkColor = bkColor.startsWith("#") ? bkColor.substring(1) : bkColor;
        frColor = frColor.startsWith("#") ? frColor.substring(1) : frColor;
        style.getFont().setColor(new Color(Integer.valueOf(frColor.substring(0, 2), 16), Integer.valueOf(frColor.substring(2, 4), 16), Integer.valueOf(
        frColor.substring(4, 6), 16)));
        if (style.getName().toLowerCase().equals("customheaderrow"))
        {
            style.getFont().getShading().setBackgroundPatternColor(new Color(Integer.valueOf(bkColor.substring(0, 2), 16), Integer.valueOf(bkColor.substring(2, 4), 16), Integer.valueOf(
            bkColor.substring(4, 6), 16)));
        }
    }
}

But this changes won’t get reflected in document.

Please suggest is this approach is good or not.

Hi Abhishek,

Thanks for your inquiry. Yes, you can change the font color and set the color that’s applied to the background of the Shading object. Please check following code snippet.

//Chagne the font color of style
doc.getStyles().get("style name").getFont().setColor(Color.*GRAY*);
doc.getStyles().get("style name").getFont().getShading().setBackgroundPatternColor(Color.YELLOW);

Please make sure that you are using the correct font color in setColor/setBackgroundPatternColor method. If you still face problem, please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Please

create a standalone/runnable simple Java application that demonstrates the code (Aspose.Words code) you used to generate
your output document

  • Please attach the output Word file that shows the undesired behavior.

The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan