Click or drag to resize

Text Element And Fonts

The Text Element allows adding texts to a PDF document and is implemented by the TextElement class. The TextElement offers many constructors that basically call the following constructor with more or less default values:

public TextElement(float x, float y, float width, float height, string text, PdfFont font, PdfColor textColor);

The constructor creates a paginable text element that will be rendered in the specified rectangle using the specified width, height, font and color.

If the text pagination is not allowed (Paginate property of the TextElement is False), the text will be written on current page, the rendered text height being given by the minimum between the specified height and the available height on page. The remaining text and the text bounds inside the current page are returned in the AddTextElementResult object when the element is added to a renderer.

If the text pagination is allowed (Paginate property of the TextElement is True) and the text needs pagination (the specified height is bigger than the available space on page), the height parameter will be ignored and the text will be rendered to the end using the necessary height. The text bounds inside the last page and the last page index are returned in the AddTextElementResult object when the element is added to a renderer.

If the text pagination is allowed but the text does not need pagination (the specified height is less than the available space on page), the rendered text will be truncated to fit the specified height. The text bounds inside the current page and the last page index are returned in the AddTextElementResult object when the element is added to a renderer.

Constructor parameters:

  • x - The start x coordinate where the text will be rendered.

  • y - The start y coordinate where the text.

  • width - The width of the destination rectangle.

  • height - The height of the destination rectangle.

  • text - The text to be rendered.

  • font - The text font.

  • textColor - The text color.

Sample Code

Below there is a sample code showing how to add text and fonts to a PDF document:

// add a page to the PDF document
PdfPage firstPage = document.AddPage();

// Create a Times New Roman .NET font of 10 points
System.Drawing.Font ttfFont = new System.Drawing.Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point);
// Create a Times New Roman Italic .NET font of 10 points
System.Drawing.Font ttfFontItalic = new System.Drawing.Font("Times New Roman", 10, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point);
// Create a Times New Roman Bold .NET font of 10 points
System.Drawing.Font ttfFontBold = new System.Drawing.Font("Times New Roman", 10, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point);
// Create a Times New Roman Bold .NET font of 10 points
System.Drawing.Font ttfFontBoldItalic = new System.Drawing.Font("Times New Roman", 10,
    System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point);

// Create a Sim Sun .NET font of 10 points
System.Drawing.Font ttfCJKFont = new System.Drawing.Font("SimSun", 10, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);

// Create the PDF fonts based on the .NET true type fonts
PdfFont newTimesFont = document.AddFont(ttfFont);
PdfFont newTimesFontItalic = document.AddFont(ttfFontItalic);
PdfFont newTimesFontBold = document.AddFont(ttfFontBold);
PdfFont newTimesFontBoldItalic = document.AddFont(ttfFontBoldItalic);

// Create the embedded PDF fonts based on the .NET true type fonts
PdfFont newTimesEmbeddedFont = document.AddFont(ttfFont, true);
PdfFont newTimesItalicEmbeddedFont = document.AddFont(ttfFontItalic, true);
PdfFont newTimesBoldEmbeddedFont = document.AddFont(ttfFontBold, true);
PdfFont newTimesBoldItalicEmbeddedFont = document.AddFont(ttfFontBoldItalic, true);
PdfFont cjkEmbeddedFont = document.AddFont(ttfCJKFont, true);

// Create a standard Times New Roman Type 1 Font
PdfFont stdTimesFont = document.AddFont(StdFontBaseFamily.TimesRoman);
PdfFont stdTimesFontItalic = document.AddFont(StdFontBaseFamily.TimesItalic);
PdfFont stdTimesFontBold = document.AddFont(StdFontBaseFamily.TimesBold);
PdfFont stdTimesFontBoldItalic = document.AddFont(StdFontBaseFamily.TimesBoldItalic);

// Create CJK standard Type 1 fonts
PdfFont cjkJapaneseStandardFont = document.AddFont(StandardCJKFont.HeiseiKakuGothicW5);
PdfFont cjkChineseTraditionalStandardFont = document.AddFont(StandardCJKFont.MonotypeHeiMedium);

