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.

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.AddFont(PdfStandardFont.Helvetica);
font1.Size = 20;

// create a new pdf font (system font)
PdfFont font2 = doc.AddFont(new System.Drawing.Font("Verdana", 15));

// 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 Verdana, 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 Verdana, size 15, limited at a width of 200 points.", font2);
text3.ForeColor = System.Drawing.Color.Red;
text3.BackColor = System.Drawing.Color.LightBlue;
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 Verdana, size 15, limited at a width of 300 points.", font2);
text4.ForeColor = System.Drawing.Color.Red;
text4.BackColor = System.Drawing.Color.LightGreen;
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 Verdana, size 15, rotated 45 degrees.", font2);
text5.Direction = 45; // rotate 45 degrees counter-clockwise
result = page.Add(text5);

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

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