Articles in this section
Category / Section

How to adjust PDF table row width based on the text length using C# and VB.NET?

5 mins read

Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can adjust PDF table row width based on the text length by enabling the AllowHorizontalOverflow property in the PDF document using C# and VB.NET.

Steps to adjust PDF table row width based on the text length 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 the click event of button to adjust PDF table row width based on the text length.

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();
//Enable the horizontal overflow property to set table width based on the text width
grid.Style.AllowHorizontalOverflow = true;
//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;
//Draw grid in PDF document
grid.Draw(page, new PointF(10, 20));
//Save and close the document
doc.Save("TableWidthBasedOnTextWidth.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()
grid.Style.AllowHorizontalOverflow = True
'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
'Assign data source
grid.DataSource = dataTable
'Draw grid
grid.Draw(page, New PointF(10, 20))
'Save and close the document
doc.Save("TableWidthBasedOnTextWidth.pdf")
doc.Close(True)

 

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

By executing the program, you will get the PDF document as follows. Output PDF document

Take a moment to peruse the documentation for working with tables, where you will find other soptions 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.

 

Conclusion

I hope you enjoyed learning about how to adjust PDF table row width based on the text length using C# and VB.NET.

You can refer to our .NET PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET PDF example 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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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