Multiple clipping regions on a PDF page not working
Asked Answered
D

1

8

I'm using SkiaSharp to print labels to PDF (and other things). Each page of the PDF can have multiple rows and columns. I need to clip each label to the correct size so it's doesn't trash neighbouring labels.

For testing, each label has a rectangle that extends too far plus 7 lines of text and a circle near the vertical middle.

My code is like:

using (var region = new SKRegion())
{
   region.SetRect(_labelClipRect);
   _currentCanvas.ClipRegion(region, SKClipOperation.Intersect);
   _labelView.Draw(_currentCanvas, _printRequest.Device.DPI, xOffsetPX, yOffsetPX);
}

The result is: Snippet of a PDF showing clipping errors The first label on each page looks correct but the rest are funky. The rectangle and circle are missing and the text is not clipped at all.

Anyone seen/got a sample of something like this?

Thanks

Damicke answered 16/5, 2018 at 12:54 Comment(1)
One idea I had was to paint a white(?) rectangle over the area outside the labels - rather than clipping it. However, labels may not have a white background so that could be an issue.Damicke
R
0

Each call to _currentCanvas.ClipRegion intersects passed region with the current one, it's not setting the clip to this region.

Try saving canvas state and restore after the label was drawn:

using (var region = new SKRegion())
{
   region.SetRect(_labelClipRect);
   _currentCanvas.Save();
   _currentCanvas.ClipRegion(region, SKClipOperation.Intersect);
   _labelView.Draw(_currentCanvas, _printRequest.Device.DPI, xOffsetPX, yOffsetPX);
   _currentCanvas.Restore();
}
Rolf answered 13/2, 2021 at 20:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.