How to apply word wrap in WinForms PDF table cell 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 apply word wrap in our WinForms PDF table cell using C# and VB.NET.
Steps to apply word wrap in PDF table cell programmatically:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Form1.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Grid; using Syncfusion.Pdf.Graphics; using System.Drawing; using System.Data;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Grid Imports Syncfusion.Pdf.Graphics Imports System.Drawing Imports System.Data
- Use the following code snippet in onclick event of button to apply word wrap in PDF table cell.
C#
//Create a new PDF document PdfDocument doc = new PdfDocument(); //Add a page PdfPage page = doc.Pages.Add(); //Create a PdfGrid PdfGrid grid = new PdfGrid(); //Create a DataTable DataTable dataTable = new DataTable(); //Add columns to the DataTable dataTable.Columns.Add("CustomerID"); dataTable.Columns.Add("CompanyName"); dataTable.Columns.Add("ContactName"); dataTable.Columns.Add("Address"); dataTable.Columns.Add("City"); dataTable.Columns.Add("PostalCode"); dataTable.Columns.Add("Country"); //Add rows to the DataTable dataTable.Rows.Add(new object[] { "ALFKI", "Alfreds Futterkiste", "Maria Anders", "Obere Str. 57", "Berlin", "12209", "Germany" }); dataTable.Rows.Add(new object[] { "ANATR", "Ana Trujillo Emparedados yhelados", "Ana Trujillo", "Avda. de la Constitución 2222", "México D.F.", "05021", "Mexico" }); dataTable.Rows.Add(new object[] { "ANTON", "Antonio Moreno Taquería", "Antonio Moreno", "Mataderos 2312", "México D.F.", "05023", "Mexico" }); dataTable.Rows.Add(new object[] { "BLAUS", "Blauer See Delikatessen", "Hanna Moos", "Forsterstr. 57", "Mannheim", "68306", "Germany" }); dataTable.Rows.Add(new object[] { "DRACD", "Drachenblut Delikatessen", "Sven Ottlieb", "Walserweg 21", "Aachen", "52066", "Germany" }); //Assign data source grid.DataSource = dataTable; //Create a new instance for string format PdfStringFormat format = new PdfStringFormat(); format.WordWrap = PdfWordWrapType.Word; //Set word wrapping in PDF table cell foreach (PdfGridRow row in grid.Headers) { foreach(PdfGridCell cell in row.Cells) { cell.Style.StringFormat = format; } } foreach (PdfGridRow row in grid.Headers) { foreach (PdfGridCell cell in row.Cells) { cell.Style.StringFormat = format; } } //Draw grid to the resultant page of the first grid grid.Draw(page, new PointF(10, 20)); //Save and the document doc.Save("WordWrapInTableCell.pdf"); doc.Close(true);
VB.NET
'Create a new PDF document Dim doc As PdfDocument = New PdfDocument() 'Add a page Dim page As PdfPage = doc.Pages.Add() 'Create a PDF Grid Dim grid As PdfGrid = New PdfGrid() 'Create a DataTable Dim dataTable As DataTable = New DataTable() 'Add columns to the DataTable dataTable.Columns.Add("CustomerID") dataTable.Columns.Add("CompanyName") dataTable.Columns.Add("ContactName") dataTable.Columns.Add("Address") dataTable.Columns.Add("City") dataTable.Columns.Add("PostalCode") dataTable.Columns.Add("Country") 'Add rows to the DataTable dataTable.Rows.Add(New Object() {"ALFKI", "Alfreds Futterkiste", "Maria Anders", "Obere Str. 57", "Berlin", "12209", "Germany"}) dataTable.Rows.Add(New Object() {"ANATR", "Ana Trujillo Emparedados yhelados", "Ana Trujillo", "Avda. de la Constitución 2222", "México D.F.", "05021", "Mexico"}) dataTable.Rows.Add(New Object() {"ANTON", "Antonio Moreno Taquería", "Antonio Moreno", "Mataderos 2312", "México D.F.", "05023", "Mexico"}) dataTable.Rows.Add(New Object() {"BLAUS", "Blauer See Delikatessen", "Hanna Moos", "Forsterstr. 57", "Mannheim", "68306", "Germany"}) dataTable.Rows.Add(New Object() {"DRACD", "Drachenblut Delikatessen", "Sven Ottlieb", "Walserweg 21", "Aachen", "52066", "Germany"}) 'Assign data source grid.DataSource = dataTable 'Create a new instance for string format Dim format As New PdfStringFormat() format.WordWrap = PdfWordWrapType.Word 'Set word wrapping in PDF table cell For Each row As PdfGridRow In Grid.Headers For Each cell As PdfGridCell In row.Cells cell.Style.StringFormat = format Next Next For Each row As PdfGridRow In Grid.Headers For Each cell As PdfGridCell In row.Cells cell.Style.StringFormat = format Next Next 'Draw grid grid.Draw(page, New PointF(10, 20)) 'Save and close the document doc.Save("WordWrapInTableCell.pdf") doc.Close(True)
A complete working sample can be downloaded from WordWrapInPDFTableCell.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation for working with tables, where you will find other options like grid pagination and different levels of grid customization.
Refer here to explore the rich set of Syncfusion Essential® PDF features.
An online sample link to create a richly formatted table.
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.
Conclusion
I hope you enjoyed learning about how to apply word wrap in PDF table cell using C# and VB.NET.
You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms PDF 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, Direct-Trac, or feedback portal. We are always happy to assist you!