Click or drag to resize
Pdf Library for .NET

Pdf Text Element

Text can be added to a pdf document using PdfTextElement objects. There are a lot of settings that can be customized by Select.Pdf library when text is added to a document. Here are some of them:

Position of the text element in the destination pdf page can be customize using X and Y properties.

Size of the destination rectangle where the text will be rendered can be customized using Width and Height properties.

Color - both foreground and background colors can be customized using the ForeColor and BackColor properties of the PdfTextElement class.

Font - font family, size and style can be customized using Font property.

A font installed on the machine is described with PdfSystemFont (family name, size and style) and embedded into the document when it is added to Fonts. Use PdfSystemFonts to find out which families are available: InstalledFamilies lists them and IsFamilyInstalled(String) tests for one. A family that is not available is refused with an error rather than replaced by another font. The Liberation Sans, Liberation Serif and Liberation Mono families are bundled with the library and are always available, which makes them a safe choice for code that runs on Windows, Linux and macOS.

Text alignments can be controlled using HorizontalAlign and VerticalAlign properties.

Other properties - CharacterSpacing, WordSpacing, RightToLeft, TextRise, Leading.

Sample Code

Sample code that shows how to create text elements with different settings. Read the comments from the code for customization details.

// create a new pdf document
PdfDocument doc = new PdfDocument();

// create a new pdf font (component standard font)
PdfFont font1 = doc.Fonts.Add(PdfStandardFont.Helvetica);
font1.Size = 20;

// create a new pdf font (system font, embedded into the document)
// Liberation Sans is bundled with the library, so it is available on every host;
// PdfSystemFonts.IsFamilyInstalled tells whether another family can be used
string family = PdfSystemFonts.IsFamilyInstalled("Verdana")
    ? "Verdana" : "Liberation Sans";
PdfFont font2 = doc.Fonts.Add(new PdfSystemFont(family, 15f));

// add a new page to the document
PdfPage page = doc.AddPage();

// define a rendering result object
PdfRenderingResult result;

// create text element 
PdfTextElement text1 = new PdfTextElement(0, 0, 
    "Regular text using standard font Helvetica, size 20.", font1);
result = page.Add(text1);

// create text element 
PdfTextElement text2 = new PdfTextElement(0, result.PdfPageLastRectangle.Bottom + 20, 
    "Regular text using system font " + family + ", size 15.", font2);
result = page.Add(text2);

// create colored text element 
PdfTextElement text3 = new PdfTextElement(
    0, result.PdfPageLastRectangle.Bottom + 20, 200,
    "Red text over light blue background, using system font " + family +
    ", size 15, limited at a width of 200 points.", font2);
text3.ForeColor = new PdfColor(255, 0, 0);
text3.BackColor = new PdfColor(173, 216, 230);
result = page.Add(text3);

// create colored text element, right aligned
PdfTextElement text4 = new PdfTextElement(
    0, result.PdfPageLastRectangle.Bottom + 20, 300,
    "Red text over light green background, right aligned, using system font " +
    family + ", size 15, limited at a width of 300 points.", font2);
text4.ForeColor = new PdfColor(255, 0, 0);
text4.BackColor = new PdfColor(144, 238, 144);
text4.HorizontalAlign = PdfTextHorizontalAlign.Right;
result = page.Add(text4);

// create text element 
PdfTextElement text5 = new PdfTextElement(
    0, result.PdfPageLastRectangle.Bottom + 200, 300,
    "Regular text using system font " + family + ", size 15, rotated 45 degrees.",
    font2);
text5.Direction = 45; // rotate 45 degrees counter-clockwise
result = page.Add(text5);

// save pdf document
doc.Save("Sample.pdf");

// close pdf document
doc.Close();
See Also