PDF Text

Using the HiQPdf library you can layout text objects at any position in a PDF canvas, you can let the text flow to the right and the bottom of the canvas or you can limit it in a box. The text objects are paginated by default which means that when a text gets to the bottom of a PDF page while flowing down it will automatically continue on the next PDF page.

The text objects are represented by the PdfText class. The text can use the true type fonts or the standard PDF Type 1 fonts.

PDF Text Demo

In this demo you can learn how to layout text objects in a PDF document with various layouts and fonts. The generated PDF document will contain horizontal and rotated text objects, text with true type fonts and text with PDF standard fonts.

There are three main layouting options for the text exemplified in this demo: the text is rendered at a given location and has free width and height, the text is limited in width or the text is limited both in width and height. There is also an example where the text is automatically laid out on the next page when it gets to the bottom of a PDF page.

Demo Source Code

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;

using HiQPdf;

namespace HiQPdf_Demo.Controllers
{
    public class PdfTextDemoController : Controller
    {
        IHostingEnvironment m_hostingEnvironment;
        public PdfTextDemoController(IHostingEnvironment hostingEnvironment)
        {
            m_hostingEnvironment = hostingEnvironment;
        }

        // GET: PdfTextDemo
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreatePdf(IFormCollection collection)
        {
            // create a PDF document
            PdfDocument document = new PdfDocument();

            // set a demo serial number
            document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";

            // create a page in document
            PdfPage page1 = document.AddPage();

            // create the true type fonts that can be used in document text
            System.Drawing.Font sysFont = new System.Drawing.Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point);
            PdfFont pdfFont = document.CreateFont(sysFont);
            PdfFont pdfFontEmbed = document.CreateFont(sysFont, true);

            System.Drawing.Font sysFontBold = new System.Drawing.Font("Times New Roman", 10, System.Drawing.FontStyle.Bold,
                            System.Drawing.GraphicsUnit.Point);
            PdfFont pdfFontBold = document.CreateFont(sysFontBold);
            PdfFont pdfFontBoldEmbed = document.CreateFont(sysFontBold, true);

            // create a standard Helvetica Type 1 font that can be used in document text
            PdfFont helveticaStdFont = document.CreateStandardFont(PdfStandardFont.Helvetica);
            helveticaStdFont.Size = 10;

            float crtYPos = 20;
            float crtXPos = 5;

            PdfLayoutInfo textLayoutInfo = null;

            string dummyText = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

            #region Layout a text that expands to the right edge of the PDF page

            PdfText titleTextAtLocation = new PdfText(crtXPos, crtYPos,
                    "The text below extends from the layout position to the right edge of the PDF page:", pdfFontBoldEmbed);
            titleTextAtLocation.ForeColor = System.Drawing.Color.Navy;
            textLayoutInfo = page1.Layout(titleTextAtLocation);

            // advance the Y position in the PDF page
            crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

            PdfText textExpandsToRightEdge = new PdfText(crtXPos + 50, crtYPos, dummyText, pdfFont);
            textExpandsToRightEdge.BackColor = System.Drawing.Color.WhiteSmoke;
            textLayoutInfo = page1.Layout(textExpandsToRightEdge);

            // draw a rectangle around the text
            PdfRectangle borderPdfRectangle = new PdfRectangle(textLayoutInfo.LastPageRectangle);
            borderPdfRectangle.LineStyle.LineWidth = 0.5f;
            page1.Layout(borderPdfRectangle);

            // advance the Y position in the PDF page
            crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

            #endregion

            #region Layout a text with width limit

            PdfText titleTextWithWidth = new PdfText(crtXPos, crtYPos,
                    "The text below is limited by a given width and has a free height:", pdfFontBoldEmbed);
            titleTextWithWidth.ForeColor = System.Drawing.Color.Navy;
            textLayoutInfo = page1.Layout(titleTextWithWidth);

            // advance the Y position in the PDF page
            crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

            PdfText textWithWidthLimit = new PdfText(crtXPos + 50, crtYPos, 300, dummyText, pdfFont);
            textWithWidthLimit.BackColor = System.Drawing.Color.WhiteSmoke;
            textLayoutInfo = page1.Layout(textWithWidthLimit);

            // draw a rectangle around the text
            borderPdfRectangle = new PdfRectangle(textLayoutInfo.LastPageRectangle);
            borderPdfRectangle.LineStyle.LineWidth = 0.5f;
            page1.Layout(borderPdfRectangle);

            // advance the Y position in the PDF page
            crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

            #endregion

            #region Layout a text with width and height limits

            PdfText titleTextWithWidthAndHeight = new PdfText(crtXPos, crtYPos,
                    "The text below is limited by a given width and height and is trimmed:", pdfFontBoldEmbed);
            titleTextWithWidthAndHeight.ForeColor = System.Drawing.Color.Navy;
            textLayoutInfo = page1.Layout(titleTextWithWidthAndHeight);

            // advance the Y position in the PDF page
            crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

