How to draw squares and assign the color property dynamically (PDFsharp)?
Asked Answered
M

3

7

I am using PDFsharp to export a chart legend. I am using a for loop and a table to display the properties. There is actually only one property: the Subdivision name. The object has the right RGB color to use. 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:

pdf

this is what I need it to look like pic

Melanson answered 12/6, 2015 at 18:42 Comment(2)
I've lost count of the number of Tile like questions this week. there is a stench of uni/college project submissions... It is a pity you don't give this task a good go yourself, who knows you may actually be good at it.Szymanowski
i have tried, and I do not know how to dynamically change the color of the object. I wouldnt set a bounty unless I really needed helpMelanson
W
9

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

Wafture answered 15/6, 2015 at 15:29 Comment(4)
Use gfx.DrawRectangle(brush, snoColumnVal); instead of gfx.DrawRectangle(XPens.Black, brush, snoColumnVal); if you don't want to have a black frame around the color rectangle.Wafture
gfx is the XGraphics object that was also used in the constructor of XTextFormatter.Wafture
so i would need to parse rgb(67,134,215) to R = value, G= value,B=valueMelanson
If you have the colour values in a string then you will have to parse them.Wafture
S
2

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

Szymanowski answered 15/6, 2015 at 15:11 Comment(2)
so I would put the color code inside the loop? item.color would replace (0,255,0) I assume e.Graphics would be xGraph.Graphics if I have XGraphics xGrap = XGraphics.FromPdfPage(pdfpage); If so then xGraph is not picking up Graphics in intellesenseMelanson
This answer shows how to draw rectangles on the screen using C# and GDI+. The classes used by PDFsharp are similar, but slightly different (see my answer).Wafture
W
0

To draw squares, use DrawRectangle instead of DrawString.

Sample code can be found here:
http://pdfsharp.net/wiki/Graphics-sample.ashx#Draw_rectangles_6

The Graphics sample is also included in the PDFsharp source package.

Wafture answered 13/6, 2015 at 6:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.