drawPolygon method

void drawPolygon(
  1. List<Offset> points, {
  2. PdfPen? pen,
  3. PdfBrush? brush,
})

Draws a polygon defined by a brush, an array of Offset structures.

//Creates a new PDF document.
PdfDocument doc = PdfDocument()
  ..pages.add().graphics.drawPolygon([
    Offset(10, 100),
    Offset(10, 200),
    Offset(100, 100),
    Offset(100, 200),
    Offset(55, 150)
  ], pen: PdfPens.black, brush: PdfBrushes.red);
//Saves the document.
List<int> bytes = doc.save();
//Dispose the document.
doc.dispose();

Implementation

void drawPolygon(List<Offset> points, {PdfPen? pen, PdfBrush? brush}) {
  _helper._beginMarkContent();
  if (points.isEmpty) {
    return;
  }
  _helper._stateControl(pen, brush, null, null);
  _helper.streamWriter!
      .beginPath(points.elementAt(0).dx, points.elementAt(0).dy);

  for (int i = 1; i < points.length; ++i) {
    _helper.streamWriter!
        .appendLineSegment(points.elementAt(i).dx, points.elementAt(i).dy);
  }
  _drawPath(pen, brush, PdfFillMode.winding, true);
  _helper.endMarkContent();
}