This sample shows how to construct and use graphics path.
Graphics path is a series of connected lines and curves. You can construct a graphics path using methods of PdfCanvas class with names that start with "Append" (e.g. AppendLineTo, AppendRectangle). All those methods do not place any marks on a canvas. Instead, they add corresponding shapes to the current graphics path.
When a graphics path is constructed, you can fill, stroke or reset it. You can also use constructed path as a clip region of a canvas.
using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; namespace BitMiracle.Docotic.Pdf.Samples { public static class Paths { public static void Main() { // NOTE: // When used in trial mode, the library imposes some restrictions. // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx // for more information. PdfDocument pdf = new PdfDocument(); PdfCanvas canvas = pdf.Pages[0].Canvas; canvas.Pen.Width = 3; canvas.Pen.Color = new PdfRgbColor(78, 55, 150); canvas.Brush.Color = new PdfRgbColor(200, 55, 50); canvas.AppendRectangle(new PdfRectangle(10, 60, 100, 100)); // following call resets the path and rectangle won't be drawn canvas.ResetPath(); canvas.CurrentPosition = new PdfPoint(20, 70); canvas.AppendLineTo(100, 70); canvas.AppendLineTo(100, 110); canvas.AppendCurveTo(new PdfPoint(80, 90), new PdfPoint(50, 90), new PdfPoint(30, 110)); canvas.FillAndStrokePath(PdfFillMode.Winding); string pathToFile = "Paths.pdf"; pdf.Save(pathToFile); pdf.Dispose(); Process.Start(pathToFile); } } }
Imports System.Diagnostics Imports System.Drawing Imports System.Drawing.Drawing2D Imports BitMiracle.Docotic.Pdf Namespace BitMiracle.Docotic.Pdf.Samples Public NotInheritable Class Paths Public Shared Sub Main() ' NOTE: ' When used in trial mode, the library imposes some restrictions. ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx ' for more information. Dim pdf As New PdfDocument() Dim canvas As PdfCanvas = pdf.Pages(0).Canvas canvas.Pen.Width = 3 canvas.Pen.Color = New PdfRgbColor(78, 55, 150) canvas.Brush.Color = New PdfRgbColor(200, 55, 50) canvas.AppendRectangle(New PdfRectangle(10, 60, 100, 100)) canvas.ResetPath() ' following call resets the path and rectangle won't be drawn canvas.CurrentPosition = New PdfPoint(20, 70) canvas.AppendLineTo(100, 70) canvas.AppendLineTo(100, 110) canvas.AppendCurveTo(New PdfPoint(80, 90), New PdfPoint(50, 90), New PdfPoint(30, 110)) canvas.FillAndStrokePath(PdfFillMode.Winding) Dim pathToFile As String = "Paths.pdf" pdf.Save(pathToFile) pdf.Dispose() Process.Start(pathToFile) End Sub End Class End Namespace