// Add text elements to the document

TextElement trueTypeText = new TextElement(0, 10, "True Type Fonts Demo:", newTimesFontBold);
AddElementResult addResult = firstPage.AddElement(trueTypeText);

// Create the text element
TextElement textElement1 = new TextElement(20, addResult.EndPageBounds.Bottom + 10, "Hello World !!!!", newTimesFont);
// Add element to page. The result of adding the text element is stored into the addResult object
// which can be used to get information about the rendered size in PDF page.
addResult = firstPage.AddElement(textElement1);

// Add another element 5 points below the text above. The bottom of the text above is taken from the AddElementResult object
// set the font size
newTimesFontItalic.Size = 15;
TextElement textElement2 = new TextElement(20, addResult.EndPageBounds.Bottom + 5, "Hello World !!!!", newTimesFontItalic);
textElement2.ForeColor = System.Drawing.Color.Green;
addResult = firstPage.AddElement(textElement2);

newTimesFontBoldItalic.Size = 20;
TextElement textElement3 = new TextElement(20, addResult.EndPageBounds.Bottom + 5, "Hello World !!!!", newTimesFontBoldItalic);
textElement3.ForeColor = System.Drawing.Color.Blue;
addResult = firstPage.AddElement(textElement3);

TextElement stdTypeText = new TextElement(0, addResult.EndPageBounds.Bottom + 10, "Standard PDF Fonts Demo:", newTimesFontBold);
addResult = firstPage.AddElement(stdTypeText);

TextElement textElement4 = new TextElement(20, addResult.EndPageBounds.Bottom + 10, "Hello World !!!!", stdTimesFont);
addResult = firstPage.AddElement(textElement4);

stdTimesFontItalic.Size = 15;
TextElement textElement5 = new TextElement(20, addResult.EndPageBounds.Bottom + 5, "Hello World !!!!", stdTimesFontItalic);
textElement5.ForeColor = System.Drawing.Color.Green;
addResult = firstPage.AddElement(textElement5);

stdTimesFontBoldItalic.Size = 20;
TextElement textElement6 = new TextElement(20, addResult.EndPageBounds.Bottom + 5, "Hello World !!!!", stdTimesFontBoldItalic);
textElement6.ForeColor = System.Drawing.Color.Blue;
addResult = firstPage.AddElement(textElement6);

// embedded true type fonts

TextElement embeddedTtfText = new TextElement(0, addResult.EndPageBounds.Bottom + 10, "Embedded True Type Fonts Demo:", newTimesFontBold);
addResult = firstPage.AddElement(embeddedTtfText);

newTimesEmbeddedFont.Size = 15;

// russian text
TextElement textElement8 = new TextElement(20, addResult.EndPageBounds.Bottom + 5, "Появление на свет!!", newTimesEmbeddedFont);
addResult = firstPage.AddElement(textElement8);

//japanesse text 
TextElement textElementJapanese = new TextElement(20, addResult.EndPageBounds.Bottom + 5, "こんにちは世界!", cjkEmbeddedFont);
addResult = firstPage.AddElement(textElementJapanese);

//chinese text 
TextElement textElementChinese = new TextElement(20, addResult.EndPageBounds.Bottom + 5, "你好世界! !", cjkEmbeddedFont);
addResult = firstPage.AddElement(textElementChinese);

TextElement stdCJKText = new TextElement(0, addResult.EndPageBounds.Bottom + 10, "Standard CJK Fonts Demo:", newTimesFontBold);
addResult = firstPage.AddElement(stdCJKText);


// Hello World in Japanese 
TextElement textElement10 = new TextElement(20, addResult.EndPageBounds.Bottom + 20, "こんにちは世界!", cjkJapaneseStandardFont);
addResult = firstPage.AddElement(textElement10);

// Hello World in Chinese Traditional 
TextElement textElement11 = new TextElement(20, addResult.EndPageBounds.Bottom + 5, "你好世界! !", cjkChineseTraditionalStandardFont);
addResult = firstPage.AddElement(textElement11);