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

Obtaining graphics object from PagePreview
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3142
Page 1 of 1

Author:  Andrew1 [ Thu Jul 09, 2015 4:09 pm ]
Post subject:  Obtaining graphics object from PagePreview

I want to call the render code manually, ie not activated by an event

I can do this with standard forms objects like a RichTextBox:

Dim myGraphics As Graphics = RichTextBox1.CreateGraphics
Dim mysize As XSize : mysize.Height = 300: mysize.Width = 300
Dim gfx As XGraphics = XGraphics.FromGraphics(myGraphics, mysize)
Call myrender(gfx)


And myrender draws the image correctly on the target.

How do I do this with the PagePreview?

trying:

Dim myGraphics As Graphics = PagePreview1.CreateGraphics

Does not throw up any errors, but the image does no appear.

Author:  () => true [ Fri Jul 10, 2015 10:48 am ]
Post subject:  Re: Obtaining graphics object from PagePreview

Andrew1 wrote:
I want to call the render code manually, ie not activated by an event
Why?

PagePreview calls a render routine supplied by you.
Why not call this routine directly without PagePreview in between?
Create a Graphics object for your target, encapsulate it in an XGraphics object and call your render routine.

If you think there is an error in PDFsharp then please provide an SSCCE.
See also:
viewtopic.php?p=2094#p2094

Author:  Andrew1 [ Fri Jul 10, 2015 8:02 pm ]
Post subject:  Re: Obtaining graphics object from PagePreview

Thanks for your reply.

>>>> Why?

Because I am trying to understand the code in the example "Preview"

In the example I can see how the graphics object from the printer or the pdf page is obtained, with code like:

Private Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim graphics As Graphics = ev.Graphics
graphics.PageUnit = GraphicsUnit.Point
Dim gfx As XGraphics = XGraphics.FromGraphics(graphics, PageSizeConverter.ToSize(PageSize.A4))
.......

But I can't see how the graphics object is got for the PagePreview.
I don't see gfx being assigned when the PagePreview is used.

I thought I would first obtain the graphics object for the PagePreview by hand, and call the renderer manually.
And then transfer it into an PagePreview event handler.
This would help me understand what the code is doing.

Author:  Andrew1 [ Mon Jul 13, 2015 2:28 pm ]
Post subject:  Re: Obtaining graphics object from PagePreview

I never did manage to separate out the page preview graphics object, I just got the code working as in the sample.

But incase it helps other people trying to understand the code, here is what I found:

A) the page preview control automatically calls the render event when the zoom level changes or the scroll bars are used, or the size of the page preview changes. I was puzzled by the lack of an invalidate call in the sample zoom routine... this is why.


B) I could not see the mechanism that creates a graphics object (gfx) so that the render can be called, but it is in the property.set shown below, which is called one time during the form load.

Friend Myrenderevent As PagePreview.RenderEvent

Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
RenderEvent = New PagePreview.RenderEvent(AddressOf myrender)
End Sub

WriteOnly Property RenderEvent() As PagePreview.RenderEvent
Set(ByVal value As PagePreview.RenderEvent)
pagePreview.SetRenderEvent(value)
Myrenderevent = value
End Set
End Property

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