How To Draw Text On a Canvas in PDF Mosaic Library .NET

This sample shows how to draw text on a canvas.

To draw text on a canvas, use PDFCanvas.DrawString.

C# :

using PDFMosaic;
using System.Drawing;

namespace DrawText
{
  class DrawText
  {
    static void Main()
    {
      PDFDocument document = new PDFDocument();
      document.Pages.Add(new PDFPage(PDFPaperFormat.A4));
      PDFCanvas canvas = document.Pages[0].Canvas;

      PDFFont font = new PDFFont("Arial", 16);
      PDFBrush brush = new PDFSolidBrush();

      canvas.DrawString("The test string", font, brush, 100, 100);

      PDFStringFormat sf = new PDFStringFormat();
      sf.HorizontalAlign = PDFHorizontalAlign.Center;
      sf.VerticalAlign = PDFVerticalAlign.Center;

      canvas.DrawString("The test string", font, brush, new RectangleF(200, 200, 100, 100), sf);
      canvas.DrawRectangle(new PDFSolidPen(), 200, 200, 100, 100);

      document.Save("DrawText.pdf", true);
    }
  }
}

VB.NET :

Imports PDFMosaic
Imports System.Drawing

Module DrawText
  Sub Main()
    Dim document As New PDFDocument()
    document.Pages.Add(New PDFPage(PDFPaperFormat.A4))
    Dim canvas As PDFCanvas = document.Pages(0).Canvas

    Dim font As New PDFFont("Arial", 16)
    Dim brush As New PDFSolidBrush()

    canvas.DrawString("The test string", font, brush, 100, 100)

    Dim sf As New PDFStringFormat()
    sf.HorizontalAlign = PDFHorizontalAlign.Center
    sf.VerticalAlign = PDFVerticalAlign.Center

    canvas.DrawString("The test string", font, brush, New RectangleF(200, 200, 100, 100), sf)
    canvas.DrawRectangle(New PDFSolidPen(), 200, 200, 100, 100)

    document.Save("DrawText.pdf", True)
  End Sub
End Module