Texts and Fonts
Adds text with TrueType fonts (embedded or not), the standard PDF fonts and the standard CJK fonts; aligned, colored and rotated text, Unicode text and a long text that flows on the next pages.
C# code of this sample
using System;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using ExpertPdf.PdfCreator;
namespace ExpertPdf.PdfCreator.Samples.Pages
{
public class TextsAndFontsDemoModel : PageModel
{
[BindProperty]
public bool LongText { get; set; } = true;
public string Message { get; set; }
public void OnGet()
{
}
public IActionResult OnPost()
{
try
{
return File(CreatePdf(), "application/pdf", "TextsAndFonts.pdf");
}
catch (Exception ex)
{
Message = "The PDF document was not created. " + ex.Message;
return Page();
}
}
private byte[] CreatePdf()
{
// LicensingManager.LicenseKey = "your license key";
Document document = new Document();
PdfPage page = document.AddPage();
float width = page.ClientRectangle.Width;
PdfFont titleFont = document.AddFont(new Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Point), true);
// TrueType fonts created from System.Drawing fonts. Not embedded, they are fine for Western text and keep the
// file small; the viewer needs the font installed to display it exactly
PdfFont times = document.AddFont(new Font("Times New Roman", 12, GraphicsUnit.Point));
PdfFont timesItalic = document.AddFont(new Font("Times New Roman", 15, FontStyle.Italic, GraphicsUnit.Point));
PdfFont timesBoldItalic = document.AddFont(new Font("Times New Roman", 20, FontStyle.Bold | FontStyle.Italic, GraphicsUnit.Point));
AddElementResult result = page.AddElement(new TextElement(0, 0, "TrueType fonts", titleFont));
result = page.AddElement(new TextElement(20, result.EndPageBounds.Bottom + 5, "Hello World! (Times New Roman)", times));
TextElement green = new TextElement(20, result.EndPageBounds.Bottom + 5, "Hello World! (italic, green)", timesItalic);
green.ForeColor = Color.Green;
result = page.AddElement(green);
TextElement blue = new TextElement(20, result.EndPageBounds.Bottom + 5, "Hello World! (bold italic, blue)", timesBoldItalic);
blue.ForeColor = Color.Blue;
result = page.AddElement(blue);
// the 14 standard PDF fonts are available in every PDF viewer and are never embedded
PdfFont helvetica = document.AddFont(StdFontBaseFamily.Helvetica);
PdfFont courier = document.AddFont(StdFontBaseFamily.CourierBold);
result = page.AddElement(new TextElement(0, result.EndPageBounds.Bottom + 15, "Standard PDF fonts", titleFont));
result = page.AddElement(new TextElement(20, result.EndPageBounds.Bottom + 5, "Hello World! (Helvetica)", helvetica));
result = page.AddElement(new TextElement(20, result.EndPageBounds.Bottom + 5, "Hello World! (Courier Bold)", courier));
// embedded TrueType fonts display any Unicode text the font has, on any computer
PdfFont arialEmbedded = document.AddFont(new Font("Arial", 12, GraphicsUnit.Point), true);
result = page.AddElement(new TextElement(0, result.EndPageBounds.Bottom + 15, "Embedded TrueType fonts (Unicode text)", titleFont));
result = page.AddElement(new TextElement(20, result.EndPageBounds.Bottom + 5, "Russian: Привет, мир!", arialEmbedded));
result = page.AddElement(new TextElement(20, result.EndPageBounds.Bottom + 5, "Greek: Γειά σου Κόσμε!", arialEmbedded));
// the standard CJK fonts are not embedded: the PDF viewer displays them with its Asian fonts.
// The lines get some extra space above them for the taller glyphs
PdfFont japanese = document.AddFont(StandardCJKFont.HeiseiKakuGothicW5);
PdfFont chinese = document.AddFont(StandardCJKFont.MonotypeHeiMedium);
result = page.AddElement(new TextElement(0, result.EndPageBounds.Bottom + 15, "Standard CJK fonts", titleFont));
result = page.AddElement(new TextElement(20, result.EndPageBounds.Bottom + 18, "こんにちは世界", japanese));
result = page.AddElement(new TextElement(20, result.EndPageBounds.Bottom + 14, "你好,世界", chinese));
// text aligned in a given width
result = page.AddElement(new TextElement(0, result.EndPageBounds.Bottom + 15, "Alignment and rotation", titleFont));
float top = result.EndPageBounds.Bottom + 5;
string[] alignNames = { "Left aligned", "Centered", "Right aligned" };
HorizontalTextAlign[] aligns = { HorizontalTextAlign.Left, HorizontalTextAlign.Center, HorizontalTextAlign.Right };
for (int i = 0; i < aligns.Length; i++)
{
page.AddElement(new RectangleElement(20, top + i * 22, 250, 20));
TextElement aligned = new TextElement(22, top + i * 22 + 4, 246, alignNames[i], arialEmbedded);
aligned.TextAlign = aligns[i];
page.AddElement(aligned);
}
// rotated text: TextAngle turns the text counter-clockwise
TextElement rotated = new TextElement(380, top + 60, "Rotated text", titleFont);
rotated.TextAngle = 30;
rotated.ForeColor = Color.DarkRed;
result = page.AddElement(rotated);
if (LongText)
{
// a text longer than the page continues on new pages; the result tells where it ended
string paragraph = "A long text element is split between pages when it does not fit in the page. ";
string longText = "";
for (int i = 0; i < 120; i++)
longText += paragraph;
TextElement longElement = new TextElement(0, top + 90, width, longText + "THE END", arialEmbedded);
longElement.TextAlign = HorizontalTextAlign.Justify;
result = page.AddElement(longElement);
}
byte[] pdf = document.Save();
document.Close();
return pdf;
}
}
}