Articles in this section
Category / Section

How to draw multiline text in PDF using C# and VB.NET

3 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can draw multiline text in PDF using C# and VB.NET.

Steps to draw multiline text in PDF programmatically:

  1. Create a new C# Windows Forms application project. Create new Windows forms application
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. Install nuget packages
  3. Include the following namespaces in Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.Drawing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports System.Drawing

 

  1. Use the following code snippet to draw multiline text in a PDF file.

C#

//Create a new PDF document
PdfDocument document = new PdfDocument();
//Add a page to the document
PdfPage page = document.Pages.Add();
//Multi-line text to draw
string text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
//Create a text element
PdfTextElement element = new PdfTextElement(text);
element.Brush = new PdfSolidBrush(Color.Black);
element.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
//Set the properties to paginate the text
PdfLayoutFormat layoutFormat = new PdfLayoutFormat();
layoutFormat.Break = PdfLayoutBreakType.FitPage;
//Set bounds to draw multiline text
RectangleF bounds = new RectangleF(PointF.Empty, page.Graphics.ClientSize);
//Draw the text element with the properties and formats set
element.Draw(page, bounds, layoutFormat);
//Save and close the docuemnt
document.Save("MultipleLineText.pdf");
document.Close(true);

 

VB.NET

'Create a new PDF document
Dim document As New PdfDocument()
'Add a page to the document
Dim page As PdfPage = document.Pages.Add()
'Multi-line text to draw
Dim text As String = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
'Create a text element
Dim element As New PdfTextElement(text)
element.Brush = New PdfSolidBrush(Color.Black)
element.Font = New PdfStandardFont(PdfFontFamily.Helvetica, 12)
'Set the properties to paginate the text
Dim layoutFormat As New PdfLayoutFormat()
layoutFormat.Break = PdfLayoutBreakType.FitPage
'Set bounds to draw multiline text
Dim bounds As New RectangleF(PointF.Empty, page.Graphics.ClientSize)
'Draw the text element with the properties and formats set
element.Draw(page, bounds, layoutFormat)
'Save and close the docuemnt
document.Save("MultipleLineText.pdf")
document.Close(True)

 

A complete working sample can be downloaded from DrawMultiLineText.zip.

By executing the program, you will get the PDF document as follows. screenshot of output PDF file

Take a moment to peruse the documentation for working with Text, where you can find the other options like drawing right-to-left text, multicolumn text, consuming TrueType fonts, Standards fonts, and CJK fonts.

Refer here to explore the rich set of Syncfusion Essential PDF features.

An online sample link to create PDF document with large text.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied