BoldDeskWe are launching BoldDesk on Product Hunt soon. Learn more & follow us.
Hi,
Can someone help me to style a PDF Grid background with alternating colors(Example: Row 1:White, Row 2: Blue Row 3:WHite and likewise)
The Syncfusion .NET PDF library supports adding different styles for a particular table cell using the BeginCellLayout event in the PdfGrid class.
Please refer to the below code snippet:
PdfDocument pdfDocument = new PdfDocument(); PdfPage pdfPage = pdfDocument.Pages.Add(); PdfGraphics graphics = pdfPage.Graphics; //Create a new PdfGrid. PdfGrid pdfGrid = new PdfGrid(); pdfGrid.BeginCellLayout += PdfGrid_BeginCellLayout; //Add three columns. pdfGrid.Columns.Add(3); //Add header. pdfGrid.Headers.Add(1); pdfGrid.RepeatHeader = true; PdfGridRow pdfGridHeader = pdfGrid.Headers[0]; pdfGridHeader.Cells[0].Value = "Employee ID"; pdfGridHeader.Cells[1].Value = "Employee Name"; pdfGridHeader.Cells[2].Value = "Salary"; for (int i = 0; i < 850; i++) { //Add rows. PdfGridRow pdfGridRow = pdfGrid.Rows.Add(); pdfGridRow.Cells[0].Value = "Employe 1 "; pdfGridRow.Cells[1].Value = "John"; pdfGridRow.Cells[2].Value = "$10,000"; } //Draw the PdfGrid. pdfGrid.Draw(pdfPage, PointF.Empty); MemoryStream ms = new MemoryStream(); //Save the document. pdfDocument.Save(ms); //Console.Read(); ms.Position = 0; File.WriteAllBytes("Output.pdf", ms.ToArray()); //Close the document pdfDocument.Close(true); static void PdfGrid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args) { //Draw multiple fonts for a particular table cell. if (args.CellIndex == 2 && args.RowIndex==0) { args.Graphics.DrawRectangle(PdfBrushes.Red, new RectangleF(args.Bounds.X, args.Bounds.Y, args.Bounds.Width, args.Bounds.Height)); } if (args.CellIndex == 0 && args.RowIndex==1) { args.Style.BackgroundBrush = PdfBrushes.Green; args.Style.TextBrush = PdfBrushes.White; } if (args.CellIndex == 1) { args.Style.TextBrush = PdfBrushes.Blue; } } |
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Net6.0Sample-1946623558
Please follow the below links for more information,
https://www.syncfusion.com/downloads/support/directtrac/general/ze/Net6.0Sample-1946623558
Thank you Irfana, appreciate it.