Create and write PDF files in C# and VB.NET

With SautinSoft.Pdf you can write rich text in PDF from your C# or VB.NET application.

Rich text can be represented by the PdfFormattedText class:

  • Append text and line breaks.
  • Set various formatting options, such as alignment, font (family, style, weight, stretch, and size), language, color, and opacity.
  • Set the maximum width of text or the maximum width of each line of text, and line height.
  • Set the text formatting mode (implementation of the text layout and text shaping).
  • Get the formatted text width and height.

The following example shows how to easily create a PDF document and write rich text on its first page.

Complete code

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
	static void Main()
	{
		
		using (var document = new PdfDocument())
		{
			// Add a page.
			var page = document.Pages.Add();

			using (var formattedText = new PdfFormattedText())
			{
				// Set font family and size.
				// All text appended next uses the specified font family and size.
				formattedText.FontFamily = new PdfFontFamily("Calibri");
				formattedText.FontSize = 12;

				formattedText.AppendLine("Hello World");

				// Reset font family and size for all text appended next.
				formattedText.FontFamily = new PdfFontFamily("Times New Roman");
				formattedText.FontSize = 14;
				formattedText.FontStyle = PdfFontStyle.Italic;
				formattedText.Color = PdfColor.FromRgb(1, 0, 0);
				formattedText.AppendLine(" This message was ");

				// Set font style and color for all text appended next.
				formattedText.FontFamily = new PdfFontFamily("Archi");
				formattedText.FontSize = 18;

				formattedText.Append("created by SautinSoft");

				// Reset font style and color for all text appended next.
				formattedText.FontStyle = PdfFontStyle.Normal;
				formattedText.Color = PdfColor.FromRgb(0, 0, 0);

				formattedText.Append(" component!");

				// Set the location of the bottom-left corner of the text.
				// We want top-left corner of the text to be at location (100, 100)
				// from the top-left corner of the page.
				// NOTE: In PDF, location (0, 0) is at the bottom-left corner of the page
				// and the positive y axis extends vertically upward.
				double x = 100, y = page.CropBox.Top - 100 - formattedText.Height;

				// Draw text to the page.
				page.Content.DrawText(formattedText, new PdfPoint(x, y));
			}
			document.Save("Writing.pdf");
		}
	}
}

            

Download.

The fonts used in the above examples are system fonts and must be placed in the directory specified by the operating system. To use non-system fonts (from a custom directory or embedded in an assembly), see the private font example.

The text in the example above is formatted to an infinite width. The following example shows how to format the text to a finite width and draw the text in various positions using the text size information, in various alignments.


Text alignment and positioning

The following example shows how to align and place text in different positions within a PDF page.

Complete code

