Sample code · C#
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using SelectPdf.Universal; namespace SelectPdf.Samples.Pages { public class PdfTextElementModel : PageModel { public void OnGet() { } public IActionResult OnPost() { // 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, subset-embedded — cross-platform replacement // for the old SDK's System.Drawing.Font). Liberation Sans is used because it resolves // on any host: the library bundles it and serves it from embedded bytes when it is not // installed, so this works on a fontless container as well as on a desktop. // Deliberately no try/catch — a font that cannot be resolved must throw rather than be // silently replaced by another, which would draw text claiming a font the file does // not carry. PdfFont font2 = doc.Fonts.Add(new PdfSystemFont("Liberation Sans", 15)); // PdfSystemFonts tells which families this host has, so a font that is not installed // everywhere can be chosen explicitly, before it is used - and the text says which one it got. string headingFamily = PdfSystemFonts.IsFamilyInstalled("Verdana") ? "Verdana" : "Liberation Serif"; PdfFont font3 = doc.Fonts.Add(new PdfSystemFont(headingFamily, 15, PdfFontStyle.Bold)); // 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 in the family chosen above PdfTextElement text0 = new PdfTextElement(0, result.PdfPageLastRectangle.Bottom + 20, "Bold text using system font " + headingFamily + ", one of the " + PdfSystemFonts.InstalledFamilies.Count + " families available on this host.", font3); result = page.Add(text0); // create text element PdfTextElement text2 = new PdfTextElement(0, result.PdfPageLastRectangle.Bottom + 20, "Regular text using system font Liberation Sans, 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 Liberation Sans, 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 Liberation Sans, 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 Liberation Sans, size 15, rotated 45 degrees.", font2); text5.Direction = 45; // rotate 45 degrees counter-clockwise result = page.Add(text5); // save pdf document byte[] pdf = doc.Save(); // close pdf document doc.Close(); // return resulted pdf document FileResult fileResult = new FileContentResult(pdf, "application/pdf"); fileResult.FileDownloadName = "Document.pdf"; return fileResult; } } }