PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

ExcludeClip in PDFSharp.Drawing.XGraphics?
https://forum.pdfsharp.net/viewtopic.php?f=4&t=2382
Page 1 of 1

Author:  inexorabletash [ Sun Mar 24, 2013 12:01 am ]
Post subject:  ExcludeClip in PDFSharp.Drawing.XGraphics?

Basically, a request for implementation of XCombineMode.Exclude support so that XGraphics.ExcludeClip can be implemented.

I use the GDI+ build and target GDI+ and PDF.

Author:  rtseuztz [ Thu Jun 30, 2022 9:36 pm ]
Post subject:  Re: ExcludeClip in PDFSharp.Drawing.XGraphics?

I have spent about a week figuring out how to do this and I finally have. To simulate an exclude path,
    1. Make an XGraphicsPath
    2. Add the shapes you want removed from the final shape. ex:
    Code:
    clipPath.AddRectangle(temp.X , temp.Y , width , height)

      * If these shapes are overlapping, this will not work as the overlaps will be filled in
      ** For paths/polygons, instead of adding paths to the clip path, you should:
      a. call
      Code:
      clipPath.StartFigure()

      b. Add all of the lines from your path/polygon into the clip path.
      Code:
      clipPath.AddLine(oldPoint, newPoint)

      c. call
      Code:
      clipPath.CloseFigure()

      d. Do this for each path you have.
    3. Add the final shape to the clip path. I have a big rectangle I'm cutting smaller rectangles out of, so now I'll add my large rectagle.
    4. Set the clip path's fill mode to alternate. This is the even-odd fill that SVGs and other stuff has.
    Code:
    clipPath.FillMode = XFillMode.Alternate

    5. Call the intersect clip on your XGraphics object with this clip path.
    Code:
    gfx.IntersectClip(clipPath)

    6. Draw the final shape with your XGraphics object. This should be the same shape you added in step 3.
    Code:
    gfx.DrawRectangle(xPen, xBrush, bigRect.X, bigRect.Y, bigWidth, bigHeight)
Now you will have your small shapes cut out of your large shape!
You can also call gfx.Save() before clipping and gfx.Restore() after drawing the final rectangle if you want.
I am 9 years late but hopefully this helps others.

Attachments:
File comment: Large shape with small paths cut out of it. Its a blurry picture but it is under 256 KB
Nature_celebrating_India-9 (3)-min_page-0001-min.jpg
Nature_celebrating_India-9 (3)-min_page-0001-min.jpg [ 38.01 KiB | Viewed 331 times ]

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/