using System;
using SautinSoft.Pdf;
using System.IO;
{
	static void Main()
	{
				using (var document = new PdfDocument())
		{
			var page = document.Pages.Add();
			double margin = 10;
			using (var formattedText = new PdfFormattedText())
			{
				formattedText.TextAlignment = PdfTextAlignment.Left;
				formattedText.MaxTextWidth = 100;
				formattedText.Append("This text is left aligned, ").
				Append("placed in the top-left corner of the page and ").
				Append("its width should not exceed 100 points.");
				page.Content.DrawText(formattedText,
					new PdfPoint(margin,
					page.CropBox.Top - margin - formattedText.Height));
					formattedText.Clear();
					formattedText.TextAlignment = PdfTextAlignment.Center;
					formattedText.MaxTextWidth = 200;
					formattedText.Append("This text is center aligned, ").
					Append("placed in the top-center part of the page ").
					Append("and its width should not exceed 200 points.");
					page.Content.DrawText(formattedText,
						new PdfPoint((page.CropBox.Width - formattedText.MaxTextWidth) / 2,
						page.CropBox.Top - margin - formattedText.Height));
						formattedText.Clear();
						formattedText.TextAlignment = PdfTextAlignment.Right;
						formattedText.MaxTextWidth = 100;
						formattedText.Append("This text is right aligned, ").
						Append("placed in the top-right corner of the page ").
						Append("and its width should not exceed 100 points.");
						page.Content.DrawText(formattedText,
							new PdfPoint(page.CropBox.Width - margin - formattedText.MaxTextWidth,
							page.CropBox.Top - margin - formattedText.Height));

							formattedText.Clear();

							formattedText.TextAlignment = PdfTextAlignment.Left;
							formattedText.MaxTextWidth = 100;
							formattedText.Append("This text is left aligned, ").
							Append("placed in the bottom-left corner of the page and ").
							Append("its width should not exceed 100 points.");
							page.Content.DrawText(formattedText,
								new PdfPoint(margin,
								margin));
								formattedText.Clear();
								formattedText.TextAlignment = PdfTextAlignment.Center;
								formattedText.MaxTextWidth = 200;
								formattedText.Append("This text is center aligned, ").
								Append("placed in the bottom-center part of the page and ").
								Append("its width should not exceed 200 points.");
								page.Content.DrawText(formattedText,
									new PdfPoint((page.CropBox.Width - formattedText.MaxTextWidth) / 2,
									margin));
									formattedText.Clear();
									formattedText.TextAlignment = PdfTextAlignment.Right;
									formattedText.MaxTextWidth = 100;
									formattedText.Append("This text is right aligned, ").
									Append("placed in the bottom-right corner of the page and ").
									Append("its width should not exceed 100 points.");
									page.Content.DrawText(formattedText,
										new PdfPoint(page.CropBox.Width - margin - formattedText.MaxTextWidth,
										margin));
										formattedText.Clear();
										formattedText.TextAlignment = PdfTextAlignment.Justify;
										formattedText.MaxTextWidths = new double[] { 200, 150, 100 };
										formattedText.Append("This text has justified alignment, ").
										Append("is placed in the center of the page and ").
										Append("its first line should not exceed 200 points, ").
										Append("its second line should not exceed 150 points and ").
										Append("its third and all other lines should not exceed 100 points.");
										// Center the text based on the width of the most lines, which is formattedText.MaxTextWidths[2].
										page.Content.DrawText(formattedText,
											new PdfPoint((page.CropBox.Width - formattedText.MaxTextWidths[2]) / 2,
											(page.CropBox.Height - formattedText.Height) / 2));
			document.Save("Alignment and Positioning.pdf");
		}
	}
  }
}

            

Download.

The above example shows how to place text on a single page. To place text on multiple pages, see the header and footer examples.

The text in the example above is just being transformed to different locations. The next example shows how to apply other transformations, such as rotation and scaling.


Text transformations

The following example shows how to apply various transformations, such as rotation and scaling, to text drawn on a PDF page.

