How to create a PDFsharp table with dynamic data?

5.5k views Asked by At

I am populating a table in PDFsharp to display chart legend data. I have 14 objects in my list and only 2 are being displayed, and the colors of their rectangles are identical. They each have a unique color to use. How can I get all of them to display correctly?

   //Draw the table Row Borders
            xGrap.DrawRectangle(XPens.DarkSeaGreen, XBrushes.DarkSeaGreen, snoColumn); //Use different Color for Colum
            xGrap.DrawRectangle(XPens.DarkSeaGreen, XBrushes.DarkSeaGreen, snoStudentName);

            //Writting Table Header Text
            textformater.DrawString(" Color", tableheader, XBrushes.Black, snoColumn);
            textformater.DrawString(" Subdivision Name", tableheader, XBrushes.Black, snoStudentName);

            foreach (var item in data)
            {
                string colorStr = item.Color;

                Regex regex = new Regex(@"rgb\((?<r>\d{1,3}),(?<g>\d{1,3}),(?<b>\d{1,3})\)");
                Match match = regex.Match(colorStr);
                if (match.Success)
                {
                    int r = int.Parse(match.Groups["r"].Value);
                    int g = int.Parse(match.Groups["g"].Value);
                    int b = int.Parse(match.Groups["b"].Value);

                    y = y + 30;
                    XRect snoColumnVal = new XRect(35, y, 60, 25);
                    XRect snoStudentNameVal = new XRect(100, y, 250, 25);

                    var brush = new XSolidBrush(XColor.FromArgb(r, g, b));
                    xGrap.DrawRectangle(brush, snoColumnVal);
                    textformater.DrawString(item.Name, bodyfont, XBrushes.Black, snoStudentNameVal);

                };
            };

List of objects pic

This is the result I am currently getting picture

1

There are 1 answers

0
I liked the old Stack Overflow On BEST ANSWER

I guess the color string of Data[1] contains a blank (blue has two digits only). Maybe this causes the match to fail and the line is skipped.

As a hack you can try regex.Match(colorStr.Replace(" ", ""));. Better modify the regular expression to allow spaces.

You do not show the color string for Savannah.