PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Drawing to Winform
http://forum.pdfsharp.com/viewtopic.php?f=2&t=3861
Page 1 of 1

Author:  LuigiVampa [ Tue Oct 16, 2018 2:55 pm ]
Post subject:  Drawing to Winform

Is it possible to draw to a WinForm or something similar? I read that the PDFSharp XGraphic can draw to a System.Drawing.Graphics object but can't quite get the code to work on it.

Here's where I'm at so far.

Any ideas what I'm doing wrong here?

Code:

            PdfDocument documentPDF = new PdfDocument();

            Graphics graphics = this.CreateGraphics();
            XSize xSize = new XSize(100.0, 100.0);

            PdfPage page = documentPDF.AddPage();
            XGraphics gfx = XGraphics.FromGraphics(graphics, xSize);

            Document documentMDoc = new Document();
            Section sec = documentMDoc.AddSection();
            Paragraph para = sec.AddParagraph();

            para.AddText("Duisism odigna acipsum delesenisl");

            DocumentRenderer docRenderer = new DocumentRenderer(documentMDoc);
            docRenderer.PrepareDocument();
            docRenderer.RenderObject(gfx, 100, 100, 100, para);

Author:  TH-Soft [ Wed Oct 17, 2018 5:44 am ]
Post subject:  Re: Drawing to Winform

Hi!

Maybe start simple: leave out MigraDoc and use the "gfx" to draw a simple line to get started.
If that works then try again with MigraDoc.

Author:  LuigiVampa [ Fri Oct 19, 2018 9:17 pm ]
Post subject:  Re: Drawing to Winform

Thanks for the reply.

Tried that already. Not sure there's much more to strip from it!

Code:
            PdfPage pdfPage = documentPDF.AddPage();
            pdfPage.Width = XUnit.FromMillimeter(this.Width);
            pdfPage.Height = XUnit.FromMillimeter(this.Height);
           
            XGraphics gfx = XGraphics.FromGraphics(this.CreateGraphics(), new XSize(this.Width, this.Height));

            XPen lineRed = new XPen(XColors.Red, 5);
            gfx.DrawLine(lineRed, new Point(0, 100), new Point(500, 100));

Author:  TH-Soft [ Sat Oct 20, 2018 6:50 am ]
Post subject:  Re: Drawing to Winform

LuigiVampa wrote:
Tried that already. Not sure there's much more to strip from it!
My modus operandi: start simple, then make it more complicated. When problems occur, compare the last iteration that worked with the first iteration that does not work.

Nice that you tried that. What was the outcome?

Author:  LuigiVampa [ Sat Oct 20, 2018 7:40 am ]
Post subject:  Re: Drawing to Winform

Yet to have an iteration that works at all! I've got it to draw to a pdf just fine but nothing when it comes to drawing on the WinForm. In that little bit of code 'this' is Form1 of the solution.

As I step through it using debug mode line by line I see that the first line;

Code:
PdfPage pdfPage = documentPDF.AddPage();


creates a PDF page object that has an ArtBox, BleedBox, TrimBox, CropBox all with x and y points set to zero. Are they additional co-ordinates of top-left and bottom-right of the drawing surface? I've set the width and height of the pdf, do I need to set widths and heights for all of them to open up the drawing area? Editing the width and height only seems to reference the MediaBox area.

Thanks again for taking the time to reply.

:)

Author:  Thomas Hoevel [ Mon Oct 22, 2018 11:52 am ]
Post subject:  Re: Drawing to Winform

Hi!

First step: use the "graphics" object to draw e.g. a line on the WinForms object.
If that works, then use the "gfx" object to draw a line with the same co-ordinates (second step).

XGraphics is a wrapper for the Graphics object. You cannot expect XGraphics to work if you cannot draw with the Graphics object.

Your code snippet creates a PdfDocument that is not used and adds a PdfPage to it that is not used either.

Author:  LuigiVampa [ Fri Oct 26, 2018 8:22 pm ]
Post subject:  Re: Drawing to Winform

Hello,
I'd noticed that about the pdf page! Thanks.

Reached a point where I've created a test solution for this one line of code now!

Code:
XGraphics gfx = XGraphics.FromGraphics(this.CreateGraphics(), new XSize(100.0, 100.0));
XPen lineRed = new XPen(XColors.Red, 5);
gfx.DrawLine(lineRed, 0, this.Height / 2, this.Width, this.Height / 2);


It should draw a line horizontally across the middle of the form shouldn't it?
Got nothing though.

'this' being the winform.

Thanks.

Author:  TH-Soft [ Fri Oct 26, 2018 8:33 pm ]
Post subject:  Re: Drawing to Winform

LuigiVampa wrote:
It should draw a line horizontally across the middle of the form shouldn't it?
Got nothing though.
Does it draw a line when you use "this.CreateGraphics()" for drawing?
That would use WinForms directly without involving PDFsharp. This should be the first step.
Your code is the second step, Make sure the first step works before trying the second step.

Author:  LuigiVampa [ Sat Oct 27, 2018 5:57 am ]
Post subject:  Re: Drawing to Winform

Legend!!! Thank you.
:D :D

I was running the code outside of the form's Paint event handler. This now draws a big fat red line across the middle.

Code:

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            XGraphics gfx = XGraphics.FromGraphics(this.CreateGraphics(), new XSize(100.0, 100.0));
            XPen lineRed = new XPen(XColors.Red, 5);
            gfx.DrawLine(lineRed, 0, this.Height / 2, this.Width, this.Height / 2);
        }




Thanks again.

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