This sample shows how to use colored and uncolored tiling patterns.
A tiling pattern consists of a small graphical figure called a pattern cell. Painting with the pattern replicates the cell at fixed horizontal and vertical intervals to fill an area. A colored pattern specifies the colors used to paint the pattern cell. An uncolored pattern does not specify any color information. Instead, the entire pattern cell is painted with a separately specified color each time the pattern is used.
PdfTilingPattern class is used for both kinds of tiling patterns. You can add a pattern to your document using PdfDocument.AddColoredPattern or PdfDocument.AddUncoloredPattern method.
After a pattern is added to your document you should construct its appearance by drawing on a pattern canvas. When pattern is constructed, you can use it to stroke lines and shapes and/or to fill areas on a canvas. Use PdfCanvas.Pen.Pattern property to stroke lines and shapes or PdfCanvas.Brush.Pattern property to fill areas with a pattern.
using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; namespace BitMiracle.Docotic.Pdf.Samples { public static class Patterns { 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; PdfFont helvetica = pdf.AddFont(PdfBuiltInFont.HelveticaBoldOblique, false, false); canvas.Font = helvetica; canvas.FontSize = 28; drawColoredPattern(pdf); drawUncoloredPattern(pdf); string pathToFile = "Patterns.pdf"; pdf.Save(pathToFile); pdf.Dispose(); Process.Start(pathToFile); } private static void drawColoredPattern(PdfDocument pdf) { PdfTilingPattern pattern = pdf.AddColoredPattern(5, 5); fill(pattern); PdfCanvas canvas = pdf.GetPage(0).Canvas; canvas.Brush.Pattern = pattern; canvas.DrawString(new PointF(50, 50), 0, "Pattern-filled text"); canvas.DrawCircle(new PointF(0, 0), 20, PdfDrawMode.FillAndStroke); } private static void drawUncoloredPattern(PdfDocument pdf) { PdfTilingPattern pattern = pdf.AddUncoloredPattern(5, 5, new PdfRgbColorSpace()); fill(pattern); PdfCanvas canvas = pdf.GetPage(0).Canvas; canvas.Brush.Pattern = pattern; canvas.Brush.Color = new PdfRgbColor(0, 255, 0); canvas.DrawString(new PointF(50, 150), "Uncolored Pattern colored green"); } private static void fill(PdfTilingPattern pattern) { PdfCanvas canvas = pattern.Canvas; PdfColor red = new PdfRgbColor(255, 0, 0); canvas.Brush.Color = red; canvas.Pen.Color = red; canvas.AppendCircle(new PointF(2, 2), 2); canvas.FillAndStrokePath(PdfFillMode.Winding); } } }
Imports System.Diagnostics Imports System.Drawing Imports System.Drawing.Drawing2D Imports BitMiracle.Docotic.Pdf Namespace BitMiracle.Docotic.Pdf.Samples Public NotInheritable Class Patterns 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 Dim helvetica As PdfFont = pdf.AddFont(PdfBuiltInFont.HelveticaBoldOblique, False, False) canvas.Font = helvetica canvas.FontSize = 28 drawColoredPattern(pdf) drawUncoloredPattern(pdf) Dim pathToFile As String = "Patterns.pdf" pdf.Save(pathToFile) pdf.Dispose() Process.Start(pathToFile) End Sub Private Shared Sub drawColoredPattern(ByVal pdf As PdfDocument) Dim pattern As PdfTilingPattern = pdf.AddColoredPattern(5, 5) fill(pattern) Dim canvas As PdfCanvas = pdf.GetPage(0).Canvas canvas.Brush.Pattern = pattern canvas.DrawString(New PointF(50, 50), 0, "Pattern-filled text") canvas.DrawCircle(New PointF(0, 0), 20, PdfDrawMode.FillAndStroke) End Sub Private Shared Sub drawUncoloredPattern(ByVal pdf As PdfDocument) Dim pattern As PdfTilingPattern = pdf.AddUncoloredPattern(5, 5, New PdfRgbColorSpace()) fill(pattern) Dim canvas As PdfCanvas = pdf.GetPage(0).Canvas canvas.Brush.Pattern = pattern canvas.Brush.Color = New PdfRgbColor(0, 255, 0) canvas.DrawString(New PointF(50, 150), "Uncolored Pattern colored green") End Sub Private Shared Sub fill(ByVal pattern As PdfTilingPattern) Dim canvas As PdfCanvas = pattern.Canvas Dim red As PdfColor = New PdfRgbColor(255, 0, 0) canvas.Brush.Color = red canvas.Pen.Color = red canvas.AppendCircle(New PointF(2, 2), 2) canvas.FillAndStrokePath(PdfFillMode.Winding) End Sub End Class End Namespace
