PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Dec 10, 2018 9:36 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Drawing to Winform
PostPosted: Tue Oct 16, 2018 2:55 pm 
Offline

Joined: Tue Oct 09, 2018 8:36 am
Posts: 7
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);


Top
 Profile  
Reply with quote  
 Post subject: Re: Drawing to Winform
PostPosted: Wed Oct 17, 2018 5:44 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 428
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
 Post subject: Re: Drawing to Winform
PostPosted: Fri Oct 19, 2018 9:17 pm 
Offline

Joined: Tue Oct 09, 2018 8:36 am
Posts: 7
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));


Top
 Profile  
Reply with quote  
 Post subject: Re: Drawing to Winform
PostPosted: Sat Oct 20, 2018 6:50 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 428
Location: CCAA
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?

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
 Post subject: Re: Drawing to Winform
PostPosted: Sat Oct 20, 2018 7:40 am 
Offline

Joined: Tue Oct 09, 2018 8:36 am
Posts: 7
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.

:)


Top
 Profile  
Reply with quote  
 Post subject: Re: Drawing to Winform
PostPosted: Mon Oct 22, 2018 11:52 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2893
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: Drawing to Winform
PostPosted: Fri Oct 26, 2018 8:22 pm 
Offline

Joined: Tue Oct 09, 2018 8:36 am
Posts: 7
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Drawing to Winform
PostPosted: Fri Oct 26, 2018 8:33 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 428
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
 Post subject: Re: Drawing to Winform
PostPosted: Sat Oct 27, 2018 5:57 am 
Offline

Joined: Tue Oct 09, 2018 8:36 am
Posts: 7
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: Baidu [Spider] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group