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.
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.
private void buttonCreatePdf_Click(object sender, EventArgs e) { // create a PDF document PdfDocument document = new PdfDocument(); // create a page in document PdfPage page1 = document.AddPage(); // create the true type fonts that can be used in document text Font sysFont = new Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point); PdfFont pdfFont = document.CreateFont(sysFont); PdfFont pdfFontEmbed = document.CreateFont(sysFont, true); Font sysFontBold = new Font("Times New Roman", 10, 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 = 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 = 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 = 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 = 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 = 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 = 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 = Color.WhiteSmoke; textWithStandardFont.ForeColor = 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 = 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 Bitmap refBmp = new Bitmap(1, 1); Graphics refGraphics = Graphics.FromImage(refBmp); refGraphics.PageUnit = GraphicsUnit.Point; string counterRotatedText = "This text is rotated 45 degrees counter clockwise"; // measure the rotated text size 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 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(Application.StartupPath + @"\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 = 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 = 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 PointF leftPoint = new PointF(textLayoutInfo.LastPageRectangle.Left, textLayoutInfo.LastPageRectangle.Bottom); PointF rightPoint = new 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 Cursor = Cursors.WaitCursor; string pdfFile = Application.StartupPath + @"\DemoOutput\PdfText.pdf"; try { document.WriteToFile(pdfFile); } catch (Exception ex) { MessageBox.Show(String.Format("Cannot create the PDF document. {0}", ex.Message)); return; } finally { document.Close(); Cursor = Cursors.Arrow; } // open the created PDF document try { System.Diagnostics.Process.Start(pdfFile); } catch (Exception ex) { MessageBox.Show(String.Format("The PDF document was created but cannot open '{0}'. {1}", pdfFile, ex.Message)); } }