setClip method

void setClip({
  1. Rect? bounds,
  2. PdfPath? path,
  3. PdfFillMode? mode,
})

Sets the clipping region of this Graphics to the result of the specified operation combining the current clip region and the rectangle specified by a RectangleF structure.

//Creates a new PDF document.
PdfDocument doc = PdfDocument();
//Create PDF graphics for the page
doc.pages.add().graphics
  //set clip.
  ..setClip(bounds: Rect.fromLTWH(0, 0, 50, 12), mode: PdfFillMode.alternate)
  ..drawString('Hello world!', PdfStandardFont(PdfFontFamily.helvetica, 12),
      pen: PdfPens.red);
//Saves the document.
List<int> bytes = doc.save();
//Dispose the document.
doc.dispose();

Implementation

void setClip({Rect? bounds, PdfPath? path, PdfFillMode? mode}) {
  if (bounds != null) {
    mode ??= PdfFillMode.winding;
    _helper.streamWriter!.appendRectangle(
        bounds.left, bounds.top, bounds.width, bounds.height);
    _helper.streamWriter!.clipPath(mode == PdfFillMode.alternate);
  } else if (path != null) {
    mode ??= PdfPathHelper.getHelper(path).fillMode;
    _buildUpPath(PdfPathHelper.getHelper(path).points,
        PdfPathHelper.getHelper(path).pathTypes);
    _helper.streamWriter!.clipPath(mode == PdfFillMode.alternate);
  }
}