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.
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 HiQPdfClient;
using Microsoft.AspNetCore.Hosting.Server;
namespace HiQPdf_Demo.Controllers
{
public class PdfTextDemoController : Controller
{
IWebHostEnvironment m_hostingEnvironment;
public PdfTextDemoController(IWebHostEnvironment hostingEnvironment)
{
m_hostingEnvironment = hostingEnvironment;
}
// GET: PdfTextDemo
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult CreatePdf(IFormCollection collection)
{
string serverIP = collection["textBoxServerIP"];
uint serverPort = uint.Parse(collection["textBoxServerPort"]);
string serverPassword = collection["textBoxServerPassword"];
// 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(m_hostingEnvironment.WebRootPath + "/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();
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "PdfText.pdf";
return fileResult;
}
finally
{
document.Close();
}
}
}
}