【问题标题】:How to create a PDFsharp table with dynamic data?如何使用动态数据创建 PDFsharp 表?
【发布时间】:2015-06-15 19:53:24
【问题描述】:

我正在 PDFsharp 中填充表格以显示图表图例数据。我的列表中有 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);

                };
            };

对象列表

这是我目前得到的结果

【问题讨论】:

  • 第一行似乎是 Data[0],但第二行不是 Data[1]。你有完整的项目,你可以在调试器中单步执行它,你可以看到你为 r,g,b 得到了哪些值,以及 foreach 循环做了多少次迭代。

标签: c# regex pdfsharp


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-20
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 2015-08-31
    • 2020-09-28
    相关资源
    最近更新 更多