In the realm of PDF creation and editing, the ability to draw different styles of text within a PDF can greatly enhance the visual appeal and functionality of the documents. For example, you can draw transformed text to add a unique touch to the document design, or draw rotated text to create angled headings. In this article, you will learn how to draw text in PDF with different styles in C# using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Coordinate System in Spire.PDF
The text drawn in PDF is based on the page coordinate system. When adding a new page in a PDF, the page usually consists of the content area and margins all around.
In this case, the origin of the coordinate system is located at the top left corner of the content area, with the x-axis extending horizontally to the right and the y-axis extending vertically down.

Transformation of the Coordinate System
When transforming text in PDF, you are actually transforming the coordinate system where the text is located. Spire.PDF allows to translate, scale, skew, and rotate the page coordinate. The specific methods are introduced below:
- TranslateTransform(5, 5): Translate the coordinate system 5 units to the right and 5 units downward.
- ScaleTransform(2, 2): Scale the unit lengths on both the X-axis and the Y-axis by a factor of 2.
- RotateTransform(-45): Rotate the coordinate system 45 degrees counterclockwise.
- SkewTransform(10, 10): Tilt the X-axis 10 degrees down and the Y-axis 10 degrees to the right

By mastering this background knowledge about the page coordinate system, you can use Spire.PDF more accurately to draw different styles of text in PDFs to achieve complex typography.
Draw Transformed Text in PDF in C#
To accomplish the task, you can scale or skew the coordinate system of a PDF page through the PdfPageBase.Canvas.ScaleTransform() or PdfPageBase.Canvas.SkewTransform() method, and then draw text on the transformed coordinate system. The following are the detailed steps.
- Create a PdfDocument instance.
- Add a page using PdfDocument.Pages.Add() method.
- Save the current drawing state using PdfPageBase.Canvas.Save() method.
- Specify the PDF font and brush.
- Translate the page coordinate system to the specified position using PdfPageBase.Canvas.TranslateTransform() method .
- Scale the current coordinate system using PdfPageBase.Canvas.ScaleTransform() method .
- Skew the current coordinate system using PdfPageBase.Canvas.SkewTransform() method.
- Draw text on the page based on the transformed coordinate system using PdfPageBase.Canvas.DrawString() method.
- Restore the previous drawing state using PdfPageBase.Canvas.Restore(state) method.
- Save the result file using PdfDocument.SaveToFile() method.
- C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace PDFText
{
class Program
{
static void Main(string[] args)
{
// Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
// Add a page
PdfPageBase page = pdf.Pages.Add();
// Save the current graphics state
PdfGraphicsState state = page.Canvas.Save();
// Specify the PDF font and brush
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 18f);
PdfSolidBrush brush1 = new PdfSolidBrush(Color.Green);
PdfSolidBrush brush2 = new PdfSolidBrush(Color.Blue);
PdfSolidBrush brush3 = new PdfSolidBrush(Color.Magenta);
// Translate the coordinate system by specified coordinates
page.Canvas.TranslateTransform(20, 280);
// Scale the coordinate system by specified coordinates
page.Canvas.ScaleTransform(1f, 0.6f);
// Skew the coordinate system axes
page.Canvas.SkewTransform(-10, 0);
// Draw transformed text on the page
page.Canvas.DrawString("A Powerful PDF Processing Library", font, brush1, 0, -30);
// Skew the coordinate system axes
page.Canvas.SkewTransform(10, 0);
// Draw transformed text on the page
page.Canvas.DrawString("A Powerful PDF Processing Library", font, brush2, 0, 0);
//Scale the coordinate system by specified coordinates
page.Canvas.ScaleTransform(1f, -1f);
// Draw transformed text on the page
page.Canvas.DrawString("A Powerful PDF Processing Library", font, brush3, 0, 0);
// Restor graphics state
page.Canvas.Restore(state);
// Save the result document
pdf.SaveToFile("TransformText.pdf");
pdf.Close();
}
}
}