Complete code

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
	static void Main()
	{
		
		using (var document = new PdfDocument())
		{
			var page = document.Pages.Add();

			using (var formattedText = new PdfFormattedText())
			{
				var text = "Rotated by 30 degrees around origin.";
				formattedText.Opacity = 0.2;
				formattedText.Append(text);
				var origin = new PdfPoint(50, 650);
				page.Content.DrawText(formattedText, origin);
				formattedText.Clear();
				formattedText.Opacity = 1;
				formattedText.Append(text);
				var transform = PdfMatrix.Identity;
				transform.Translate(origin.X, origin.Y);
				transform.Rotate(30);
				page.Content.DrawText(formattedText, transform);

				formattedText.Clear();
				text = "Rotated by 30 degrees around center.";
				formattedText.Opacity = 0.2;
				formattedText.Append(text);
				origin = new PdfPoint(300, 650);
				page.Content.DrawText(formattedText, origin);
				formattedText.Clear();
				formattedText.Opacity = 1;
				formattedText.Append(text);
				transform = PdfMatrix.Identity;
				transform.Translate(origin.X, origin.Y);
				transform.Rotate(30, formattedText.Width / 2, formattedText.Height / 2);
				page.Content.DrawText(formattedText, transform);

				formattedText.Clear();

				text = "Scaled horizontally by 0.5 around origin.";
				formattedText.Opacity = 0.2;
				formattedText.Append(text);
				origin = new PdfPoint(50, 500);
				page.Content.DrawText(formattedText, origin);
				formattedText.Clear();
				formattedText.Opacity = 1;
				formattedText.Append(text);
				transform = PdfMatrix.Identity;
				transform.Translate(origin.X, origin.Y);
				transform.Scale(0.5, 1);
				page.Content.DrawText(formattedText, transform);

				formattedText.Clear();

				text = "Scaled horizontally by 0.5 around center.";
				formattedText.Opacity = 0.2;
				formattedText.Append(text);
				origin = new PdfPoint(300, 500);
				page.Content.DrawText(formattedText, origin);
				formattedText.Clear();
				formattedText.Opacity = 1;
				formattedText.Append(text);
				transform = PdfMatrix.Identity;
				transform.Translate(origin.X, origin.Y);
				transform.Scale(0.5, 1, formattedText.Width / 2, formattedText.Height / 2);
				page.Content.DrawText(formattedText, transform);

				formattedText.Clear();

				text = "Scaled vertically by 2 around origin.";
				formattedText.Opacity = 0.2;
				formattedText.Append(text);
				origin = new PdfPoint(50, 400);
				page.Content.DrawText(formattedText, origin);
				formattedText.Clear();
				formattedText.Opacity = 1;
				formattedText.Append(text);
				transform = PdfMatrix.Identity;
				transform.Translate(origin.X, origin.Y);
				transform.Scale(1, 2);
				page.Content.DrawText(formattedText, transform);

				formattedText.Clear();

				text = "Scaled vertically by 2 around center.";
				formattedText.Opacity = 0.2;
				formattedText.Append(text);
				origin = new PdfPoint(300, 400);
				page.Content.DrawText(formattedText, origin);
				formattedText.Clear();
				formattedText.Opacity = 1;
				formattedText.Append(text);
				transform = PdfMatrix.Identity;
				transform.Translate(origin.X, origin.Y);
				transform.Scale(1, 2, formattedText.Width / 2, formattedText.Height / 2);
				page.Content.DrawText(formattedText, transform);

				formattedText.Clear();

				text = "Rotated by 30 degrees around origin and ";
				var text2 = "scaled horizontally by 0.5 and ";
				var text3 = "vertically by 2 around origin.";
				formattedText.Opacity = 0.2;
				formattedText.AppendLine(text).AppendLine(text2).Append(text3);
				origin = new PdfPoint(50, 200);
				page.Content.DrawText(formattedText, origin);
				formattedText.Clear();
				formattedText.Opacity = 1;
				formattedText.AppendLine(text).AppendLine(text2).Append(text3);
				transform = PdfMatrix.Identity;
				transform.Translate(origin.X, origin.Y);
				transform.Rotate(30);
				transform.Scale(0.5, 2);
				page.Content.DrawText(formattedText, transform);

				formattedText.Clear();

				text = "Rotated by 30 degrees around center and ";
				text2 = "scaled horizontally by 0.5 and ";
				text3 = "vertically by 2 around center.";
				formattedText.Opacity = 0.2;
				formattedText.AppendLine(text).AppendLine(text2).Append(text3);
				origin = new PdfPoint(300, 200);
				page.Content.DrawText(formattedText, origin);
				formattedText.Clear();
				formattedText.Opacity = 1;
				formattedText.AppendLine(text).AppendLine(text2).Append(text3);
				transform = PdfMatrix.Identity;
				transform.Translate(origin.X, origin.Y);
				transform.Rotate(30, formattedText.Width / 2, formattedText.Height / 2);
				transform.Scale(0.5, 2, formattedText.Width / 2, formattedText.Height / 2);
				page.Content.DrawText(formattedText, transform);
			}
			document.Save("Transformations.pdf");
		}
	}
}

            

Download.

The above example shows how to convert text on a single page. To convert text on multiple pages, see the watermark example.

The previous examples show how to write text in Latin script. The following examples show how to write text in other scripts, including Arabic, Hebrew, various Indian scripts, and other complex scripts.


Text from complex scripts

The following example shows how to write text from complex scripts, such as Arabic, Hebrew, various Indic, and other.

