Platforms to show: All Mac Windows Linux Cross-Platform
/DynaPDF/Raster/DynaPDF Raster to MemoryBlock
Required plugins for this example: MBS DynaPDF Plugin, MBS Images Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DynaPDF/Raster/DynaPDF Raster to MemoryBlock
This example is the version from Thu, 27th Mar 2019.
Project "DynaPDF Raster to MemoryBlock.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub NewDocument()
dim file as FolderItem
if DebugBuild then
file = FindFile("license.pdf")
end if
if file = nil then
file = GetOpenFolderItem(FileTypes1.Pdf)
end if
if file<>nil then
OpenDocument file
end if
End EventHandler
EventHandler Sub OpenDocument(item As FolderItem)
dim p as new PDFWindow(item)
p.show
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 PDFWindow Inherits Window
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect)
If pic<>Nil Then
// scale down for
g.DrawPicture pic, 0, 0, g.Width, g.Height, 0, 0, pic.Width, pic.Height
end if
End EventHandler
EventHandler Sub Resized()
render
End EventHandler
EventHandler Sub Resizing()
'render
End EventHandler
Sub Constructor(file as FolderItem)
// Calling the overridden superclass constructor.
Super.Constructor
Self.file = file
Self.Title = file.DisplayName
pdf = New MyDynaPDFMBS
PageNumber = 1
call pdf.CreateNewPDF(nil)
Call pdf.OpenImportFile(file, 0, "")
call pdf.SetImportFlags(pdf.kifImportAsPage + pdf.kifImportAll) // important! It makes the rendering faster.
Call pdf.ImportPDFFile(1,1.0,1.0)
If pdf.GetPageCount > 0 Then
render
End If
End Sub
Private Sub render()
// *2 for higher DPI
Dim Width As UInt32 = Self.Width * 2
Dim Height As UInt32 = Self.Height * 2
Dim ScanlineLen As Int32 = Width * 4
Dim PixelFormat As UInt32 = pdf.kpxfBGRA
// we render here to memoryblock in case you need pixels in a memoryblock...
Dim Buffer As New Memoryblock(height * ScanlineLen)
Dim Rasterizer As New DynaPDFRasterizerMBS(pdf, Nil, buffer, width, Height, ScanlineLen, PixelFormat)
Dim options As New DynaPDFRasterImageMBS
options.DefScale = options.kpsFitBest
options.Flags = options.krfCompositeWhite
options.InitWhite = True
Dim page As DynaPDFPageMBS = pdf.GetPage(PageNumber)
If Rasterizer.RenderPage(page, options) Then
// we use PictureMBS to display it
Dim PP As New PictureMBS(Buffer, Width, Height, PictureMBS.ImageFormatBGRA, ScanlineLen)
pic = pp.CopyPicture
Else
// Failed to render page?
pic = Nil
End If
#if RBVersion < 2013 then
self.Refresh(False)
#else
Self.Invalidate
#endif
End Sub
Property Private PageNumber As Integer
Property file As FolderItem
Property Private pdf As MyDynaPDFMBS
Property pic As Picture
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
FileTypes1
Filetype application/pdf
End FileTypes1
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
Property IgnoreWarnings As Boolean
End Class
End Project
See also:
The items on this page are in the following plugins: MBS DynaPDF Plugin.