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

Free Support Forum - aspose.com

Unable to get text color of styled table

Using Aspose.Words 19.2 in Java.

I have a document which contains a styled table. The table header row contains white text. I need to be able to read the text color from this DOCX, and am unable to do so. I’ve tried looking in the following spots for a White color: cell shading (foreground), paragraph shading (foreground), run font color, run font shading (foreground). All return rgb 0,0,0; not 255, 255, 255 as expected.

Additionally, I am using expandTableStylesToDirectFormatting() but I don’t know that it’s doing anything.

I’ve attached a sample project that reproduces this (includes a sample DOCX file in resources). Can you verify whether this is a bug, or whether I’m just looking for the text color in the wrong place?

Thanks!

Repro source:

    public static void main(String[] args) throws Exception {
        byte[] docx = IOUtils.toByteArray(getSystemResourceAsStream("table_header_text_color.docx"));

        Document document = new Document(new ByteArrayInputStream(docx));
        document.expandTableStylesToDirectFormatting(); // Unsure of whether this has an effect, but worth trying.

        Table table = document.getFirstSection().getBody().getTables().get(0);
        Row row = table.getFirstRow();
        Cell cell = row.getFirstCell();

        System.out.println("Cell text: " + cell.getText());

        Color colorWhite = Color.WHITE;

        // Expect white color to be found somewhere. Why doesn't it exist in any of these spots?
        System.out.println("Expecting: " + colorWhite);

        // Check cell properties.
        System.out.println("Cell Shading (Foreground): " + cell.getCellFormat().getShading().getForegroundPatternColor());

        // Check paragraph properties
        for(Paragraph paragraph : cell.getParagraphs()) {
            System.out.println("Paragraph Shading (Foreground):" + paragraph.getParagraphFormat().getShading().getForegroundPatternColor());

            // Check run properties
            for(Run run : paragraph.getRuns()) {
                System.out.println("Run Font Color: " + run.getFont().getColor());
                System.out.println("Run Shading (Foreground): " + run.getFont().getShading().getForegroundPatternColor());
            }
        }
    }

Attachment:
asposebug.zip (87.8 KB)

@justinlindh,

We have logged your requirement in our issue tracking system. Your ticket number is WORDSNET-18161. We will further look into the details of this requirement and will keep you updated on the status of the linked issue.

@justinlindh,

Regarding WORDSNET-18161, we have completed the work on your issue and concluded to close this issue as ‘Not a Bug’. Please see the following analysis details:

Problematic paragraph is formatted with style “GSATableHeading” which has “auto” color setting. So, actually Aspose.Words returns “Color.Empty” (0x00000000) value corresponding to “auto” font color value. Automatic font color can be black or white depending on the current background.

MS Word GUI shows color “Automatic” for the text, so the concrete color is not specified. MS Word automation returns “WdColor.wdColorAutomatic” value at this case for the “Color” property and -16777216(0xFF000000 i.e. “Black”) for the “Font.TextColor.RGB” property.

So, it is not necessary to change current behavior of Aspose.Words API as MS Word does not resolve “Auto” color either.

You can check whether the color is ‘Auto’ using Color.IsEmpty property in .NET. And in Java, you can compare whether the Color is equal to:

new java.awt.Color(0, true)

or

new java.awt.Color(0, 0, 0, 0)

or write your own logic for this comparison. Hope, this helps.

Thank you for the response. I understand that DOCX specifies “Auto” for these colors, so this isn’t considered a bug. I’m writing an application that needs to mirror how Word colors the text/backgrounds. I know that Aspose is capable of doing this logic, because saving as a PDF via Words preserves the colors accurately. Can anybody provide some insight into how I’d be able to do this in my code?

@justinlindh,

Thanks for your inquiry. As mentioned in my previous post, you can check whether the color is ‘Auto’ using Color.IsEmpty property in .NET. And in Java, you can compare whether the Color is equal to:

new java.awt.Color(0, true)

or

new java.awt.Color(0, 0, 0, 0)

or please write your own logic for this comparison.

If we can help you with anything else, please feel free to ask.

Yes, I know how to find whether the color is set to “Auto”. I need to resolve the rendered value of “Auto” on this font color to black or white (Word seems to do this based on different themes/background colors/etc). My point was that I know Aspose has written code that does this, because saving as a PDF (and probably image) resolves the “Auto” colors to the correctly rendered color. I’m wondering if you can share some insight on how you do that.

@justinlindh,

We have logged your concern in our issue tracking system and will keep you posted on any further updates.

@justinlindh,

You can use the Font.AutoColor property to achieve this. Here is the description of this property:

Hope, this helps.

This is exactly what I was looking for. Thank you!