PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

how to draw squares and assign the color property dynamicall
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3128
Page 1 of 1

Author:  texas697 [ Fri Jun 12, 2015 6:45 pm ]
Post subject:  how to draw squares and assign the color property dynamicall

I am using pdf sharp to export a chart legend. I am using a forloop 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 apply a squares next to it just as chart legends display the series's? forgive me for the variable names, I am going off a example.

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

                }

Author:  () => true [ Sat Jun 13, 2015 6:55 am ]
Post subject:  Re: how to draw squares and assign the color property dynami

Answered here:
http://stackoverflow.com/a/30815707/1015447

Author:  Thomas Hoevel [ Mon Jun 15, 2015 3:59 pm ]
Post subject:  Re: how to draw squares and assign the color property dynami

Some sample code:
Code:
struct RowItem
{
    public int R, G, B;
    public string Text;
}

static void Demo()
{
    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"}
                   };

    var document = new PdfDocument();

    PdfPage page = document.AddPage();
    XGraphics gfx = XGraphics.FromPdfPage(page);
    var textformater = new XTextFormatter(gfx);
    XFont font = new XFont("Segoe UI", 20, XFontStyle.Regular);
    int y = 50;
    textformater.DrawString(" Color", font, XBrushes.Black, new XRect(35, y, 70, 25));
    textformater.DrawString(" Subdivision Name", font, XBrushes.Black, new XRect(100, y, 250, 25));

    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);
        gfx.DrawRectangle(brush, snoColumnVal);
        textformater.DrawString(item.Text, font, XBrushes.Black, snoStudentNameVal);
    }
    const string filename = "HelloWorld_tempfile.pdf";
    document.Save(filename);
    // ...and start a viewer.
    Process.Start(filename);
}

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/