getBackgroundPatternColor issue

Hi support,

The method getBackgroundPatternColor() of the Shading object returns same values for red, green and blue, when the background of the cell is set black or when is not set. My document is made with Word 2007.

Please advice,
Milan

Hi Milan,

Thanks for your request. Please attach your document here for testing. I will check the issue and provide you more information.

Best regards.

Hi Alexey,

Here is the document I’m using. Please specify if you need my code also.

Milan

Hi,

I’ve note that the Alpha component of the color (color.getAlpha()) is 0 when the background color of the cell is not set, so I think that a safety check over this component can make the difference between black and no color.

Regards,
Milan

Hi

Thanks for your request. It seems you missed that there is also Alpha in the color. In all cells in your document except the first one background color is Auto. Auto color in MS Word document is A=0, R=0, G=0, B=0.

Try using the following code to check the color:

public static void main(String[] args) throws Exception
{
    // Open document
    Document doc = new Document("C:\\Temp\\TemplBig.dot");

    // Get cells
    NodeCollection cells = doc.getChildNodes(NodeType.CELL, true);

    // Loop through all cells
    for (int i = 0; i < cells.getCount(); i++)
    {
        Cell cell = (Cell)cells.get(i);

        // Print background and foreground collors
        System.out.println("Cell #" + i);
        System.out.println("Background color: " + colorToString(cell.getCellFormat().getShading().getBackgroundPatternColor()));
        System.out.println("Foreground color: " + colorToString(cell.getCellFormat().getShading().getForegroundPatternColor()));
    }

    // Save output document
    doc.save("C:\\Temp\\out.doc");
}

private static String colorToString(java.awt.Color color)
{
    return String.format("java.awt.Color [a=%d r=%d g=%d b=%d]",
    color.getAlpha(),
    color.getRed(),
    color.getGreen(),
    color.getBlue());
}

You will see that background color in the first cell is different.

Best regards,