Platforms to show: All Mac Windows Linux Cross-Platform

/DynaPDF/Raster/DynaPDF Threaded Render Job


Last modified Mon, 16th Mar 2025.

You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /DynaPDF/Raster/DynaPDF Threaded Render Job

Download this example: DynaPDF Threaded Render Job.zip

Project "DynaPDF Threaded Render Job.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Opening() // using plugin MainWindow.show // using Xojo to run the thread instead 'MainWindowXojo.show End EventHandler
End Class
Class MainWindow Inherits DesktopWindow
Control List Inherits DesktopListBox
ControlInstance List Inherits DesktopListBox
End Control
Control Canvas1 Inherits DesktopCanvas
ControlInstance Canvas1 Inherits DesktopCanvas
EventHandler Sub Paint(g As Graphics, areas() As Rect) // Calculate scale factor If pic <> Nil Then Var faktor As Double = Min( g.Height / Pic.Height, g.Width / Pic.Width) // Calculate new size Var w As Integer = Pic.Width * faktor Var h As Integer = Pic.Height * faktor // draw picture in the new size g.DrawPicture Pic, 0, 0, w, h, 0, 0, Pic.Width, Pic.Height End If End EventHandler
End Control
EventHandler Sub Opening() Var file As New FolderItem("dynapdf_help.pdf") If Not file.Exists Then MessageBox "Please change file path" Break return End If job = New DynaPDFRenderJob job.list = list job.Resolution = 150 job.InputPath = file.NativePath 'job.PageRangeStart = 30 'job.PageRangeLength = 30 List.AddRow "Start..." job.run End EventHandler
Sub UpdatePicture(p as Picture) pic = p canvas1.Refresh End Sub
Property job As DynaPDFRenderJob
Property pic As Picture
End Class
Class MainWindowXojo Inherits DesktopWindow
Control List Inherits DesktopListBox
ControlInstance List Inherits DesktopListBox
End Control
Control Canvas1 Inherits DesktopCanvas
ControlInstance Canvas1 Inherits DesktopCanvas
EventHandler Sub Paint(g As Graphics, areas() As Rect) // Calculate scale factor If pic <> Nil Then Var faktor As Double = Min( g.Height / Pic.Height, g.Width / Pic.Width) // Calculate new size Var w As Integer = Pic.Width * faktor Var h As Integer = Pic.Height * faktor // draw picture in the new size g.DrawPicture Pic, 0, 0, w, h, 0, 0, Pic.Width, Pic.Height End If End EventHandler
End Control
EventHandler Sub Opening() Var file As New FolderItem("dynapdf_help.pdf") If Not file.Exists Then MessageBox "Please change file path" Break return End If job = New DynaPDFRenderJobXojo job.list = list job.Resolution = 150 job.InputPath = file.NativePath 'job.PageRangeStart = 30 'job.PageRangeLength = 30 List.AddRow "Start..." job.run End EventHandler
Sub UpdatePicture(p as Picture) pic = p canvas1.Refresh End Sub
Property job As DynaPDFRenderJobXojo
Property pic As Picture
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
MenuItem WindowMenu = "Window"
MenuItem HelpMenu = "&Help"
End MenuBar
Sign
End Sign
Class DynaPDFRenderJob Inherits DynaPDFRenderJobMBS
EventHandler Sub Finished() Var e As Double = Microseconds Var d As Double = e - StartTime Var m As String = CurrentMethodName+" "+Str(d/1000000.0, "0.0")+" seconds" System.DebugLog m list.AddRow m End EventHandler
EventHandler Sub PageRendered(Page as Integer) list.AddRow CurrentMethodName+" "+page.ToString Var JPEGData As String = Self.ImageData(page) var pic As Picture = Picture.FromData(JPEGData) MainWindow.UpdatePicture pic End EventHandler
EventHandler Sub PagesImported(Count as Integer) list.AddRow CurrentMethodName+" "+count.ToString End EventHandler
Sub Constructor() // Calling the overridden superclass constructor. Super.Constructor StartTime = Microseconds End Sub
Property StartTime As Double
Property list As DesktopListBox
End Class
Class DynaPDFRenderJobXojo
Sub Constructor() System.DebugLog CurrentMethodName StartTime = Microseconds InitCMFlags = DynaPDFMBS.kicmBPCompensation DestSpace = DynaPDFMBS.kcsDeviceRGB PixFmt = DynaPDFMBS.kpxfRGB Filter = DynaPDFMBS.kcfFlate Flags = DynaPDFMBS.krfDefault Format = DynaPDFMBS.kifmJPEG PageRangeLength = -1 End Sub
Sub JobFinished() Var e As Double = Microseconds Var d As Double = e - StartTime Var m As String = CurrentMethodName+" "+Str(d/1000000.0, "0.0")+" seconds" System.DebugLog m list.AddRow m Thread = Nil End Sub
Sub PageRendered(Page as Integer) System.DebugLog CurrentMethodName+" "+page.ToString list.AddRow CurrentMethodName+" "+page.ToString Var JPEGData As String = Self.ImageData(page) var pic As Picture = Picture.FromData(JPEGData) MainWindow.UpdatePicture pic End Sub
Sub PagesImported(Count as Integer) System.DebugLog CurrentMethodName+" "+count.ToString list.AddRow CurrentMethodName+" "+count.ToString End Sub
Sub Run() If Width = 0 And Height = 0 And Resolution = 0 Then Resolution = 150 End If Redim ImageData(-1) Finished = False cancel = False running = True Thread = New DynaPDFRenderJobXojoThread Thread.job = Self Thread.run End Sub
Property DefInCMYK As String
Property DefInGray As String
Property DefInRGB As String
Property DestSpace As Integer
Property DeviceProfile As String
Property Filter As Integer
Property Finished As Boolean
Property Flags As Integer
Property Format As Integer
Property Height As Integer
Property ImageData() As string
Property InitCMFlags As Integer
Property InputData As string
Property InputPath As string
Property PageCount As Integer
Property PageRangeLength As Integer
Property PageRangeStart As Integer
Property Password As String
Property PasswordType As Integer
Property PixFmt As Integer
Property Resolution As Integer
Property SoftProof As String
Property StartTime As Double
Property Thread As DynaPDFRenderJobXojoThread
Property Width As Integer
Property cancel As Boolean
Property list As DesktopListBox
Property list1 As DesktopListBox
Property running As Boolean
End Class
Class DynaPDFRenderJobXojoThread Inherits Thread
EventHandler Sub Run() Var pdf As New DynaPDFMBS Call pdf.CreateNewPDF(Nil) Call pdf.SetImportFlags pdf.kifImportAll Or pdf.kifImportAsPage Call pdf.SetImportFlags2 pdf.kif2NoResNameCheck Or pdf.kif2UseProxy Or pdf.kif2NoMetadata Call pdf.SetUseTransparency False If job.DefInRGB.Bytes > 0 Or job.DefInCMYK.Bytes > 0 Or job.DefInGray.Bytes > 0 Or job.SoftProof.Bytes > 0 And job.DeviceProfile.Bytes > 0 Then Var profiles As New DynaPDFColorProfilesExMBS profiles.DefInRGB = job.DefInRGB profiles.DefInCMYK = job.DefInCMYK profiles.DefInGray = job.DefInGray profiles.SoftProof = job.SoftProof profiles.DeviceProfile = job.DeviceProfile Var b As Boolean = pdf.InitColorManagementEx(profiles, job.DestSpace, job.InitCMFlags) If Not b Then Return End If If job.cancel Then Return Var PageCount As Integer = 0 Var destPage As Integer = 1 If job.InputData.Bytes > 0 Then Var r As Integer = pdf.OpenImportBuffer(job.InputData, job.PasswordType, job.Password) If r < 0 Then Return Var InPageCount As Integer = pdf.GetInPageCount PageCount = PageCount + InPageCount r = pdf.ImportPDFFile(destPage) If r < 0 Then Return destPage = r + 1 End If If job.InputPath.Bytes > 0 Then Var r As Integer = pdf.OpenImportFile(job.InputPath, job.PasswordType, job.Password) If r < 0 Then Return Var InPageCount As Integer = pdf.GetInPageCount PageCount = PageCount + InPageCount r = pdf.ImportPDFFile(destPage) If r < 0 Then Return destPage = r + 1 End If job.PageCount = PageCount If job.cancel Then Return Redim job.ImageData(PageCount) timer.CallLater 0, AddressOf CallPagesImported, PageCount Var f1 As Integer = pdf.FlattenAnnots(pdf.kafNone) If f1 < 0 Then Return Var f2 As Boolean = pdf.FlattenForm If f2 = False Then Return If job.PageRangeLength <= 0 Then job.PageRangeLength = job.PageCount - job.PageRangeStart + 1 End If For page As Integer = 1 To job.PageCount If (page >= job.PageRangeStart And page < job.PageRangeStart + job.PageRangeLength) Then Var r As Boolean = pdf.RenderPageToImage(page, Nil, job.Resolution, job.Width, job.Height, job.FLags, job.PixFmt, job.Filter, job.Format) If r Then var buf as string = pdf.GetImageBuffer job.ImageData(page) = buf Call pdf.FreeImageBuffer End If timer.CallLater 0, AddressOf CallPageFinished, page End If If job.cancel Then Return Next Call pdf.CloseFile timer.CallLater 0, AddressOf CallFinished End EventHandler
Sub CallFinished() job.JobFinished job = Nil End Sub
Private Sub CallPageFinished(value as Variant) job.PageRendered value End Sub
Private Sub CallPagesImported(v as Variant) job.PagesImported v End Sub
Sub Run() Self.Type = Thread.types.Preemptive // Calling the overridden superclass method. Super.Run() End Sub
Property job As DynaPDFRenderJobXojo
End Class
End Project

See also:

Download this example: DynaPDF Threaded Render Job.zip

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


The biggest plugin in space...