【问题标题】:How to draw squares and assign the color property dynamically (PDFsharp)?如何绘制正方形并动态分配颜色属性(PDFsharp)?
【发布时间】:2015-06-12 18:42:42
【问题描述】:

我正在使用 PDFsharp 导出图表图例。我正在使用 for 循环和表格来显示属性。实际上只有一个属性:细分名称。该对象具有要使用的正确 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);

}

这是输出:

这就是我想要的样子

【问题讨论】:

  • 这周我数不清有多少类似 Tile 的问题了。有一股大学/大学项目提交的恶臭......很遗憾你自己没有好好完成这个任务,谁知道你可能真的很擅长。
  • 我试过了,不知道怎么动态改变物体的颜色。除非我真的需要帮助,否则我不会设置赏金

标签: c# pdfsharp


【解决方案1】:

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

struct RowItem
{
    public int R, G, B;
    public string Text;
}

这是我的测试数据:

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"}
               };

这是绘制的代码:

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 和 B 是颜色分量(范围 0 到 255)。

【讨论】:

  • 如果您不想在彩色矩形周围有黑框,请使用 gfx.DrawRectangle(brush, snoColumnVal); 而不是 gfx.DrawRectangle(XPens.Black, brush, snoColumnVal);
  • gfx 是 XGraphics 对象,也用于 XTextFormatter 的构造函数。
  • 所以我需要将 rgb(67,134,215) 解析为 R = value, G= value,B=value
  • 如果字符串中有颜色值,则必须解析它们。
【解决方案2】:

像这样画正方形:

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

这是设置颜色的方法:

// Create a green color using the FromRgb static method.
Color myRgbColor = new Color();
myRgbColor = Color.FromRgb(0, 255, 0);
return myRgbColor;

参考:https://msdn.microsoft.com/en-us/library/19sb1bw6(v=vs.110).aspx

【讨论】:

  • 所以我会将颜色代码放入循环中? item.color 将替换 (0,255,0) 我假设 e.Graphics 将是 xGraph.Graphics 如果我有 XGraphics xGrap = XGraphics.FromPdfPage(pdfpage);如果是这样,那么 xGraph 不会在智能感知中拾取图形
  • 这个答案展示了如何使用 C# 和 GDI+ 在屏幕上绘制矩形。 PDFsharp 使用的类是相似的,但略有不同(见我的回答)。
【解决方案3】:

要绘制正方形,请使用DrawRectangle 而不是DrawString

可在此处找到示例代码:
http://pdfsharp.net/wiki/Graphics-sample.ashx#Draw_rectangles_6

图形示例也包含在 PDFsharp 源包中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    相关资源
    最近更新 更多