Skip Navigation Links.
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 ths 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.
Server Settings
IP: Port: Password:
Skip Navigation Links
C# Code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using HiQPdfClient;

namespace HiQPdf_Demo
{
    public partial class PdfTextDemo : System.Web.UI.Page
    {
        protected void buttonCreatePdf_Click(object sender, EventArgs e)
        {
            string serverIP = textBoxServerIP.Text;
            uint serverPort = uint.Parse(textBoxServerPort.Text);
            string serverPassword = textBoxServerPassword.Text;

            // create a PDF document
            PdfDocument document = new PdfDocument(serverIP, serverPort);

            // use server password if necessary
            if (serverPassword.Length > 0)
                document.ServerPassword = serverPassword;

            // 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
            PdfFont pdfFont = document.CreateFontFromName("Times New Roman", 10, false);
            PdfFont pdfFontEmbed = document.CreateFontFromName("Times New Roman", 10, true);

            PdfFont pdfFontBoldEmbed = document.CreateFontFromName("Times New Roman", 10, true);
            pdfFontBoldEmbed.Bold = true;

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

            float startYPos = 20;
            float startXPos = 5;

            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(startXPos, startYPos,
                    "The text below extends from the layout position to the right edge of the PDF page:", pdfFontBoldEmbed);
            titleTextAtLocation.ForeColor = PdfColor.Navy;
            page1.Layout(titleTextAtLocation);

            PdfText textExpandsToRightEdge = new PdfText(0, 0, dummyText, pdfFont);
            textExpandsToRightEdge.BackColor = PdfColor.WhiteSmoke;
            document.Layout(textExpandsToRightEdge, 50, 10);

            // draw a rectangle around the text
            PdfRectangle borderPdfRectangle = new PdfRectangle(new RectangleFloat(0, 0, 0, 0));
            borderPdfRectangle.LineStyle.LineWidth = 0.5f;
            document.Layout(borderPdfRectangle);

            #endregion

            #region Layout a text with width limit

            PdfText titleTextWithWidth = new PdfText(0, 0,
                    "The text below is limited by a given width and has a free height:", pdfFontBoldEmbed);
            titleTextWithWidth.ForeColor = PdfColor.Navy;
            document.Layout(titleTextWithWidth, startXPos, false, 10, true);

            PdfText textWithWidthLimit = new PdfText(0, 0, 300, dummyText, pdfFont);
            textWithWidthLimit.BackColor = PdfColor.WhiteSmoke;
            document.Layout(textWithWidthLimit, 50, 10);


            // draw a rectangle around the text
            borderPdfRectangle = new PdfRectangle(new RectangleFloat(0, 0, 0, 0));
            borderPdfRectangle.LineStyle.LineWidth = 0.5f;
            document.Layout(borderPdfRectangle);

            #endregion

            #region Layout a text with width and height limits

            PdfText titleTextWithWidthAndHeight = new PdfText(0, 0,
                    "The text below is limited by a given width and height and is trimmed:", pdfFontBoldEmbed);
            titleTextWithWidthAndHeight.ForeColor = PdfColor.Navy;
            document.Layout(titleTextWithWidthAndHeight, startXPos, false, 10, true);

            PdfText textWithWidthAndHeightLimit = new PdfText(0, 0, 300, 50, dummyText, pdfFont);
            textWithWidthAndHeightLimit.BackColor = PdfColor.WhiteSmoke;
            document.Layout(textWithWidthAndHeightLimit, 50, 10);

            // draw a rectangle around the text
            borderPdfRectangle = new PdfRectangle(new RectangleFloat(0, 0, 0, 0));
            borderPdfRectangle.LineStyle.LineWidth = 0.5f;
            document.Layout(borderPdfRectangle);

            #endregion

            #region Layout a text with standard font

            PdfText textWithStandardFont = new PdfText(0, 0, "This green text is written with a Helvetica Standard Type 1 font", helveticaStdFont);
            textWithStandardFont.BackColor = PdfColor.WhiteSmoke;
            textWithStandardFont.ForeColor = PdfColor.Green;
            document.Layout(textWithStandardFont, startXPos, false, 10, true);

            #endregion

            #region Layout a rotated text

            PdfText titleRotatedText = new PdfText(0, 0, "The text below is rotated:", pdfFontBoldEmbed);
            titleRotatedText.ForeColor = PdfColor.Navy;
            document.Layout(titleRotatedText, startXPos, false, 10, true);

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

            PdfText rotatedCounterClockwiseText = new PdfText(0, 0, counterRotatedText, pdfFontEmbed);
            rotatedCounterClockwiseText.SetRotationAngle(-45);
            document.Layout(rotatedCounterClockwiseText, startXPos + 100, false, 150, true);

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

            PdfText rotatedClockwiseText = new PdfText(0, 0, clockwiseRotatedText, pdfFontEmbed);
            rotatedClockwiseText.SetRotationAngle(45);
            document.Layout(rotatedClockwiseText, 0, true, true, 0, true, false);

            #endregion

            #region Layout an automatically paginated text

            string dummyBigText = System.IO.File.ReadAllText(Server.MapPath("~") + @"\DemoFiles\Text\DummyBigText.txt");

            PdfText titleTextPaginated = new PdfText(0, 0,
                    "The text below is automatically paginated when it gets to the bottom of this page:", pdfFontBoldEmbed);
            titleTextPaginated.ForeColor = PdfColor.Navy;
            document.Layout(titleTextPaginated, startXPos, false, true, 140, true, false);

            PdfText paginatedText = new PdfText(0, 0, 300, dummyBigText, pdfFont);
            paginatedText.BackColor = PdfColor.WhiteSmoke;
            paginatedText.Cropping = false;
            document.Layout(paginatedText, 50, 10);

            // draw a line at the bottom of the text on the second page
            PointFloat leftPoint = new PointFloat(0, 0);
            PointFloat rightPoint = new PointFloat(300, 0);
            PdfLine borderLine = new PdfLine(leftPoint, rightPoint);
            borderLine.LineStyle.LineWidth = 0.5f;
            document.Layout(borderLine);

            #endregion

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

                // inform the browser about the binary data format
                HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");

                // let the browser know how to open the PDF document and the file name
                HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=PdfText.pdf; size={0}",
                            pdfBuffer.Length.ToString()));

                // write the PDF buffer to HTTP response
                HttpContext.Current.Response.BinaryWrite(pdfBuffer);

                // call End() method of HTTP response to stop ASP.NET page processing
                HttpContext.Current.Response.End();
            }
            finally
            {
                document.Close();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Master.SelectNode("pdfText");

                Master.LoadCodeSample("PdfTextDemo");
            }
        }
    }
}