How to Adjust Text Size to Fit Cell Size in ASP.NET Core PDF Grid?
The Syncfusion Essential PDF is a feature-rich and high-performance ASP.NET Core PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. Using this library, you can adjust the text size to fit the cell width in PdfGrid using
Steps to adjust the text size to fit cell size in PdfGrid programmatically:
- Create a new console application project.
- Install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your console application from Nuget.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf;
using Syncfusion.Pdf.Grid;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Drawing;
- Use the following code sample in Program.cs to adjust the text size to fit the cell width in PdfGrid.
C#
// Create a new PDF document.
PdfDocument document = new PdfDocument();
// Add a page to the document.
PdfPage page = document.Pages.Add();
// Create a PdfGrid.
PdfGrid grid = new PdfGrid();
// Set the font for the grid.
grid.Style.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
// Add three columns to the grid.
grid.Columns.Add(3);
// Set the width of each column.
for (int i = 0; i < 3; i++)
{
grid.Columns[i].Width = page.GetClientSize().Width / 3;
}
// Set up the header cells.
PdfGridRow pdfGridHeader = grid.Headers.Add(1)[0];
pdfGridHeader.Cells[0].Value = "Employee ID";
pdfGridHeader.Cells[1].Value = "Employee Name";
pdfGridHeader.Cells[2].Value = "Details";
// Add rows to the grid.
for (int i = 1; i <= 5; i++)
{
PdfGridRow row = grid.Rows.Add();
row.Height = 20;
row.Cells[0].Value = "E0" + i;
row.Cells[1].Value = "Employee " + i;
row.Cells[2].Value = String.Format ("Employee {0} is a software engineer with over ten years of experience in developing scalable applications. He specializes in full-stack development and enjoys working on innovative projects.", i);
}
// Adjust the font size to fit the cell content.
AdjustFontSize(grid);
// Draw the PdfGrid on the page.
grid.Draw(page, PointF.Empty);
// Save and close the document.
using (FileStream stream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
document.Save(stream);
}
document.Close(true);
// Method to adjust the font size to fit the cell content.
public static void AdjustFontSize(PdfGrid grid)
{
for (int i = 0; i < grid.Rows.Count; i++)
{
for (int j = 0; j < grid.Columns.Count; j++)
{
// Get the cell.
PdfGridCell cell = grid.Rows[i].Cells[j];
// Get the cell font.
PdfFont initialFont = cell.Style.Font ?? grid.Style.Font;
// Get the cell value.
string text = cell.Value?.ToString() ?? string.Empty;
// Get the cell size.
SizeF cellSize = new SizeF(grid.Columns[j].Width, grid.Rows[i].Height);
// Get the initial font size.
float fontSize = initialFont.Size;
PdfFont currentFont = initialFont;
while (fontSize > 0)
{
// Measure the text.
SizeF textSize = currentFont.MeasureString(text, cellSize.Width);
if (textSize.Height <= cellSize.Height)
{
cell.Style.Font = currentFont;
break;
}
fontSize--;
if (initialFont is PdfStandardFont)
{
currentFont = new PdfStandardFont(initialFont as PdfStandardFont, fontSize);
}
else
{
currentFont = new PdfTrueTypeFont(initialFont as PdfTrueTypeFont, fontSize);
}
}
}
}
}
A complete working sample can be downloaded from GitHub.
Take a moment to peruse the documentation on adjusting text size to fit cell width in PdfGrid., where you will find various options for Row customization, Cell customization, Table customization.
Conclusion
I hope you enjoyed learning about how to adjust text size to fit cell size in ASP.NET Core PDF Grid.
You can refer to our ASP.NET Core 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 ASP.NET Core 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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!