簡體   English   中英

如何繪制正方形並動態分配顏色屬性(PDFsharp)?

[英]How to draw squares and assign the color property dynamically (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);

}

這是輸出:

PDF格式

這就是我需要的樣子 PIC

我在演示中使用了這個結構:

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)。

像這樣畫廣場:

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

要繪制正方形,請使用DrawRectangle而不是DrawString

示例代碼可以在這里找到:
http://pdfsharp.net/wiki/Graphics-sample.ashx#Draw_rectangles_6

Graphics樣本也包含在PDFsharp源包中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM