Xojo PDFGraphics ←→ Graphics objects, convert between them?

Since Graphics and PDFGraphics object are so similar is there a way to convert between them?
Here is my use case:
I ultimately create a PDF from scratch with Graphics (I have that working) but want to also provide an on-screen “preview” of the output. Do I really have to duplicate all the graphics code to have both PDFGraphics and Graphics?

1 Like

Hi @German_Bauer Welcome to the Xojo Community.

I looked into the documentation and found out that PDFGraphics is a sub-class of Graphics. I searched for the Contructors for PDFGraphics but didn’t find anything. I thought maybe one of the constructors may get a Graphics as a parameter.

Maybe somebody else may have an idea about that.

1 Like

if you have a draw method with g As Graphics input
you can give a PDFGraphics object and that means you lost features of this extra pdf class. theoretical :slight_smile:

Agreed. Make a method somewhere that takes a Graphics object as a parameter and do your drawing in there. If you’re generating a PDF, pass in the PDFGraphics object, otherwise, just the Graphics object.

If you need to know which context you are in inside the method, you can always use something like this:

Sub MyPaint(g as Graphics)
Var isPDF = ( g IsA PDFGraphics) 
If IsPDF Then 
    // do pdf stuff
End If

If Not IsPDF then
    // do non-PDF Stuff
End If
End Sub

Keep in mind that you will need to cast g to a PDFGraphics object for things that only exist in the PDFGraphics class.

3 Likes

This is super helpful thank you so much!