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