PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Oct 31, 2024 11:53 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Drawing with wrong brush
PostPosted: Sun Sep 20, 2009 2:30 pm 
Offline

Joined: Sun Sep 20, 2009 12:55 pm
Posts: 3
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


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 16, 2009 1:54 pm 
Offline

Joined: Sun Sep 20, 2009 12:55 pm
Posts: 3
Hi again!

The bug can still be found in version 1.31.

Regards,

Malte


Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 17, 2011 1:19 pm 
Offline

Joined: Thu Feb 17, 2011 12:27 pm
Posts: 11
Similar bug seems to occur when drawing XForms.
To reproduce:
1. Crate XGraphics (graphics1) from PdfPage
2. Draw pink rectangle to graphics1
3. Create XForm using constructor XForm(graphics1, width, height)
4. Create XGraphics (graphics2) using Xgraphics.FromForm(form)
5. Draw black GraphicsPath to graphics2 (XForm)
6. Draw XForm to graphics1 using graphics1.DrawImage(form, 0,0)

Result: pink circle on a pink rectangle.
Any suggestions for a fix?

The wired part is that brush color from first drawing is used for drawing XForm, but the brush transparency is not preserved.


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 22, 2011 6:12 pm 
Offline

Joined: Thu Feb 17, 2011 12:27 pm
Posts: 11
SiliconMind wrote:
Similar bug seems to occur when drawing XForms.
1. Crate XGraphics (graphics1) from PdfPage
2. Draw pink rectangle to graphics1
3. Create XForm using constructor XForm(graphics1, width, height)
4. Create XGraphics (graphics2) using Xgraphics.FromForm(form)
5. Draw black GraphicsPath to graphics2 (XForm)
6. Draw XForm to graphics1 using graphics1.DrawImage(form, 0,0)


Workaround for this bug is to save and restore XGraphicsState each time a code block responsible for drawing is created. In the example above, one should do graphics1.Save() between steps 1 and 2. Then call graphics1.Restore(state) between steps 2 and 3. Ugly but works.


Top
 Profile  
Reply with quote  
PostPosted: Mon Jan 25, 2016 9:46 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3111
Location: Cologne, Germany
The workaround is not needed when using the current version of PDFsharp (1.50, released last year). Those who want to stick to version 1.3x have to use the workaround.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group