Creating stylish PDF grids with colored text backgrounds using .NET Core
The Syncfusion Essential® PDF is a robust .NET PDF library for creating, editing, and managing PDF documents programmatically. This tutorial will guide you through adding text with a colored background in a PDF grid using C#, specifically creating white text on a blue background.
Steps to generate text with a background in PDF grid
- Create a Console Application: Set up a new console application project.
- Install Syncfusion® Package: Add the Syncfusion.Pdf.Net.Core NuGet package to your console application
- Include Required Namespaces: Add the following namespaces in
Program.cs
.
C#
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Grid;
- Implementation to Add Text with a Background: Use the following code to style the PDF grid with custom text and background colors
C#
// Create a new PDF document.
PdfDocument document = new PdfDocument();
// Add a page.
PdfPage page = document.Pages.Add();
// Create a PdfGrid.
PdfGrid pdfGrid = new PdfGrid();
// Add values to the list.
List<object> data = new List<object>
{
new { ID = "E01", Name = "Clay" },
new { ID = "E02", Name = "Thomas" },
new { ID = "E03", Name = "John" }
};
// Assign data source.
pdfGrid.DataSource = data;
// Customize cell styles for the grid.
PdfGridCellStyle cellStyle = new PdfGridCellStyle();
// Set background color to blue.
cellStyle.BackgroundBrush = PdfBrushes.Blue;
// Set text color to white.
cellStyle.TextBrush = PdfBrushes.White;
// Set font and size.
cellStyle.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12f);
// Apply cell style to all cells in the grid.
foreach (PdfGridRow row in pdfGrid.Rows)
{
row.ApplyStyle(cellStyle);
}
// Apply cell style to the header row.
pdfGrid.Headers[0].ApplyStyle(cellStyle);
// Draw the grid to the page of PDF document.
pdfGrid.Draw(page, new Syncfusion.Drawing.PointF(10, 10));
// Create the stream object.
MemoryStream stream = new MemoryStream();
// Save the PDF document to stream.
document.Save(stream);
// Close the document.
document.Close(true);
// Write the PDF document to disk.
File.WriteAllBytes("output.pdf", stream.ToArray());
A complete working sample can be downloaded from White_text_in_blue_background.zip
By executing the program, the output PDF document will be generated as shown below.
Take a moment to explore the documentation on working with tables, where you can find additional features such as grid pagination and advanced grid customization options.
Conclusion
I hope you enjoyed learning about how to generate text with a background in PDF grid using C#.
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!