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

xanadev
  • 751
  • 9
  • 26
Jonesie
  • 6,997
  • 10
  • 48
  • 66
  • 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. – Jonesie May 28 '18 at 12:58

1 Answers1

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();
}
Maku
  • 1,464
  • 11
  • 20