Hi all!
I've found a bug while creating some graphics with PDF-Sharp 1.30. If you execute the following code, you would expect rectangle which is filled with a linear gradient from blue to green and a black text.
Code:
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XBrush brush = new XLinearGradientBrush(new XPoint(0, 0), new XPoint(50, 20), XColors.Blue, XColors.Green);
gfx.DrawRectangle(brush, 10, 10, 50, 20);
XFont font = new XFont("Microsoft Sans Serif", 9);
XBrush brush2 = new XSolidBrush(XColors.Black);
gfx.DrawString("DummyString", font, brush2, 10, 50);
document.Save(@"C:\test.pdf");
But the text isn't black here - it is rendered with the same linear gradient as the rectangle.
Digging through the code, I found a way to change this behaviour: Class PdfGraphicsState, Line 252 reads
Code:
if (this.realizedFillColor.Rgb != color.Rgb)
Change it to the following and it will work:
Code:
if (this.realizedFillColor.IsEmpty || this.realizedFillColor.Rgb != color.Rgb)
The reason is: After a linear gradient is used, the realizedFillColor is set do XColor.Empty. When drawing the text (in our example), the mentioned if statement is executed. It checks if XColor.Empty.Rgb (which is stored in realizedFillColor) equals the new XColor.Rgb (which is black in our example). This is true, because XColor.Empty.Rgb == 0 and XColor.Black.Rgb == 0.
I'm not sure if the mentioned solution is the best way. Maybe this has some sideeffects I dont't see here.
Malte