How to draw multiline text in PDF using C# and VB.NET
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:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the 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
- 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 document 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 document 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.
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 a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about How to draw multiline text in PDF using C# and VB.NET.
You can refer to our PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!