We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

setting the text color of a column in pdfLightTable

Hello,

I need to change the column text color, for each column a different color.
I can not find the right properties for this.
I have tried the column properties
I have tried the pdfLightTable_BeginCellLayout

What I have:

               PdfLoadedDocument doc = new PdfLoadedDocument(_BeginBestand);

                //Get first page from document
                PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;


                // Create a PdfLightTable.
                PdfLightTable pdfLightTable = new PdfLightTable();

                //Assign data source.
                DataTable AndereVolgorde = new DataTable();
                // here the table gets filled.

                pdfLightTable.DataSource = AndereVolgorde;


                pdfLightTable.BeginCellLayout += new BeginCellLayoutEventHandler(pdfLightTable_BeginCellLayout);


                #region Cell Styles

                //Create Pdf pen for drawing broder
                PdfPen borderPen = new PdfPen(PdfBrushes.DarkBlue);
                borderPen.Width = 0;

                PdfColor zwart = new PdfColor(0, 0, 0);
                PdfSolidBrush zwarte_brush = new PdfSolidBrush(zwart);

                //Create cell styles
                PdfCellStyle altStyle = new PdfCellStyle();
                altStyle.Font = font;
                altStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(192, 201, 219));
                altStyle.BorderPen = borderPen;
                altStyle.TextBrush = zwarte_brush;

                PdfCellStyle defStyle = new PdfCellStyle();
                defStyle.Font = font;
                defStyle.BackgroundBrush = PdfBrushes.White;
                defStyle.BorderPen = borderPen;
                defStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);

                PdfCellStyle headerStyle = new PdfCellStyle(font, PdfBrushes.White, PdfPens.DarkBlue);
                headerStyle.BackgroundBrush = new PdfSolidBrush(Color.FromArgb(33, 67, 126));
                headerStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

                #endregion

                #region Format Table

                ////Set table alternate row style
                pdfLightTable.Style.AlternateStyle = altStyle;

                ////Set default style
                pdfLightTable.Style.DefaultStyle = defStyle;

                //Set header row style        
                pdfLightTable.Style.HeaderStyle = headerStyle;

                //Show the header row
                pdfLightTable.Style.ShowHeader = true;

                //Repeate header in all the pages
                pdfLightTable.Style.RepeatHeader = true;

                //Set header data from column caption
                pdfLightTable.Style.HeaderSource = PdfHeaderSource.ColumnCaptions;

                pdfLightTable.Style.BorderPen = borderPen;
                pdfLightTable.Style.CellPadding = 2;

                pdfLightTable.Columns[0].Width = 3;
                pdfLightTable.Columns[1].Width = 8;

// Maybe here?

                #endregion





                //Draw PdfLightTable.
                pdfLightTable.Draw(graphics, new PointF(10, 200));

                //Save the document.
                doc.Save(_EindBestand);

                //Close the document
                doc.Close(true);


AND:

        private void pdfLightTable_BeginCellLayout(object sender, BeginCellLayoutEventArgs args)
        {

            float x = args.Bounds.X;
            float y = args.Bounds.Y;
            float width = args.Bounds.Right;
            float height = args.Bounds.Bottom;


            try
            {
                if (args.Value.Contains("http"))
                {

                    args.Skip = true;

                    PdfTextWebLink textLink = new PdfTextWebLink();
                    textLink.Url = args.Value;
                    textLink.Text =  args.Value;
                    textLink.Brush = PdfBrushes.Black;                                   
                    textLink.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 10);
                    textLink.DrawTextWebLink(args.Graphics, new PointF(x, y));
                   
                }



// second column
                if (args.CellIndex == 1)
                {
                    PdfColor Bijna_zwart = new PdfColor(192, 201, 219);
                    PdfPen Bijna_zwarte_pen = new PdfPen(Bijna_zwart);

                    PdfCellStyle cel = new PdfCellStyle();
                    cel.TextPen = Bijna_zwarte_pen;

// how do I connect the cel to args?

                }


            }
            catch (System.Exception ex)
            {
                FoutOmschrijving = "Er is een fout opgetreden in de functie pdfLightTable_BeginCellLayout()." + System.Environment.NewLine + ex.Message;
               // Debug.Print(ex.Message);
            }

        }



