Creating a Dashed Line Border for a Table in RadPdfProcessing
Environment
| Version | Product | Author |
|---|---|---|
| 2024.1.124 | RadPdfProcessing | Desislava Yordanova |
Description
Learn how to add a dashed line border in a table using RadPdfProcessing.
Solution
To create a dashed line border for a Table in RadPdfProcessing, you can follow these steps:
- Set the desired font style properties for the table.
- Create a
Borderobject with the desired thickness, style, and color. - Set the
Bordersproperty of the table'sDefaultCellPropertiesto the createdBorderobject. - Set the
Paddingproperty of the table'sDefaultCellPropertiesto the desired padding values. - Add rows and cells to the table and set the preferred width for each cell.
- Insert text into each cell using the desired font style properties.
- Use a FixedContentEditor to draw the table on the document page.
- Specify the desired dashed line style by setting the
StrokeDashArrayproperty of theGraphicPropertiesof theFixedContentEditor.
Here is a sample code snippet that demonstrates how to create a dashed line border for a table in RadPdfProcessing:
FontFamily fFamily = new FontFamily("Verdana");
RadFixedDocument fixedDocument = new RadFixedDocument();
Table table = new Table();
int thickness = 1;
RgbColor borderColor = new RgbColor(255, 0, 0);
Border b = new Border(thickness, BorderStyle.Single, borderColor);
table.DefaultCellProperties.Borders = new TableCellBorders(b,b,b,b);
table.DefaultCellProperties.Padding = new Thickness(2, 2, 2, 2);
TableRow r1 = table.Rows.AddTableRow();
TableCell firstCell = r1.Cells.AddTableCell();
firstCell.PreferredWidth = 200;
firstCell.Blocks.AddBlock().InsertText(fFamily, FontStyles.Normal, FontWeights.ExtraBold, "Telerik");
TableCell secondCell = r1.Cells.AddTableCell();
secondCell.PreferredWidth = 150;
secondCell.Blocks.AddBlock().InsertText(fFamily, FontStyles.Normal, FontWeights.Regular, "Document");
TableCell thirdCell = r1.Cells.AddTableCell();
thirdCell.PreferredWidth = 100;
thirdCell.Blocks.AddBlock().InsertText(fFamily, FontStyles.Normal, FontWeights.Bold, "Processing");
FixedContentEditor fixedEditor = new FixedContentEditor(fixedDocument.Pages.AddPage());
fixedEditor.GraphicProperties.IsFilled = true;
fixedEditor.GraphicProperties.StrokeDashArray = new double[] { 2,2};
fixedEditor.Position.Translate(10, 100);
fixedEditor.DrawTable(table);
string outputFilePath = @"..\..\output.pdf";
File.Delete(outputFilePath);
PdfFormatProvider provider = new PdfFormatProvider();
using (Stream output = File.OpenWrite(outputFilePath))
{
provider.Export(fixedDocument, output);
}
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
Please note that you can modify the Borders property of the DefaultCellProperties to specify different border styles for each side of the cell or render a border only at the bottom:
table.DefaultCellProperties.Borders = new TableCellBorders(null, null, null,b);
As of Q3 2024 RadPdfProcessing offers Dotted, Dashed, and DashSmallGap border styles out-of-the-box without the necessity to play with the StrokeDashArray of the FixedContentEditor. With this update, the Dotted, Dashed, DashSmallGap, and Thick border lines are now exported from RadFlowDocument to RadFixedDocument as well.