How to draw squares and assign color property dynamically (PDFsharp)?

I use PDFsharp to export legend diagrams. I use a for loop and a table to display properties. In fact, there is only one property: the name of the subkey. The object has the correct RGB color. How can I draw squares next to it just like chart legends show a series? Forgive me for the variable names, I'm going to an 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 result:

pdf

that's what i need to look pic

+6
source share
3 answers

I used this structure to demonstrate:

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

Here are 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 is the code that makes 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 color components (range from 0 to 255).

+6
source

Just draw a square like this:

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

This 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; 

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

+2
source

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 original PDFsharp package.

0
source

Source: https://habr.com/ru/post/989032/


All Articles