Convert documents to image: Fonts with auto Color

Hi all,

there is an issue within converting documents to images(png in my case), where Aspose.Words

interpretes auto Colors as black Colors. This behaviour is especially bad, if the document or paragraph -which contains these fonts- has a black background.
However, I managed to get a programmatic workaround. So maybe, this will help someone with his issues. Or maybe, you even see possibilities to optimize my code.
Since the following methods are parts of special wrapper classes, i added some parameters to make it more clear.
So here it is:

public void ConvertAutoColors(Document _document)
{
    // assignment just for initial value, preventing compiler errors
    Color documentBgColor = Color.White;

    // true, if document background is colored
    bool coloredDoc;
    if ((_document.BackgroundShape != null) && (_document.BackgroundShape.FillColor.ToArgb() != Color.White.ToArgb()))
    {
        documentBgColor = _document.BackgroundShape.FillColor;
        coloredDoc = true;
    }
    else coloredDoc = false;

    foreach(Paragraph paragraph in _document.GetChildNodes(NodeType.Paragraph, true))
    {
        Color paragraphColor = paragraph.ParagraphFormat.Shading.BackgroundPatternColor;
        int paragraphArgbValue = paragraphColor.ToArgb();
        // Document background = colored and paragraph with no extra shading
        if ((coloredDoc) && (paragraphArgbValue == 0))
            foreach(Run run in paragraph.Runs)
            {
                // ARGB == 0 => auto color
                if (run.Font.Color.ToArgb() == 0)
                    run.Font.Color = ConversionUtils.GetInverseColor(documentBgColor);
            }
        // paragraph has colored background shading
        if (paragraphArgbValue != 0)
            foreach(Run run in paragraph.Runs)
            {
                if (run.Font.Color.ToArgb() == 0)
                    run.Font.Color = ConversionUtils.GetInverseColor(paragraphColor);
            }
    }
}

And maybe you ask yourself, how the conversion method looks like. Well… in our special case, by now the customers only use black background (or maybe, they just recognized this issue on black background, since the font simply disappears for eyes). The naming may not be the best, since they are not really the inverse, but more the auto matched colors.

public static Color GetInverseColor(Color currentColor)
{
    int black = Color.Black.ToArgb();
    // no switch statement possible, since the compiler cant't handle defined Color int values
    if (currentColor.ToArgb() == black)
        return Color.White;
    else
        return Color.Black; //not the best, but a solution, one can live with
}

So I hope, this will help someone… Or if you just find some bad coding or a better way to implement, please let me know. As you can see, you can easily extend the mapping for other “inverse” colors. But that has to be made manually, which is not a lucky task i think.
kind regards,
Wolfgang

Hi Wolfgang,
Thank you for sharing your code. Could you please also attach the document you are having the problem with? I will check the issue and provide you more information.
Best regards,

Hello Alexey,
it seems, that I was not clearly enough.
The posting is not really about an actual issue we have, since we managed to solve it in the way described above. So that’s already a kind of the solution :wink:
I am sorry, that I had not explicitly made clear, that this post was just some kind of knowledge sharing for other users, which might experience similar problems.
The original issue was, that image conversion didnt apply auto color of fonts. So the font color was always black, instead of a matching the auto color of word. This is bad if the background color is also black.
The code above is one try to get a solution for this. Auto color has ARGB value 0. So I simply check for this value as you might notice. I also check before, if document oder paragraph has a black background. If so, i set the font programmatically to white by now.
If you want to reproduce the issue -but that was not my Intention- you can create a document with black background filling. then write some text with font color set to auto. convert the document to png and see the result. (You should see nothing except a black document).
Maybe, this might help you for some kind of fix.
Kind regards,
Wolfgang

Hi Wolfgang,
Thank you for additional information. I managed to reproduce the problem on my side. Your request has been linked to the appropriate issue. We will let you know once it is resolved.
One more time thank you for sharing your workaround, it can really help other customers.
Best regards,

The issues you have found earlier (filed as WORDSNET-4960) have been fixed in this .NET update and in this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.