EVO HTML to PDF Converter allows you to add text with various fonts to a PDF document. A text element is represented in API by a EvoPdfClientTextElement object.
You can add text elements using the fonts installed in system, fonts from local file system and standard PDF fonts. You can also add vertical text and text stamps to PDF, render the text in a given box or flow the text on multiple pages.
Code Sample - Add Text Elements to a PDF Document
C#
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Hosting; // Use EVO PDF Namespace using EvoPdfClient; namespace EvoHtmlToPdfDemo.Controllers.PDF_Creator { public class PDF_Creator_Text_ElementsController : Controller { private readonly Microsoft.AspNetCore.Hosting.IWebHostEnvironment m_hostingEnvironment; public PDF_Creator_Text_ElementsController(IWebHostEnvironment hostingEnvironment) { m_hostingEnvironment = hostingEnvironment; } // GET: PDF_Creator_Text_Elements public ActionResult Index() { return View(); } [HttpPost] public ActionResult CreatePdf(IFormCollection collection) { // Get the server options string serverIP = collection["textBoxServerIP"]; uint serverPort = uint.Parse(collection["textBoxServerPort"]); string servicePassword = collection["textBoxServicePassword"]; bool useServicePassword = servicePassword.Length > 0; bool useTcpService = collection["ServerType"] == "radioButtonUseTcpService"; string webServiceUrl = collection["textBoxWebServiceUrl"]; // Create a PDF document Document pdfDocument = null; if (useTcpService) pdfDocument = new Document(serverIP, serverPort); else pdfDocument = new Document(true, webServiceUrl); // Set optional service password if (useServicePassword) pdfDocument.ServicePassword = servicePassword; // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // The titles font used to mark various sections of the PDF document PdfFont titleFont = new PdfFont("Times New Roman", 10, true); titleFont.Bold = true; titleFont.Underline = true; // Create a PDF page in PDF document PdfPage pdfPage = pdfDocument.AddPage(); // Text Elements Using Fonts Installed in System // Add section title TextElement titleTextElement = new TextElement(5, 5, "Text Elements Using Fonts Installed in System", titleFont); titleTextElement.ForeColor = RgbColor.DarkGreen; pdfPage.AddElement(titleTextElement); // Embed in PDF document a font with Normal style installed in system PdfFont embeddedSystemFontNormal = new PdfFont("Times New Roman", 10, true); // Add a text element using a font with Normal style installed in system TextElement embeddedSystemFontNormalTextElement = new TextElement(0, 0, "This text uses a font with Normal style installed in system", embeddedSystemFontNormal); pdfDocument.AddElement(embeddedSystemFontNormalTextElement, 5, 10); // Embed in PDF document a font with Bold style installed in system PdfFont embeddedSystemFontBold = new PdfFont("Times New Roman", 10, true); embeddedSystemFontBold.Bold = true; // Add a text element using a font with Bold style installed in system TextElement embeddedSystemFontBoldTextElement = new TextElement(0, 0, "This text uses a font with Bold style installed in system", embeddedSystemFontBold); pdfDocument.AddElement(embeddedSystemFontBoldTextElement, 3); // Embed in PDF document a font with Italic style installed in system PdfFont embeddedSystemFontItalic = new PdfFont("Times New Roman", 10, true); embeddedSystemFontItalic.Italic = true; // Add a text element using a font with Italic style installed in system TextElement embeddedSystemFontItalicTextElement = new TextElement(0, 0, "This text uses a font with Italic style installed in system", embeddedSystemFontItalic); pdfDocument.AddElement(embeddedSystemFontItalicTextElement, 3); // Text Elements Using Fonts From Local Files // Add section title titleTextElement = new TextElement(0, 0, "Text Elements Using Fonts From Local Files", titleFont); titleTextElement.ForeColor = RgbColor.Navy; pdfDocument.AddElement(titleTextElement, -5, 15); // Embed a True Type font from a local file in PDF document PdfFont localTrueTypeFont = new PdfFont(m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Fonts/TrueType.ttf", 10); // Add a text element using the local True Type font to PDF document TextElement localFontTtfTextElement = new TextElement(0, 0, "This text uses a True Type Font loaded from a local file", localTrueTypeFont); pdfDocument.AddElement(localFontTtfTextElement, 5, 10); // Embed an OpenType font with TrueType Outlines in PDF document PdfFont localOpenTypeTrueTypeFont = new PdfFont(m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Fonts/OpenTypeTrueType.otf", 10); // Add a text element using the local OpenType font with TrueType Outlines to PDF document TextElement localOpenTypeTrueTypeFontTextElement = new TextElement(0, 0, "This text uses an Open Type Font with TrueType Outlines loaded from a local file", localOpenTypeTrueTypeFont); pdfDocument.AddElement(localOpenTypeTrueTypeFontTextElement); // Embed an OpenType font with PostScript Outlines in PDF document PdfFont localOpenTypePostScriptFont = new PdfFont(m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Fonts/OpenTypePostScript.otf", 10); // Add a text element using the local OpenType font with PostScript Outlines to PDF document TextElement localOpenTypePostScriptFontTextElement = new TextElement(0, 0, "This text uses an Open Type Font with PostScript Outlines loaded from a local file", localOpenTypePostScriptFont); pdfDocument.AddElement(localOpenTypePostScriptFontTextElement, 3); // Text Elements Using Standard PDF Fonts // Add section title titleTextElement = new TextElement(0, 0, "Text Elements Using Standard PDF Fonts", titleFont); titleTextElement.ForeColor = RgbColor.DarkGreen; pdfDocument.AddElement(titleTextElement, -5, 10); // Create a standard PDF font with Normal style PdfFont standardPdfFontNormal = new PdfFont(StdFontBaseFamily.Helvetica, 10); // Add a text element using the standard PDF font with Normal style TextElement standardPdfFontNormalTextElement = new TextElement(0, 0, "This text uses a standard PDF font with Normal style", standardPdfFontNormal); pdfDocument.AddElement(standardPdfFontNormalTextElement, 5, 10); // Create a standard PDF font with Bold style PdfFont standardPdfFontBold = new PdfFont(StdFontBaseFamily.HelveticaBold, 10); // Add a text element using the standard PDF font with Bold style TextElement standardPdfFontBoldTextElement = new TextElement(0, 0, "This text uses a standard PDF font with Bold style", standardPdfFontBold); pdfDocument.AddElement(standardPdfFontBoldTextElement, 3); // Create a standard PDF font with Italic style PdfFont standardPdfFontItalic = new PdfFont(StdFontBaseFamily.HelveticaOblique, 10); // Add a text element using the standard PDF font with Italic style TextElement standardPdfFontItalicTextElement = new TextElement(0, 0, "This text uses a standard PDF font with Italic style", standardPdfFontItalic); pdfDocument.AddElement(standardPdfFontItalicTextElement, 3); // Add a text element that flows freely in width and height string text = System.IO.File.ReadAllText(m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Text_Files/Text_File.txt"); // Add section title titleTextElement = new TextElement(0, 0, "Text Element that flows freely in width and height", titleFont); titleTextElement.ForeColor = RgbColor.DarkGreen; pdfDocument.AddElement(titleTextElement, -5, 10); // Add the text element TextElement freeWidthAndHeightTextElement = new TextElement(0, 0, text, embeddedSystemFontNormal); pdfDocument.AddElement(freeWidthAndHeightTextElement, 5, 10); // Add a text element with a given width that flows freely in height // Add section title titleTextElement = new TextElement(0, 0, "Text Element with a given width that flows freely in height", titleFont); titleTextElement.ForeColor = RgbColor.Navy; pdfDocument.AddElement(titleTextElement, -5, 10); // Add the text element TextElement freeHeightTextElement = new TextElement(0, 0, 400, text, embeddedSystemFontNormal); pdfDocument.AddElement(freeHeightTextElement, 5, 10); // Add a text element with a given width and height // Add section title titleTextElement = new TextElement(0, 0, "Text Element with a given width and height", titleFont); titleTextElement.ForeColor = RgbColor.DarkGreen; pdfDocument.AddElement(titleTextElement, -5, 10); // Add the text element TextElement boundedTextElement = new TextElement(0, 0, 400, 50, text, embeddedSystemFontNormal); pdfDocument.AddElement(boundedTextElement, 5, 10); // Add a text element that flows freely on next PDF page // Add section title titleTextElement = new TextElement(0, 0, "Text Element that flows freely on multiple PDF pages", titleFont); titleTextElement.ForeColor = RgbColor.Navy; pdfDocument.AddElement(titleTextElement, -5, 10); // Add the text element string multiPageText = System.IO.File.ReadAllText(m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/Text_Files/Large_Text_File.txt"); TextElement multiPageTextElement = new TextElement(0, 0, 575, multiPageText, embeddedSystemFontNormal); multiPageTextElement.BackColor = RgbColor.WhiteSmoke; pdfDocument.AddElement(multiPageTextElement, 5, 10); // Add a line at the bottom of the multipage text element LineElement bottomLine = new LineElement(0, 0, 100, 0); pdfDocument.AddElement(bottomLine, 0, 1); pdfPage = pdfDocument.AddPage(); // Text Elements with Vertical Text // Add section title titleTextElement = new TextElement(0, 0, "Vertical Text", titleFont); titleTextElement.ForeColor = RgbColor.Navy; pdfPage.AddElement(titleTextElement); // Add a top to bottom vertical text string topBottomText = "This is a Top to Bottom Vertical Text"; TextElement topBottomVerticalTextElement = new TextElement(0, 0, topBottomText, embeddedSystemFontNormal); topBottomVerticalTextElement.Translate(25, 50); topBottomVerticalTextElement.Rotate(90); pdfDocument.AddElement(topBottomVerticalTextElement); pdfPage = pdfDocument.AddPage(); // Add a bottom to top vertical text string bottomTopText = "This is a Bottom to Top Vertical Text"; float bottomTopTextWidth = 200; TextElement bottomTopVerticalTextElement = new TextElement(0, 0, bottomTopText, embeddedSystemFontNormal); bottomTopVerticalTextElement.Translate(0, bottomTopTextWidth); bottomTopVerticalTextElement.Rotate(-90); pdfPage.AddElement(bottomTopVerticalTextElement); // Add a text stamp to a PDF document // Create a PDF font PdfFont stampPdfFont = new PdfFont("Times New Roman", 24, true); // The stamp text string stampText = String.Format("Text Stamp {0}", DateTime.Now.ToString("d")); // Measure the text float textWidth = 100; PdfPage page = pdfDocument.AddPage(); // Get the PDF page drawable area width and height float pdfPageWidth = page.PageSize.Width; float pdfPageHeight = page.PageSize.Height; // Calculate the PDF page diagonal float pdfPageDiagonal = (float)Math.Sqrt(pdfPageWidth * pdfPageWidth + pdfPageHeight * pdfPageHeight); // The text location on PDF page diagonal float xStampLocation = (pdfPageDiagonal - textWidth) / 2; // Create the stamp as a rotated text element TextElement stampTextElement = new TextElement(xStampLocation, 0, stampText, stampPdfFont); stampTextElement.ForeColor = RgbColor.Coral; stampTextElement.Rotate((float)(Math.Atan(pdfPageHeight / pdfPageWidth) * (180 / Math.PI))); stampTextElement.Opacity = 75; // Add the stamp to PDF page page.AddElement(stampTextElement); // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF file to browser FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf"); fileResult.FileDownloadName = "Text_Elements.pdf"; return fileResult; } } }