)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to apply word wrap in PDF table cell using C# and VB.NET

Platform: WinForms |
Control: PDF |
Published Date: November 26, 2018 |
Last Revised Date: April 29, 2019

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 PDF table cell using C# and VB.NET.

Steps to apply word wrap in PDF table cell 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 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

 

  1. 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. Screenshot of output PDF file

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.

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.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon