Platforms to show: All Mac Windows Linux Cross-Platform

/DynaPDF/place centered/place centered and rotate


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 and rotate

This example is the version from Thu, 6th Apr 2016.

Project "place centered and rotate.xojo_binary_project"
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 winkel,i,tmpl as integer dim d as new date dim mediaBox, cropBox as DynaPDFRectMBS dim x,y,w,h as Double dim f as FolderItem=SpecialFolder.Desktop.Child("place centered and rotate.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 = 0.0 y = 0 for i = 0 to 3 winkel=i*90 call pdf.Append call pdf.SetOrientationEx winkel w = pdf.GetPageWidth h = pdf.CalcHeight(mediaBox, cropBox, w) call pdf.SetStrokeColor(255) call pdf.PlaceRotatedCentered(tmpl, mediaBox, cropBox, x, y, w, h, winkel) call pdf.EndPage next 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
Function PlaceRotatedCentered(tmpl as integer, MediaBox as DynaPDFRectMBS, CropBox as DynaPDFRectMBS, x as double, y as double, w as double, h as double, winkel as integer) As boolean dim n as Double dim result as Boolean dim xx,yy as Double Select case winkel case 90 n=w w=h h=n xx=GetPageHeight-GetPageWidth-50 yy=-GetPageWidth case 180 x=x+w y=y+h case 270 n=w w=h h=n xx=-GetPageWidth-50 yy=0 end Select call me.SaveGraphicState call me.RotateCoords winkel,x,y result=me.PlaceCentered(tmpl, mediaBox, cropBox, xx, yy, w, h) call me.RestoreGraphicState Return result End Function
Property IgnoreWarnings As Boolean
End Class
End Project

See also:

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


The biggest plugin in space...