            PdfText textWithWidthAndHeightLimit = new PdfText(crtXPos + 50, crtYPos, 300, 50, dummyText, pdfFont);
            textWithWidthAndHeightLimit.BackColor = System.Drawing.Color.WhiteSmoke;
            textLayoutInfo = page1.Layout(textWithWidthAndHeightLimit);

            // draw a rectangle around the text
            borderPdfRectangle = new PdfRectangle(textLayoutInfo.LastPageRectangle);
            borderPdfRectangle.LineStyle.LineWidth = 0.5f;
            page1.Layout(borderPdfRectangle);

            // advance the Y position in the PDF page
            crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

            #endregion

            #region Layout a text with standard font

            PdfText textWithStandardFont = new PdfText(crtXPos, crtYPos, "This green text is written with a Helvetica Standard Type 1 font", helveticaStdFont);
            textWithStandardFont.BackColor = System.Drawing.Color.WhiteSmoke;
            textWithStandardFont.ForeColor = System.Drawing.Color.Green;
            textLayoutInfo = page1.Layout(textWithStandardFont);

            // advance the Y position in the PDF page
            crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

            #endregion

            #region Layout a rotated text

            PdfText titleRotatedText = new PdfText(crtXPos, crtYPos, "The text below is rotated:", pdfFontBoldEmbed);
            titleRotatedText.ForeColor = System.Drawing.Color.Navy;
            textLayoutInfo = page1.Layout(titleRotatedText);

            // advance the Y position in the PDF page
            crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

            // create a reference Graphics used for measuring
            System.Drawing.Bitmap refBmp = new System.Drawing.Bitmap(1, 1);
            System.Drawing.Graphics refGraphics = System.Drawing.Graphics.FromImage(refBmp);
            refGraphics.PageUnit = System.Drawing.GraphicsUnit.Point;

            string counterRotatedText = "This text is rotated 45 degrees counter clockwise";

            // measure the rotated text size
            System.Drawing.SizeF counterRotatedTextSize = refGraphics.MeasureString(counterRotatedText, sysFont);

            // advance the Y position in the PDF page
            crtYPos += counterRotatedTextSize.Width / (float)Math.Sqrt(2) + 10;

            string clockwiseRotatedText = "This text is rotated 45 degrees clockwise";

            PdfText rotatedCounterClockwiseText = new PdfText(crtXPos + 100, crtYPos, counterRotatedText, pdfFontEmbed);
            rotatedCounterClockwiseText.RotationAngle = 45;
            textLayoutInfo = page1.Layout(rotatedCounterClockwiseText);

            PdfText rotatedClockwiseText = new PdfText(crtXPos + 100, crtYPos, clockwiseRotatedText, pdfFontEmbed);
            rotatedClockwiseText.RotationAngle = -45;
            textLayoutInfo = page1.Layout(rotatedClockwiseText);

            // measure the rotated text size
            System.Drawing.SizeF clockwiseRotatedTextSize = refGraphics.MeasureString(clockwiseRotatedText, sysFont);

            // advance the Y position in the PDF page
            crtYPos += clockwiseRotatedTextSize.Width / (float)Math.Sqrt(2) + 10;

            // dispose the graphics used for measuring
            refGraphics.Dispose();
            refBmp.Dispose();

            #endregion

            #region Layout an automatically paginated text

            string dummyBigText = System.IO.File.ReadAllText(m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Text\DummyBigText.txt");

            PdfText titleTextPaginated = new PdfText(crtXPos, crtYPos,
                    "The text below is automatically paginated when it gets to the bottom of this page:", pdfFontBoldEmbed);
            titleTextPaginated.ForeColor = System.Drawing.Color.Navy;
            textLayoutInfo = page1.Layout(titleTextPaginated);

            // advance the Y position in the PDF page
            crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

            PdfText paginatedText = new PdfText(crtXPos + 50, crtYPos, 300, dummyBigText, pdfFont);
            paginatedText.BackColor = System.Drawing.Color.WhiteSmoke;
            paginatedText.Cropping = false;
            textLayoutInfo = page1.Layout(paginatedText);

            // get the last page where the text was rendered
            PdfPage crtPage = document.Pages[textLayoutInfo.LastPageIndex];

            // draw a line at the bottom of the text on the second page
            System.Drawing.PointF leftPoint = new System.Drawing.PointF(textLayoutInfo.LastPageRectangle.Left, textLayoutInfo.LastPageRectangle.Bottom);
            System.Drawing.PointF rightPoint = new System.Drawing.PointF(textLayoutInfo.LastPageRectangle.Right, textLayoutInfo.LastPageRectangle.Bottom);
            PdfLine borderLine = new PdfLine(leftPoint, rightPoint);
            borderLine.LineStyle.LineWidth = 0.5f;
            crtPage.Layout(borderLine);

            // advance the Y position in the second PDF page
            crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

            #endregion

            try
            {
                // write the PDF document to a memory buffer
                byte[] pdfBuffer = document.WriteToMemory();

                FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
                fileResult.FileDownloadName = "PdfText.pdf";

                return fileResult;
            }
            finally
            {
                document.Close();
            }
        }
    }
}

See Also