如何绘制正方形并动态分配颜色属性(PDFsharp)?

tex*_*697 7 c# pdfsharp

我正在使用PDFsharp导出图表图例.我使用for循环和表来显示属性.实际上只有一个属性:Subdivision名称.该对象具有正确的RGB颜色.如图表图例显示系列那样,如何在它旁边绘制正方形?请原谅我的变量名,我要举个例子.

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

foreach (var item in data)
{
    y = y + 30;
    XRect snoColumnVal = new XRect(35, y, 70, height);
    XRect snoStudentNameVal = new XRect(100, y, 250, height);
    textformater.DrawString(item.Color, tableheader, XBrushes.Black, snoColumnVal);
    textformater.DrawString(item.Name, tableheader, XBrushes.Black, snoStudentNameVal);

}
Run Code Online (Sandbox Code Playgroud)

这是输出:

PDF格式

这就是我需要的样子 PIC

Je *_*not 6

我在演示中使用了这个结构:

struct RowItem
{
    public int R, G, B;
    public string Text;
}
Run Code Online (Sandbox Code Playgroud)

这是我的测试数据:

var data = new[]
               {
                   new RowItem{R = 255, G = 0, B = 0, Text = "Red row"},
                   new RowItem{R = 0, G = 255, B = 0, Text = "Green row"},
                   new RowItem{R = 255, G = 255, B = 0, Text = "Yellow row"},
                   new RowItem{R = 0, G = 0, B = 255, Text = "Blue row"},
                   new RowItem{R = 255, G = 0, B = 255, Text = "Purple row"},
                   new RowItem{R = 0, G = 255, B = 255, Text = "Cyan row"},
                   new RowItem{R = 0, G = 0, B = 0, Text = "Black row"}
               };
Run Code Online (Sandbox Code Playgroud)

这是执行绘图的代码:

foreach (var item in data)
{
    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(255, item.R, item.G, item.B));
    gfx.DrawRectangle(XPens.Black, brush, snoColumnVal);
    textformater.DrawString(item.Text, font, XBrushes.Black, snoStudentNameVal);
}
Run Code Online (Sandbox Code Playgroud)

R,G和B是颜色分量(范围0到255).