Thank you in advance.
Brian

5 Replies

SK Sasi Kumar Sekar Syncfusion Team November 29, 2016 11:34 AM UTC

Hi Brain, 
 
Thank you for contacting Syncfusion support, 

We have analyzed your requirements and the given code snippet, currently we do not have support for binding the cell style values from the BeginCellEvent. We can achieve different text color for each column by using the BeginCellEvent. Please find the below code snippet to draw the alternative text color for alternative column.  
 
Code snippet: 
 
//BeginCell Event metod. 
private void pdfLightTable_BeginCellLayout(object sender, BeginCellLayoutEventArgs args) 
{      
    //Check the even cells. 
    if (args.CellIndex % 2 == 0) 
    { 
       //Draw the alternative table cell with the alternative color. 
       args.Graphics.DrawString(args.Value, font, PdfPens.Red, args.Bounds); 
       args.Skip = true; 
    }      
} 
 
Please let us know if you need further assistance. 
 
Regards. 
Sasi Kumar S. 



BB Brian Baart replied to Sasi Kumar Sekar December 2, 2016 10:35 AM UTC

Hi Brain, 
 
Thank you for contacting Syncfusion support, 

We have analyzed your requirements and the given code snippet, currently we do not have support for binding the cell style values from the BeginCellEvent. We can achieve different text color for each column by using the BeginCellEvent. Please find the below code snippet to draw the alternative text color for alternative column.  
 
Code snippet: 
 
//BeginCell Event metod. 
private void pdfLightTable_BeginCellLayout(object sender, BeginCellLayoutEventArgs args) 
{      
    //Check the even cells. 
    if (args.CellIndex % 2 == 0) 
    { 
       //Draw the alternative table cell with the alternative color. 
       args.Graphics.DrawString(args.Value, font, PdfPens.Red, args.Bounds); 
       args.Skip = true; 
    }      
} 
 
Please let us know if you need further assistance. 
 
Regards. 
Sasi Kumar S. 


Dear Sir,

Thank you for your help, but I can't lose the back color text what ever I try.
Could I ask for your assistane again?
This is what I have.

