Platforms to show: All Mac Windows Linux Cross-Platform
/DynaPDF/DynaPDF Graphics/DynaPDF Graphics
Required plugins for this example: MBS DynaPDF Plugin, MBS Main Plugin
Last modified Sun, 9th Aug 2025.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /DynaPDF/DynaPDF Graphics/DynaPDF Graphics
Download this example: DynaPDF Graphics.zip
Project "DynaPDF Graphics.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action()
Dim g As Graphics=OpenPrinterDialog
if g=nil then
Return
end if
draw g
End EventHandler
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action()
Dim pdf As New MyDynapdfMBS
Dim f1 As FolderItem = SpecialFolder.Desktop.Child("DynaPDF Graphics.pdf")
Dim f2 As FolderItem = SpecialFolder.Desktop.Child("DynaPDF Graphics.png")
Dim f3 As FolderItem = SpecialFolder.Desktop.Child("DynaPDF Graphics Rendered.png")
'pdf.SetLicenseKey "Starter" // For this example you can use a Starter, Lite, Pro or Enterprise License
If Not pdf.CreateNewPDF(f1) Then
Return
end if
Call pdf.Append
dim g as Graphics = pdf.PageGraphics
draw g, pdf
// for debugging, show temp picture
Dim p As Picture = pdf.PageGraphicsPicture
p.Save(f2, p.SaveAsPNG)
Call pdf.EndPage
If pdf.HasPro Then
Call pdf.RenderPageToImage(1, f3, 150, 0, 0, pdf.krfDefault, pdf.kpxfRGB, pdf.kcfFlate, pdf.kifmPNG)
End If
Call pdf.CloseFile
f1.Launch True
End EventHandler
End Control
Control EditField1 Inherits TextArea
ControlInstance EditField1 Inherits TextArea
End Control
Control Canvas1 Inherits Canvas
ControlInstance Canvas1 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect)
draw g
End EventHandler
End Control
Sub draw(g as graphics, pdf as DynaPDFMBS = nil)
// this is normal code as you would use with graphics class anyway
// but we use GraphicsWrapper which can be DynaPDFGraphics, too.
// so graphics can go to printer, window, picture or PDF!
g.ForeColor=&c777777
g.DrawRect 0,0,g.Width-1,g.Height-1
g.ForeColor=&cFF0000
g.FillRect 100,100,200,200
g.DrawLine 0,0,100,100
g.ForeColor=&c000000
g.DrawString "Hello",150,150
g.DrawLine 100,100,200,200
g.ForeColor=&c00FF00
g.FillOval 100,0,100,100
g.ForeColor=&c0000FF
g.DrawOval 100,0,100,100
g.ForeColor=&cFFFF00
g.FillRect 0,100,100,100
g.ForeColor=&c000000
g.DrawRect 0,100,100,100
g.ForeColor=&c00FF00
g.FillRoundRect 0,250,100,100,20,20
g.ForeColor=&c000000
g.DrawRoundRect 0,250,100,100,20,20
Dim Points(6) as Integer
Points(1)=10 //X of Point 1
Points(2)=10 //Y of Point 1
Points(3)=75 //X of Point 2
Points(4)=30 //Y of Point 2
Points(5)=10 //X of Point 3
Points(6)=125 //Y of Point 3
g.ForeColor= RGB(100,200,255)
g.FillPolygon Points
g.ForeColor=&c000000
g.DrawPolygon Points
g.TextFont = "Times"
g.TextSize = 12
g.DrawString "Hello World", 200, 100
g.Bold = True
g.DrawString "Bold", 300, 100
g.Bold = False
g.Italic = True
g.DrawString "Italic", 300, 120
g.Italic = False
g.Underline = True
g.DrawString "Underlined", 300, 140
g.Underline = False
g.DrawString "Just a test with wrapping", 300, 160, 100
g.DrawString "Just a test with condensed", 300, 200, 100, true
// test picture drawing
dim logo as Picture = LogoMBS(200)
g.DrawPicture logo, 200, 500
g.DrawPicture logo, 50, 500, 100, 200, 50, 50, 100, 100
// set a pixel
g.Pixel(400,400)=&cFFFFFF
// StyledTextPrinter is not supported for DynaPDF's graphics object
If pdf = Nil Then
Dim stp As StyledTextPrinter = EditField1.StyledTextPrinter(g, 200)
stp.DrawBlock 200,500,250
Else
Call pdf.WriteStyledTextEx(200, 500, 200, 250, pdf.ktaLeft, EditField1)
End If
End Sub
Sub testImage()
Var g As Graphics
Var f As FolderItem
// we place an image today
Var imageFile As New FolderItem("IMG_4601.jpeg")
Var pic As Picture = Picture.Open(imageFile)
// try Xojo and make a new PDF
Var doc As New PDFDocument
// and get graphics for drawing to the page
g = doc.Graphics
f = New FolderItem("test2.pdf")
// we pick a font, which supports all the characters
g.FontName = "Arial Unicode MS"
g.FontSize = 20
g.DrawText "Xojo " + XojoVersionString, 50, 100
// place the image and scale down
Var w As Integer = g.Width
Var h As Integer = w * (pic.Height / pic.Width)
g.DrawPicture pic, 0, 150, w, h, 0, 0, pic.Width, pic.Height
doc.Save f
// show file
f.Launch
// now try with DynaPDF
Var pdf As New MyDynapdfMBS
// For this example you can use a Starter, Lite, Pro or Enterprise License
#If False
pdf.SetLicenseKey "Starter"
#EndIf
// write to this file
f = New FolderItem("test1.pdf")
Call pdf.CreateNewPDF f
// new page
Call pdf.Append
// if you need higher resolution than 150 dpi
Call pdf.SetResolution(600)
// we use graphics class to draw, so we can use same commands as above for Xojo
g = pdf.PageGraphics
g.FontName = "Arial Unicode MS"
g.FontSize = 20
g.DrawText "DynaPDF " + DynaPDFMBS.GetDynaPDFVersion, 50, 100
// place the image and scale down
g.DrawPicture pic, 0, 150, w, h, 0, 0, pic.Width, pic.Height
// close page and file
Call pdf.EndPage
Call pdf.CloseFile
// show file
f.Launch
// now check the differences between both PDFs!
quit
End Sub
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MyDynaPDFMBS Inherits DynaPDFMBS
EventHandler Function Error(ErrorCode as integer, ErrorMessage as string, ErrorType as integer) As integer
// output all messages on the console:
System.DebugLog str(ErrorCode)+": "+ErrorMessage
// and display dialog:
Dim d as New MessageDialog //declare the MessageDialog object
Dim b as MessageDialogButton //for handling the result
d.icon=MessageDialog.GraphicCaution //display warning icon
d.ActionButton.Caption="Continue"
d.CancelButton.Visible=True //show the Cancel button
// a warning or an error?
if BitAnd(ErrorType, me.kE_WARNING) = me.kE_WARNING then
// if user decided to ignore, we'll ignore
if IgnoreWarnings then Return 0
d.Message="A warning occurred while processing your PDF code."
// we add a third button to display all warnings
d.AlternateActionButton.Caption = "Ignore warnings"
d.AlternateActionButton.Visible = true
else
d.Message="An error occurred while processing your PDF code."
end if
d.Explanation = str(ErrorCode)+": "+ErrorMessage
b=d.ShowModal //display the dialog
Select Case b //determine which button was pressed.
Case d.ActionButton
Return 0 // ignore
Case d.AlternateActionButton
IgnoreWarnings = true
Return 0 // ignore
Case d.CancelButton
Return -1 // stop
End select
End EventHandler
EventHandler Function PageBreak(LastPosX as double, LastPosY as double, PageBreak as boolean) As integer
PageBreak = true
Return -1 // stop
End EventHandler
Property IgnoreWarnings As Boolean
Property PageBreak As Boolean
End Class
Sign
End Sign
End Project
See also:
- /DynaPDF/DynaPDF Database Invoice Example
- /DynaPDF/DynaPDF Graphics/DynaPDF Graphics Desktop
- /DynaPDF/DynaPDF Graphics/DynaPDF Graphics iOS
- /DynaPDF/DynaPDF Graphics/Reporting/Breaking List Of Orders
- /DynaPDF/DynaPDF Graphics/Reporting/List Of Products Preview
- /DynaPDF/DynaPDF Graphics/VectorGraphics
- /DynaPDF/DynaPDF Graphics/VectorGraphics Alignments
- /DynaPDF/DynaPDF Matrix tests
- /DynaPDF/DynaPDF PDF Viewer
- /DynaPDF/DynaPDF Transparency Color Test
Download this example: DynaPDF Graphics.zip
The items on this page are in the following plugins: MBS DynaPDF Plugin.