Draw Rotated Text in PDF in C#
The PdfPageBase.Canvas.RotateTransform(float angle) method provided by Spire.PDF allows to rotate the page coordinate system to specified angle, and then the text drawn on the page will be presented as rotated text. The following are the detailed steps.
- Create a PdfDocument instance.
- Add a page using PdfDocument.Pages.Add() method.
- Save the current drawing state using PdfPageBase.Canvas.Save() method.
- Specify the PDF font and brush, and set PDF text alignment.
- Translate the page coordinate system to the specified position using PdfPageBase.Canvas.TranslateTransform() method.
- Rotate the coordinate system by specified degree using PdfPageBase.Canvas.RotateTransform() method.
- Draw text on the page based on the rotated coordinate system using PdfPageBase.Canvas.DrawString() method.
- Restore the previous drawing state using PdfPageBase.Canvas.Restore(state) method.
- Save the result file using PdfDocument.SaveToFile() method.
- C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace PDFText
{
class Program
{
static void Main(string[] args)
{
// Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
// Add a page
PdfPageBase page = pdf.Pages.Add();
// Save the current graphics state
PdfGraphicsState state = page.Canvas.Save();
// Specify the PDF font and brush
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f);
PdfSolidBrush brush = new PdfSolidBrush(Color.Blue);
// Set PDF text alignment
PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
// Translate the coordinate system by specified coordinates
page.Canvas.TranslateTransform(page.Canvas.ClientSize.Width / 2, 380);
// Draw rotated text on the PDF page
for (int i = 0; i < 8; i++)
{
page.Canvas.RotateTransform(45);
page.Canvas.DrawString("A Powerful PDF Processing Library", font, brush, 20, 0, centerAlignment);
}
// Restor graphics state
page.Canvas.Restore(state);
// Save the result document
pdf.SaveToFile("RotateText.pdf");
pdf.Close();
}
}
}

Draw Aligned Text in PDF in C#
Spire.PDF for .NET provides the PdfStringFormat class to represent the text layout information. You can initialize an instance of the PdfStringFormat class and pass in two parameters PdfTextAlignment and PdfVerticalAlignment to specify a text alignment style, then draw text with the alignment style. The following are the detailed steps.
- Create a PdfDocument instance.
- Add a page using PdfDocument.Pages.Add() method.
- Save the current drawing state using PdfPageBase.Canvas.Save() method.
- Specify the PDF font and brush.
- Create a PdfStringFormat instance and pass in the PdfTextAlignment and PdfVerticalAlignment parameters to specify the text alignment style.
- Draw text with the specified alignment style on the PDF page using PdfPageBase.Canvas.DrawString() method.
- Save the result file using PdfDocument.SaveToFile() method.
- C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace PDFText
{
class Program
{
static void Main(string[] args)
{
// Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
// Add a page
PdfPageBase page = pdf.Pages.Add();
// Specify the PDF font and brush
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 14f);
PdfSolidBrush brush1 = new PdfSolidBrush(Color.Green);
PdfSolidBrush brush2 = new PdfSolidBrush(Color.Blue);
PdfSolidBrush brush3 = new PdfSolidBrush(Color.Magenta);
// Draw left-aligned text on the PDF page
PdfStringFormat leftAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
page.Canvas.DrawString("Left Align", font, brush1, 0, 60, leftAlignment);
// Draw right-aligned text on the PDF page
PdfStringFormat rightAlignment = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
page.Canvas.DrawString("Right Align", font, brush2, page.Canvas.ClientSize.Width, 60, rightAlignment);
// Draw center-aligned text on the PDF page
PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
page.Canvas.DrawString("Center Align", font, brush3, page.Canvas.ClientSize.Width / 2, 60, centerAlignment);
// Save the result document
pdf.SaveToFile("PdfTextAlignment.pdf");
pdf.Close();
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
