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/place centered/place centered
Function:
Required plugins for this example: MBS DynaPDF Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DynaPDF/place centered/place centered
This example is the version from Thu, 6th Apr 2016.
Project "place centered.rbp"
FileTypes
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 i,tmpl as integer dim d as new date dim mediaBox, cropBox as DynaPDFRectMBS dim x,y,w,h as Double dim b as Boolean dim f as FolderItem=SpecialFolder.Desktop.Child("place centered.pdf") pdf.SetLicenseKey "Pro" // For this example you can use a Pro or Enterprise License call pdf.CreateNewPDF f call pdf.SetDocInfo pdf.kdiSubject, "My first Realbasic output" call pdf.SetDocInfo pdf.kdiProducer, "Realbasic test application" call pdf.SetDocInfo pdf.kdiTitle, "My first Realbasic output" // Open an existing PDF file dim file as FolderItem = FindFile("taxform.pdf") call pdf.OpenImportFile(file, pdf.kptOpen, "") // Import the fist page as template tmpl = pdf.ImportPage(1) // Get the bounding boxes of the imported page mediaBox=pdf.GetInBBox( 1, pdf.kpbMediaBox) cropBox=pdf.GetInBBox( 1, pdf.kpbCropBox) // PlaceCentered() requires Bottom-Up coordinates. call pdf.SetPageCoords(pdf.kpcBottomUp) x = 50.0 y = pdf.GetPageHeight - 50.0 // We place the template 4 times on the page... call pdf.Append // 50 units margin plus 50 units distance between columns w = (pdf.GetPageWidth - 150.0) / 2.0 h = pdf.CalcHeight(mediaBox, cropBox, w) y = y-h call pdf.SetStrokeColor(255) for i = 0 to 3 if BitwiseAnd(i,1)=1 then x = x + (50.0 + w) elseif bitwiseand(i,2)=2 then x = 50.0 y = y - (50.0 + h) end if call pdf.Rectangle( x, y, w, h, pdf.kfmStroke) b=pdf.PlaceCentered(tmpl, mediaBox, cropBox, x, y, w, h) next call pdf.EndPage call pdf.CloseFile f.Launch quit End EventHandler
Function FindFile(name as string) As FolderItem // Look for file in parent folders from executable on dim parent as FolderItem = app.ExecutableFile.Parent while parent<>Nil dim file as FolderItem = parent.Child(name) if file<>Nil and file.Exists then Return file end if parent = parent.Parent wend 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
Function CalcHeight(MediaBox as DynaPDFRectMBS, CropBox as DynaPDFRectMBS, destwidth as double) As double dim bbox as DynaPDFRectMBS if CropBox = nil then bbox = MediaBox else bbox = CropBox end if dim w as double = bbox.Right - bbox.Left dim h as double = bbox.Top - bbox.Bottom return me.CalcWidthHeight(w, h, DestWidth, 0.0) End Function
Function PlaceCentered(tmpl as integer, MediaBox as DynaPDFRectMBS, CropBox as DynaPDFRectMBS, DestX as double, DestY as double, DestWidth as double, DestHeight as double) As boolean dim bbox as DynaPDFRectMBS dim x, y, w, h, sx as Double dim haveCropBox as Boolean if CropBox = nil then bbox = MediaBox haveCropBox = false else bbox = CropBox haveCropBox = true end if w = bbox.Right - bbox.Left h = bbox.Top - bbox.Bottom sx = DestWidth / w if (h * sx <= DestHeight) then h = h * sx w = (MediaBox.Right - MediaBox.Left) * sx x = DestX - bbox.Left * sx y = DestY - bbox.Bottom * sx + (DestHeight - h) / 2.0 if (haveCropBox) then call me.SaveGraphicState call me.Rectangle(DestX, DestY, DestWidth, DestHeight, me.kfmNoFill) call me.ClipPath(me.kcmWinding, me.kfmNoFill) call me.PlaceTemplate( Tmpl, x, y, w, 0.0) return me.RestoreGraphicState else return me.PlaceTemplate( Tmpl, x, y, w, 0.0) end if else sx = DestHeight / h w = w * sx h = (MediaBox.Top - MediaBox.Bottom) * sx x = DestX - bbox.Left * sx + (DestWidth - w) / 2.0 y = DestY - bbox.Bottom * sx if (haveCropBox) then call me.SaveGraphicState call me.Rectangle(DestX, DestY, DestWidth, DestHeight, me.kfmNoFill) call me.ClipPath( me.kcmWinding, me.kfmNoFill) call me.PlaceTemplate( Tmpl, x, y, 0.0, h) return me.RestoreGraphicState else return me.PlaceTemplate( Tmpl, x, y, 0.0, h) end if end if End Function
Property IgnoreWarnings As Boolean
End Class
End Project

See also:

Feedback, Comments & Corrections

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




Links
MBS Xojo tutorial videos