Skip to content

Commit 2a9d2ff

Browse files
github-actions[bot]KB Botdessyordanova
authored
Added new kb article alternating-row-color-in-pdf-tables (#669)
* Added new kb article alternating-row-color-in-pdf-tables * added screenshot --------- Co-authored-by: KB Bot <[email protected]> Co-authored-by: Desislava Yordanova <[email protected]>
1 parent 7baa8e9 commit 2a9d2ff

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-0
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
---
2+
title: How to Achieve Alternating Row Color for Tables in PdfProcessing
3+
description: Learn how to achieve alternating row styles for PDF tables export using Telerik PdfProcessing.
4+
type: how-to
5+
page_title: How to Achieve Alternating Row Color for Tables in PdfProcessing
6+
meta_title: How to Achieve Alternating Row Color for Tables in PdfProcessing
7+
slug: alternating-row-color-in-pdf-tables
8+
tags: pdf, processing, telerik, document, table ,style, border, alternating, row, color, header
9+
res_type: kb
10+
ticketid: 1700632
11+
---
12+
13+
## Environment
14+
15+
| Version | Product | Author |
16+
| ---- | ---- | ---- |
17+
| 2025.3.806| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
18+
19+
## Description
20+
21+
This article shows how to generate a PDF table with styled header cells with border and background color. For the data rows an alternating row color style is applied.
22+
23+
A similar design can be produced with the demonstrated approach:
24+
25+
<img style="border: 1px solid gray;" src="images/alternating-row-color-in-pdf-tables.png" />
26+
27+
## Solution
28+
29+
To achieve **Alternating Row Style** for Tables using RadPdfProcessing, follow these steps:
30+
31+
1. Generate dummy Report data.
32+
1. Create a PDF document and generate a table using the sample data records.
33+
34+
```csharp
35+
internal class Report
36+
{
37+
public string? JobNumber { get; set; }
38+
public string? Description { get; set; }
39+
public string? Location { get; set; }
40+
public string? DocName { get; set; }
41+
public string? DocCategory { get; set; }
42+
public string? CBNumber { get; set; }
43+
public string? Name { get; set; }
44+
public string? DocType { get; set; }
45+
public string? Status { get; set; }
46+
public string? Pages { get; set; }
47+
public DateTime? StatusDate { get; set; }
48+
}
49+
50+
internal static class ReportFactory
51+
{
52+
public static List<Report> Generate(int count, int seed = 123)
53+
{
54+
var list = new List<Report>(count);
55+
var rnd = new Random(seed);
56+
string[] locations = { "New York", "Berlin", "London", "Tokyo", "Sydney" };
57+
string[] categories = { "Engineering", "Finance", "HR", "Operations", "Legal" };
58+
string[] names = { "Alice", "Bob", "Charlie", "Diana", "Evan", "Fiona" };
59+
string[] docTypes = { "PDF", "DOCX", "XLSX", "DWG" };
60+
string[] statuses = { "Draft", "In Review", "Approved", "Rejected" };
61+
62+
for (int i = 1; i <= count; i++)
63+
{
64+
list.Add(new Report
65+
{
66+
JobNumber = $"JOB-{i:0000}",
67+
Description = $"Description of job {i} with some additional dummy text to test wrapping.",
68+
Location = locations[rnd.Next(locations.Length)],
69+
DocName = $"Document_{i}",
70+
DocCategory = categories[rnd.Next(categories.Length)],
71+
CBNumber = $"CB{rnd.Next(1000, 9999)}",
72+
Name = names[rnd.Next(names.Length)],
73+
DocType = docTypes[rnd.Next(docTypes.Length)],
74+
Status = statuses[rnd.Next(statuses.Length)],
75+
Pages = rnd.Next(1, 40).ToString(),
76+
StatusDate = DateTime.Today.AddDays(-rnd.Next(0, 365))
77+
});
78+
}
79+
return list;
80+
}
81+
}
82+
83+
private static void GeneratedDummyTable()
84+
{
85+
FontBase defaultFont = FontsRepository.TimesRoman;
86+
FontFamily fontFamily = new FontFamily(defaultFont.Name);
87+
88+
int index = 0;
89+
List<Report> reports = ReportFactory.Generate(20);
90+
91+
while (index < reports.Count)
92+
{
93+
// Build table for this page
94+
var table = new Telerik.Windows.Documents.Fixed.Model.Editing.Tables.Table();
95+
table.LayoutType = TableLayoutType.AutoFit;
96+
97+
// Header row
98+
var headerRow = table.Rows.AddTableRow();
99+
100+
string[] headers = new[] {
101+
"Job Number", "Description", "Location", "Document Name",
102+
"Document Category", "CB Number", "Name", "Document Type",
103+
"Status", "Pages", "Status Date"
104+
};
105+
106+
double[] preferredWidths = new double[] {
107+
80, 300, 120, 160, 120, 80, 120, 100, 80, 50, 100
108+
};
109+
110+
for (int h = 0; h < headers.Length; h++)
111+
{
112+
var headerCell = headerRow.Cells.AddTableCell();
113+
headerCell.PreferredWidth = preferredWidths[h];
114+
var headerBlock = headerCell.Blocks.AddBlock();
115+
headerBlock.TextProperties.Font = defaultFont;
116+
headerBlock.InsertText(headers[h]);
117+
118+
// Header cell styling
119+
headerCell.Background = new RgbColor(220, 230, 241);
120+
headerCell.Borders = new TableCellBorders(
121+
new Border(1, new RgbColor(0, 0, 0)), // Top
122+
new Border(1, new RgbColor(0, 0, 0)), // Right
123+
new Border(1, new RgbColor(0, 0, 0)), // Bottom
124+
new Border(1, new RgbColor(0, 0, 0)) // Left
125+
);
126+
}
127+
128+
while (index < reports.Count)
129+
{
130+
var reportItem = reports[index];
131+
132+
var row = table.Rows.AddTableRow();
133+
var c0 = row.Cells.AddTableCell();
134+
c0.PreferredWidth = preferredWidths[0];
135+
Block block1 = c0.Blocks.AddBlock();
136+
block1.TextProperties.Font = defaultFont;
137+
block1.InsertText(reportItem.JobNumber ?? string.Empty);
138+
139+
var c1 = row.Cells.AddTableCell();
140+
c1.Blocks.AddBlock().InsertText(fontFamily, reportItem.Description ?? string.Empty);
141+
142+
var c2 = row.Cells.AddTableCell();
143+
c2.Blocks.AddBlock().InsertText(fontFamily, reportItem.Location ?? string.Empty);
144+
145+
var c3 = row.Cells.AddTableCell();
146+
c3.Blocks.AddBlock().InsertText(fontFamily, reportItem.DocName ?? string.Empty);
147+
148+
var c4 = row.Cells.AddTableCell();
149+
c4.Blocks.AddBlock().InsertText(fontFamily, reportItem.DocCategory ?? string.Empty);
150+
151+
var c5 = row.Cells.AddTableCell();
152+
c5.Blocks.AddBlock().InsertText(fontFamily, reportItem.CBNumber ?? string.Empty);
153+
154+
var c6 = row.Cells.AddTableCell();
155+
c6.Blocks.AddBlock().InsertText(fontFamily, reportItem.Name ?? string.Empty);
156+
157+
var c7 = row.Cells.AddTableCell();
158+
c7.Blocks.AddBlock().InsertText(fontFamily, reportItem.DocType ?? string.Empty);
159+
160+
var c8 = row.Cells.AddTableCell();
161+
c8.Blocks.AddBlock().InsertText(fontFamily, reportItem.Status ?? string.Empty);
162+
163+
var c9 = row.Cells.AddTableCell();
164+
c9.Blocks.AddBlock().InsertText(fontFamily, reportItem.Pages ?? string.Empty);
165+
166+
var c10 = row.Cells.AddTableCell();
167+
c10.Blocks.AddBlock().InsertText(fontFamily, reportItem.StatusDate?.ToString("dd/MM/yyyy") ?? string.Empty);
168+
169+
170+
for (var i = 0; i < row.Cells.Count; i++)
171+
{
172+
var cell = row.Cells[i];
173+
// Alternating row colors
174+
cell.Background = (index % 2 == 0) ? new RgbColor(255, 255, 255) : new RgbColor(245, 245, 245);
175+
cell.PreferredWidth = preferredWidths[i];
176+
}
177+
index++;
178+
}
179+
180+
RadFixedDocument fixedDocument = new RadFixedDocument();
181+
RadFixedPage contentPage = fixedDocument.Pages.AddPage();
182+
contentPage.Size = new Size(1400, 800);
183+
FixedContentEditor editor = new FixedContentEditor(contentPage);
184+
editor.TextProperties.FontSize = 10;
185+
editor.TextProperties.Font = defaultFont;
186+
187+
editor.Position.Translate(10, 10);
188+
editor.DrawTable(table);
189+
190+
string outputFilePath = "output.pdf";
191+
File.WriteAllBytes(outputFilePath, new PdfFormatProvider().Export(fixedDocument, TimeSpan.FromSeconds(10)));
192+
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
193+
}
194+
}
195+
```
196+
197+
198+
## See Also
199+
200+
- [ Tables in PdfProcessing]({%slug radpdfprocessing-editing-table-overview%})
201+
130 KB
Loading

libraries/radpdfprocessing/editing/table/overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,5 @@ As of **Q3 2024**, along with the BorderStyle.*Single*, RadPdfProcessing offers
199199
* [Implementing Column Span in RadPdfProcessing Tables]({%slug table-column-span-radpdfprocessing%})
200200
* [Generating a Table with RadFixedDocumentEditor]({%slug generate-table-with-radfixeddocumenteditor%})
201201
* [Avoiding Table Splits Across Pages Using FixedContentEditor in RadPdfProcessing]({%slug avoid-table-splits-across-pages-radpdfprocessing%})
202+
* [How to Achieve Alternating Row Color for Tables in PdfProcessing]({%slug alternating-row-color-in-pdf-tables%})
202203

0 commit comments

Comments
 (0)