How To Change Word Spacing in PDF Mosaic Library .NET

This sample shows how to change word spacing using PDFStringFormat.WordSpacing property.

Word spacing defines a distance between adjacent words in a line of text.

C# :

using PDFMosaic;
using System.Drawing;

namespace WordSpacing
{
  class WordSpacing
  {
    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();
      PDFStringFormat sf = new PDFStringFormat();

      string text = "Word spacing = ";
      
      sf.WordSpacing = 12.0f;
      canvas.DrawString(text + "20.0", font, brush, 10, 100, sf);

      sf.WordSpacing = 5.0f;
      canvas.DrawString(text + "5.0", font, brush, 10, 130, sf);

      sf.WordSpacing = 1.0f;
      canvas.DrawString(text + "1.0", font, brush, 10, 160, sf);

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

VB.NET :

Imports PDFMosaic
Imports System.Drawing

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

    Dim font As PDFFont = New PDFFont("Arial", 16)
    Dim brush As PDFBrush = New PDFSolidBrush()
    Dim sf As PDFStringFormat = New PDFStringFormat()

    Dim text As String = "The test word spacing = "

    sf.WordSpacing = 12.0F
    canvas.DrawString(text + "20.0", font, brush, 10, 100, sf)

    sf.WordSpacing = 5.0F
    canvas.DrawString(text + "5.0", font, brush, 10, 130, sf)

    sf.WordSpacing = 1.0F
    canvas.DrawString(text + "1.0", font, brush, 10, 160, sf)

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