What I have tried:
Lose the text color in both the styles.
Force some silly color in the extra functie BeginCellLayoutEventHandler
with brush and with pen.
But the column text is still in black.
Here is what I have now:


 public void AanmakenPDF()
        {

            try
            {

                FoutOmschrijving = "";

                PdfLoadedDocument doc = new PdfLoadedDocument(_BeginBestand);

                //Get first page from document
                PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage;

                //Create PDF graphics for the page
                PdfGraphics graphics = page.Graphics;

                PdfFont fontAdres = new PdfStandardFont(PdfFontFamily.Helvetica, 12);

                //Draw the text.
                graphics.DrawString(Debiteur.ADRES, fontAdres, PdfBrushes.Black, new PointF(10, 120));

                PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10);

                //Draw the text.
                graphics.DrawString(_AutodocForm, font, PdfBrushes.White, new PointF(250, 120));
                graphics.DrawString(Debiteur.EMAIL, font, PdfBrushes.White, new PointF(250, 140));
                graphics.DrawString(Debiteur.NAAM, font, PdfBrushes.WhiteSmoke, new PointF(250, 160));

                // Create a PdfLightTable.
                PdfLightTable pdfLightTable = new PdfLightTable();

                //Assign data source.

                DataTable AndereVolgorde = new DataTable();
                AndereVolgorde.Columns.Add("NR");
                AndereVolgorde.Columns.Add("OMSCHRIJVING");
                AndereVolgorde.Columns.Add("LINK");

                foreach (DataRow rij in Table.Rows)
                {
                    DataRow _rij = AndereVolgorde.NewRow();
                    _rij["NR"] = rij["TOELATINGSNR"];
                    _rij["OMSCHRIJVING"] = rij["OMSCHRIJVING"];
                    _rij["LINK"] = rij["LINK"];
                    AndereVolgorde.Rows.Add(_rij);

                }

                pdfLightTable.DataSource = AndereVolgorde;



                #region Cell Styles

                //Create Pdf pen for drawing broder
                PdfPen borderPen = new PdfPen(PdfBrushes.DarkBlue);
                borderPen.Width = 0;

                PdfColor zwart = new PdfColor(33, 67, 126);
                PdfSolidBrush zwarte_brush = new PdfSolidBrush(zwart);

                PdfPen TekstPen = new PdfPen(zwart);

                //Create cell styles
                PdfCellStyle altStyle = new PdfCellStyle();
                altStyle.Font = font;
                altStyle.BackgroundBrush = new PdfSolidBrush(new PdfColor(192, 201, 219));
                altStyle.BorderPen = borderPen;
                //altStyle.TextBrush = zwarte_brush;
                //altStyle.TextPen = TekstPen;
                altStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);

                PdfCellStyle defStyle = new PdfCellStyle();
                defStyle.Font = font;
                defStyle.BackgroundBrush = PdfBrushes.White;
                defStyle.BorderPen = borderPen;
                //defStyle.TextBrush = zwarte_brush;
                //defStyle.TextPen = TekstPen;
                defStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);

                PdfCellStyle headerStyle = new PdfCellStyle(font, PdfBrushes.White, PdfPens.DarkBlue);
                headerStyle.BackgroundBrush = new PdfSolidBrush(Color.FromArgb(33, 67, 126));
                headerStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

                #endregion

                #region Format Table

                //Set table alternate row style
                pdfLightTable.Style.AlternateStyle = altStyle;

                //Set default style
                pdfLightTable.Style.DefaultStyle = defStyle;

                //Set header row style         
                pdfLightTable.Style.HeaderStyle = headerStyle;

                //Show the header row
                pdfLightTable.Style.ShowHeader = true;

                //Repeate header in all the pages
                pdfLightTable.Style.RepeatHeader = true;

                //Set header data from column caption
                pdfLightTable.Style.HeaderSource = PdfHeaderSource.ColumnCaptions;

                pdfLightTable.Style.BorderPen = borderPen;
                pdfLightTable.Style.CellPadding = 2;

                pdfLightTable.Columns[0].Width = 2;
                pdfLightTable.Columns[1].Width = 8;


                #endregion


                pdfLightTable.BeginCellLayout += new BeginCellLayoutEventHandler(pdfLightTable_BeginCellLayout);


                //Draw PdfLightTable.
                pdfLightTable.Draw(graphics, new PointF(10, 200));

                //Save the document.
                doc.Save(_EindBestand);

                //Close the document
                doc.Close(true);

            }
            catch (System.Exception ex)
            {
                FoutOmschrijving = "Er is een fout opgetreden in de functie AanmakenPDF()." + System.Environment.NewLine + ex.Message;
                //Debug.Print(ex.Message);
            }

        }


        private void pdfLightTable_BeginCellLayout(object sender, BeginCellLayoutEventArgs args)
        {

            float x = args.Bounds.X;
            float y = args.Bounds.Y;
            float width = args.Bounds.Right;
            float height = args.Bounds.Bottom;


            try
            {

                

                if (args.Value.Contains("http"))
                {

                    
                    PdfTextWebLink textLink = new PdfTextWebLink();
                    textLink.Url = args.Value;
                    textLink.Text = args.Value;
                    textLink.Brush = PdfBrushes.Black;
                    textLink.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 10);
                    textLink.DrawTextWebLink(args.Graphics, new PointF(x, y));
                    args.Skip = true;

                }

                if (args.CellIndex == 0)
                {

                    PdfColor zwart = new PdfColor(10, 10, 0);
                    PdfPen zwarte_pen = new PdfPen(zwart);
                    PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10);
                    args.Graphics.DrawString(args.Value, font, zwarte_pen, args.Bounds);

                }


                if (args.CellIndex == 1)
                {

                    PdfColor Bijna_zwart = new PdfColor(33, 67, 126);
                    PdfPen Bijna_zwarte_pen = new PdfPen(Bijna_zwart);

                    PdfSolidBrush kleur = new PdfSolidBrush(Bijna_zwart);
                    PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10);
                    args.Graphics.DrawString(args.Value, font, kleur, args.Bounds);

                }


                if (args.CellIndex == 2)
                {

                    PdfColor Geen_zwart = new PdfColor(30, 30, 2);
                    PdfPen Geen_zwarte_pen = new PdfPen(Geen_zwart);

                    PdfSolidBrush kleur = new PdfSolidBrush(Geen_zwart);
                    PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10);
                    args.Graphics.DrawString(args.Value, font, kleur, args.Bounds);


                }



            }
            catch (System.Exception ex)
            {
                FoutOmschrijving = "Er is een fout opgetreden in de functie pdfLightTable_BeginCellLayout()." + System.Environment.NewLine + ex.Message;
                // Debug.Print(ex.Message);
            }





        }

