Xojo Developer Conference
25/27th April 2018 in Denver.
MBS Xojo Conference
6/7th September 2018 in Munich, Germany.

Platforms to show: All Mac Windows Linux Cross-Platform

/DynaPDF/Create PDF with object2d
Required plugins for this example: MBS DynaPDF Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DynaPDF/Create PDF with object2d
This example is the version from Sun, 23th Sep 2017.
Project "Create PDF with object2d.rbp"
Filetype text
End FileTypes
MenuBar MenuBar1
MenuItem UntitledMenu1 = ""
MenuItem FileMenu = "&File"
MenuItem FileQuit = "Quit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu0 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class App Inherits Application
EventHandler Sub Open() dim pdf as new MyDynapdfMBS dim o as Object2d = CreateObjects pdf.SetLicenseKey "Starter" // For this example you can use a Starter, Lite, Pro or Enterprise License dim f as FolderItem = SpecialFolder.Desktop.Child("Create PDF with object2d.pdf") call pdf.CreateNewPDF f call pdf.SetViewerPreferences pdf.kvpDisplayDocTitle,pdf.kavNone call pdf.SetDocInfo pdf.kdiAuthor, "Christian Schmitz" call pdf.SetDocInfo pdf.kdiSubject, "My first Xojo output" call pdf.SetDocInfo pdf.kdiProducer, "Xojo test application" call pdf.SetDocInfo pdf.kdiTitle, "My first Xojo output" // We want to use top-down coordinates call pdf.SetPageCoords pdf.kpcTopDown call pdf.Append pdf.drawObject o, pdf.GetPageWidth/2, pdf.GetPageHeight/2 call pdf.EndPage call pdf.CloseFile f.Launch MainWindow.o=o MainWindow.show End EventHandler
Function CreateObjects() As Object2d Dim px as PixmapShape Dim s as StringShape Dim d as New Group2D const pi = 3.14159265 px=New PixmapShape(logombs(100)) d.append px s=New StringShape s.y=70 s.Text="You see the MBS Logo!" s.TextFont="Helvetica" s.Bold=true d.append s s=New StringShape s.y=120 s.Rotation = pi/2.0 s.Text="Rotated Text" s.TextFont="Helvetica" s.Bold=true d.append s Dim r as New RectShape r.X=150 r.width=75 r.height=75 r.border=100 r.bordercolor=RGB(0,0,0) //black r.fillcolor=RGB(0,127,127) // teal r.borderwidth=2.5 r.rotation=-.78 d.append r Dim o as New OvalShape o.width=60 o.height=60 o.Fillcolor=RGB(127,127,255) o.x=-150 d.Append o // mitte r=New RectShape r.X=0 r.width=10 r.height=10 r.border=100 r.borderwidth=0 r.rotation=0 d.append r // rechts r=New RectShape r.X=100 r.width=10 r.height=10 r.border=100 r.borderwidth=0 r.FillColor=&cFF0000 r.rotation=0 d.append r // links r=New RectShape r.X=-100 r.width=10 r.height=10 r.border=100 r.borderwidth=0 r.FillColor=&c0000FF r.rotation=0 d.append r // unten r=New RectShape r.Y=100 r.width=10 r.height=10 r.FillColor=&c00FF00 r.border=100 r.borderwidth=0 r.rotation=0 d.append r // oben r=New RectShape r.Y=-100 r.width=10 r.height=10 r.border=100 r.borderwidth=0 r.rotation=0 d.append r Return d End Function
End Class
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
Private Sub DrawGroup(g as Group2D) dim i,c as integer SetupGraphicsState g c=g.Count-1 for i=0 to c drawObject g.Item(i) next call me.RestoreGraphicState End Sub
Private Sub DrawOval(o as OvalShape) SetupGraphicsState o call me.DrawCircle(0,0,(o.width+o.height)/4.0, kfmFill) call me.RestoreGraphicState End Sub
Private Sub DrawPixmap(p as PixmapShape) SetupGraphicsState p call me.SetUseTransparency(false) call me.InsertPicture(p.Image, -p.Width/2.0, -p.Height/2.0, p.Width, p.Height) call me.RestoreGraphicState End Sub
Private Sub DrawRect(r as RectShape) SetupGraphicsState r dim m as integer=-1 if r.Border>0 then if r.Fill>0 then m=me.kfmFillStroke else m=me.kfmStroke end if else if r.Fill>0 then m=me.kfmFill else m=0 end if end if if m>=0 then call me.Rectangle -r.Width/2.0, -r.Height/2.0, r.Width, r.Height, m end if call me.RestoreGraphicState End Sub
Private Sub DrawString(s as StringShape) SetupGraphicsState s dim fontstyle as integer if s.Bold then fontstyle=fontstyle+me.kfsBold end if if s.Italic then fontstyle=fontstyle+me.kfsItalic end if if s.Underline then fontstyle=fontstyle+me.kfsUnderlined end if call me.SetFont s.TextFont, fontstyle, s.TextSize, true, me.kcp1252 dim w as integer = me.GetPageWidth dim h as integer = me.GetFTextHeight(me.ktaCenter,s.text) call me.WriteFTextEx -w/2.0, h, w, -1, me.ktaCenter, s.Text 'call me.Rectangle -w/2.0, 0, w, h, me.kfmStroke call me.RestoreGraphicState End Sub
Private Sub SetupGraphicsState(o as Object2D) const pi=3.14159265 call me.SaveGraphicState call me.SetLineWidth o.BorderWidth call me.SetStrokeColor o.BorderColor.blue*65536+o.BorderColor.green*256+o.BorderColor.red call me.SetFillColor o.FillColor.blue*65536+o.FillColor.green*256+o.FillColor.red call me.ScaleCoords o.Scale, o.Scale call me.TranslateCoords o.x, -o.y call me.RotateCoords 360.0-o.Rotation*180.0/pi, 0, 0 End Sub
Private Sub drawObject(o as Object2d) if o isa Group2D then DrawGroup Group2D(o) elseif o isa OvalShape then DrawOval OvalShape(o) elseif o isa PixmapShape then DrawPixmap PixmapShape(o) elseif o isa RectShape then DrawRect RectShape(o) elseif o isa StringShape then DrawString StringShape(o) end if End Sub
Sub drawObject(o as Object2d, offsetX as integer, offsetY as integer) call me.SaveGraphicState call me.TranslateCoords offsetx, offsety drawObject o call me.RestoreGraphicState End Sub
Note "About"
This is a very basic extension for DynaPDF to use Object2D. If you add code, send it to me so I can include it with this example project. still todo: * transparency * other shapes Disadvantages: * Input Object2D may not have sizes correct for the page format used. * PictureShape uses RB picture object. Adding JPEG/TIFF file directly may result into smaller PDF files. * Font handling expects the font names to match between RB and DynaPDF, but some translation is needed there. Greetings Christian
Property IgnoreWarnings As Boolean
End Class
Class MainWindow Inherits Window
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect) g.DrawObject o, me.Width/2, me.Height/2 End EventHandler
Property o As Object2d
End Class
End Project

See also:

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS DynaPDF Plugin.

MBS FileMaker Plugins