[英]How to draw squares and assign the color property dynamically (PDFsharp)?
I am using PDFsharp to export a chart legend. 我正在使用PDFsharp导出图表图例。 I am using a for loop and a table to display the properties. 我使用for循环和表来显示属性。 There is actually only one property: the Subdivision name. 实际上只有一个属性:Subdivision名称。 The object has the right RGB color to use. 该对象具有正确的RGB颜色。 How can I draw squares next to it just as chart legends display the series? 如图表图例显示系列那样,如何在它旁边绘制正方形? Forgive me for the variable names, I am going off a example. 请原谅我的变量名,我要举个例子。
//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);
}
Here is the output: 这是输出:
this is what I need it to look like 这就是我需要的样子
I used this struct for the demo: 我在演示中使用了这个结构:
struct RowItem
{
public int R, G, B;
public string Text;
}
Here's my test data: 这是我的测试数据:
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"}
};
And here's the code that does the drawing: 这是执行绘图的代码:
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);
}
R, G, and B are the color components (range 0 through 255). R,G和B是颜色分量(范围0到255)。
Just draw the square like so: 像这样画广场:
// Create solid brush.
SolidBrush blueBrush = new SolidBrush(Color.Blue);
// Create rectangle.
Rectangle rect = new Rectangle(0, 0, 200, 200);
// Fill rectangle to screen.
e.Graphics.FillRectangle(blueBrush, rect);
Here is how you set the color: 以下是设置颜色的方法:
// Create a green color using the FromRgb static method.
Color myRgbColor = new Color();
myRgbColor = Color.FromRgb(0, 255, 0);
return myRgbColor;
Ref: https://msdn.microsoft.com/en-us/library/19sb1bw6(v=vs.110).aspx 参考: https : //msdn.microsoft.com/en-us/library/19sb1bw6(v = vs.110) .aspx
To draw squares, use DrawRectangle
instead of DrawString
. 要绘制正方形,请使用DrawRectangle
而不是DrawString
。
Sample code can be found here: 示例代码可以在这里找到:
http://pdfsharp.net/wiki/Graphics-sample.ashx#Draw_rectangles_6 http://pdfsharp.net/wiki/Graphics-sample.ashx#Draw_rectangles_6
The Graphics sample is also included in the PDFsharp source package. Graphics样本也包含在PDFsharp源包中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.