Complete code

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
	static void Main()
	{
		
		using (var document = new PdfDocument())
		{
			var page = document.Pages.Add();

			using (var formattedText = new PdfFormattedText())
			{
				formattedText.Language = new PdfLanguage("en-US");
				formattedText.Font = new PdfFont("Calibri", 12);

				formattedText.AppendLine("An example of a fully vocalised (vowelised or vowelled) Arabic ").
				Append("from the Basmala: ");

				formattedText.Language = new PdfLanguage("ar-SA");
				formattedText.Font = new PdfFont("Arial", 24);
				formattedText.Append("بِسْمِ ٱللَّٰهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ");

				formattedText.Language = new PdfLanguage("en-US");
				formattedText.Font = new PdfFont("Calibri", 12);
				formattedText.AppendLine(", which means: ").
				Append("In the name of God, the All-Merciful, the Especially-Merciful.");

				page.Content.DrawText(formattedText, new PdfPoint(50, 750));

				formattedText.Clear();

				formattedText.Append("An example of Hebrew: ");

				formattedText.Language = new PdfLanguage("he-IL");
				formattedText.Font = new PdfFont("Arial", 24);
				formattedText.Append("מה קורה");

				formattedText.Language = new PdfLanguage("en-US");
				formattedText.Font = new PdfFont("Calibri", 12);
				formattedText.AppendLine(", which means: What's going on, ").
				Append("and ");

				formattedText.Language = new PdfLanguage("he-IL");
				formattedText.Font = new PdfFont("Arial", 24);
				formattedText.Append("תודה לכולם");

				formattedText.Language = new PdfLanguage("en-US");
				formattedText.Font = new PdfFont("Calibri", 12);
				formattedText.Append(", which means: Thank you all.");

				page.Content.DrawText(formattedText, new PdfPoint(50, 650));

				formattedText.Clear();

				formattedText.LineHeight = 50;

				formattedText.Append("An example of Thai: ");
				formattedText.Language = new PdfLanguage("th-TH");
				formattedText.Font = new PdfFont("Leelawadee UI", 16);
				formattedText.AppendLine("ภัำ");

				formattedText.Language = new PdfLanguage("en-US");
				formattedText.Font = new PdfFont("Calibri", 12);
				formattedText.Append("An example of Tamil: ");
				formattedText.Language = new PdfLanguage("ta-IN");
				formattedText.Font = new PdfFont("Nirmala UI", 16);
				formattedText.AppendLine("போது");

				formattedText.Language = new PdfLanguage("en-US");
				formattedText.Font = new PdfFont("Calibri", 12);
				formattedText.Append("An example of Bengali: ");
				formattedText.Language = new PdfLanguage("be-IN");
				formattedText.Font = new PdfFont("Nirmala UI", 16);
				formattedText.AppendLine("আবেদনকারীর মাতার পিতার বর্তমান স্থায়ী ঠিকানা নমিনি নাম");

				formattedText.Language = new PdfLanguage("en-US");
				formattedText.Font = new PdfFont("Calibri", 12);
				formattedText.Append("An example of Gujarati: ");
				formattedText.Language = new PdfLanguage("gu-IN");
				formattedText.Font = new PdfFont("Nirmala UI", 16);
				formattedText.AppendLine("કાર્બન કેમેસ્ટ્રી");

				formattedText.Language = new PdfLanguage("en-US");
				formattedText.Font = new PdfFont("Calibri", 12);
				formattedText.Append("An example of Osage: ");
				formattedText.Language = new PdfLanguage("osa");
				formattedText.Font = new PdfFont("Gadugi", 16);
				formattedText.Append("𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟");

				page.Content.DrawText(formattedText, new PdfPoint(50, 350));
			}

			document.Save("Complex scripts.pdf");
		}
	}
}

            

Download.

Note that formatting Right-to-Left complex scripts, such as Arabic or Hebrew, currently works only via WPF (by setting the PdfFormattedText.TextFormattingMode property to PdfTextFormattingMode.WPF, which is set by default when running on Windows).

Formating Left-to-Right complex scripts, such as various Indic scripts, also works via HarfBuzz text-shaping engine (by setting the PdfFormattedText.TextFormattingMode property to PdfTextFormattingMode.HarfBuzz, which is set by default when running on non-Windows operating systems and the HarfBuzzSharp package is referenced by the project).


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.