如何使用动态数据创建 PDFsharp table?

How to create a PDFsharp table with dynamic data?

我正在 PDFsharp 中填充 table 以显示图表图例数据。我的列表中有 14 个对象,但只显示了 2 个,它们的矩形颜色相同。他们每个人都有独特的颜色可供使用。我怎样才能让所有这些都正确显示?

   //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);

                };
            };

对象列表

这是我目前得到的结果

我猜数据[1]的颜色字符串包含一个空白(蓝色只有两位数)。也许这会导致匹配失败并跳过该行。

作为 hack,您可以尝试 regex.Match(colorStr.Replace(" ", ""));。最好修改正则表达式允许空格。

您没有显示 Savannah 的颜色字符串。