Thanks in advance.
Brian






BB Brian Baart December 2, 2016 10:51 AM UTC

Dear Sir,

Added info:

If I use: args.Skip = true;
Then I get the error that there is not enough space to print the table.

If I use your code:
args.Graphics.DrawString(args.Value, font, PdfPens.Red, args.Bounds);
It still prints black text.

With kind regards,
Brian

Attachment: 04293.pdf_4ec2b04e.zip


BB Brian Baart December 7, 2016 04:31 PM UTC

Dear Sir,

I have solved the problem.

What I did was make my own table:


                int rijHoogte = 200;
                int TopKolom1 = 20;
                int TopKolom2 = 60;
                int TopKolom3 = 250;

                font = new PdfStandardFont(PdfFontFamily.Helvetica, 8);

                PdfColor zwart = new PdfColor(1, 1, 1);
                PdfPen zwarte_pen = new PdfPen(zwart);

                PdfColor Bijna_zwart = new PdfColor(3, 3, 3);
                PdfPen Bijna_zwarte_pen = new PdfPen(Bijna_zwart);

                PdfColor Geen_zwart = new PdfColor(2, 2, 2);
                PdfPen Geen_zwarte_pen = new PdfPen(Geen_zwart);

                foreach (DataRow rij in Table.Rows)
                {

                    string TOELATINGSNR = rij["TOELATINGSNR"].ToString();
                    graphics.DrawString(TOELATINGSNR, font, PdfBrushes.Aqua, new PointF(TopKolom1, rijHoogte));

                    string OMSCHRIJVING = rij["OMSCHRIJVING"].ToString();
                    graphics.DrawString(OMSCHRIJVING, font, PdfBrushes.Chocolate, new PointF(TopKolom2, rijHoogte));

                    string LINK= rij["LINK"].ToString();
                    graphics.DrawString(LINK, font, PdfBrushes.DarkBlue, new PointF(TopKolom3, rijHoogte));

                    rijHoogte = rijHoogte + 10;

                }

Maybe if somebody else searches these forums and comes across my problem he will have a solution.

Thank you for your help.

With regards,
Brian


SK Sasi Kumar Sekar Syncfusion Team December 8, 2016 12:52 PM UTC

Hi Brain,   
 
Thank you for your update, we are happy to know that the issue is solved at your end and please let us know if you need further assistance. 
  
Regards, 
Sasi Kumar S. 


Loader.
Up arrow icon