Platforms to show: All Mac Windows Linux Cross-Platform
/WinFrameworks/Windows PDF Viewer
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /WinFrameworks/Windows PDF Viewer
This example is the version from Tue, 27th Mar 2023.
Project "Windows PDF Viewer.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub DocumentCreated()
Dim f As FolderItem
#If DebugBuild Then
f = SpecialFolder.Desktop.Child("test.pdf")
if f.Exists then
OpenDocument f
return
end if
#endif
Dim o As New OpenFileDialog
o.Filter = FileTypeGroup1.Pdf
f = o.ShowModal
If f <> Nil Then
OpenDocument f
End If
End EventHandler
EventHandler Sub DocumentOpened(item As FolderItem)
Try
#If TargetWindows Then
// use Windows built-in PDF reader
Dim pdf As WindowsPDFDocumentMBS = WindowsPDFDocumentMBS.LoadFromFile(item)
if pdf <> nil then
dim w as new MainWindow
w.Load item, pdf
end if
#ElseIf TargetMacOS Then
// use built-in PDFKit for macOS
Dim pdf As PDFDocumentMBS = New PDFDocumentMBS(item)
If pdf <> Nil Then
Dim w As New MainWindow
w.Load item, pdf
End If
#EndIf
Catch r As RuntimeException
MessageBox "Failed to load PDF."+EndOfLine+EndOfLine+r.Message
End Try
End EventHandler
EventHandler Sub Opening()
End EventHandler
Function FileOpen() As Boolean
Dim o As New OpenFileDialog
o.Filter = FileTypeGroup1.Pdf
dim f as FolderItem = o.ShowModal
if f <> nil then
OpenDocument f
end if
Return True
End Function
End Class
Class MainWindow Inherits DesktopWindow
Control Out Inherits DesktopCanvas
ControlInstance Out Inherits DesktopCanvas
EventHandler Sub Paint(g As Graphics, areas() As Rect)
dim image as Picture = CurrentImage
if image <> nil then
dim faktor as Double = min( g.Height / image.Height, g.Width / image.Width)
// Calculate new size
dim w as Integer = image.Width * faktor
dim h as Integer = image.Height * faktor
dim x as integer = (g.Width -w)/2
dim y as integer = (g.Height-h)/2
g.DrawPicture image, x, y, w, h, 0, 0, image.Width, image.Height
end if
End EventHandler
End Control
Control List Inherits DesktopListBox
ControlInstance List Inherits DesktopListBox
EventHandler Function PaintCellText(g as Graphics, row as Integer, column as Integer, x as Integer, y as Integer) As Boolean
Dim image As Picture = List.CellTagAt(row, 0)
#If TargetWindows Then
If image = Nil Then
// use Windows built-in PDF reader
dim page as WindowsPDFPageMBS = List.RowTagAt(row)
if options = nil then
options = new WindowsPDFPageRenderOptionsMBS
Const desiredHeight = 200 // 100 * 2x resolution
options.DestinationHeight = desiredHeight
options.setBitmapEncoderJPEG
'options.BackgroundColor = &CFFFFFF
end if
dim data as string = page.RenderToStream(options)
if data.Bytes > 0 then
image = Picture.FromData(data)
List.CellTagAt(row, 0) = image
end if
end if
#ElseIf TargetMacOS Then
// use built-in PDFKit for macOS
If image = Nil Then
Dim page As PDFPageMBS = List.RowTagAt(row)
// request low resolution version at 36 dpi.
// a 842 points height A4 document is now about 400 pixel height
Dim nsImage As NSImageMBS = page.Render(72/2)
If nsImage <> Nil Then
image = nsImage.CopyPicture
List.CellTagAt(row, 0) = image
End If
End If
#EndIf
if image <> nil then
Dim faktor As Double = Min( g.Height / image.Height, g.Width / image.Width)
// Calculate new size
dim w as Integer = image.Width * faktor
dim h as Integer = image.Height * faktor
dim xx as integer = (g.Width -w)/2
dim yy as integer = (g.Height-h)/2
g.DrawPicture image, xx, yy, w, h, 0, 0, image.Width, image.Height
end if
End EventHandler
EventHandler Sub SelectionChanged()
if me.SelectedRowIndex >= 0 then
LoadPage me.SelectedRowIndex
end if
End EventHandler
End Control
EventHandler Sub Opening()
End EventHandler
Sub Load(file as FolderItem, thePDF as Variant)
Self.file = file
Self.pdf = thePDF
Title = file.DisplayName
#If TargetWindows Then
// use Windows built-in PDF reader
Dim pdf As WindowsPDFDocumentMBS = thePDF
Dim pageCount As Integer = pdf.PageCount
For i As Integer = 0 To pageCount-1
Dim page As WindowsPDFPageMBS = pdf.page(i)
List.AddRow ""
List.RowTagAt(List.LastAddedRowIndex) = page
next
#ElseIf TargetMacOS Then
Dim pdf As PDFDocumentMBS = thePDF
Dim pageCount As Integer = pdf.PageCount
For i As Integer = 0 To pageCount-1
Dim page As PDFPageMBS = pdf.pageAtIndex(i)
List.AddRow ""
List.RowTagAt(List.LastAddedRowIndex) = page
Next
#EndIf
List.HeaderAt(0) = pageCount.ToString+" pages"
List.SelectedRowIndex = 0
End Sub
Sub LoadPage(index as integer)
#If TargetWindows Then
Dim page As WindowsPDFPageMBS = List.RowTagAt(index)
if page <> nil then
dim options as new WindowsPDFPageRenderOptionsMBS
if out.Width > out.Height then
options.DestinationWidth = out.Width * 2
else
options.DestinationHeight = out.Height * 2
end if
options.setBitmapEncoderJPEG
'options.BackgroundColor = &CFFFFFF
dim data as string = page.RenderToStream(options)
if data.Bytes > 0 then
currentimage = Picture.FromData(data)
out.Refresh
end if
End If
#Elseif TargetMacOS then
// use built-in PDFKit for macOS
Dim page As PDFPageMBS = List.RowTagAt(index)
If page <> Nil Then
Dim NSIMage As NSImageMBS = page.Render(144)
If NSIMage <> Nil Then
currentimage = NSIMage.CopyPicture
out.Refresh
End If
End If
#EndIf
End Sub
Property CurrentImage As Picture
Property PDF As Variant
just To keep a reference And this Object In memory.
Property file As FolderItem
Property options As WindowsPDFPageRenderOptionsMBS
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileOpen = "Open..."
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 HelpMenu = "&Help"
End MenuBar
FileTypeGroup1
Filetype Pdf
End FileTypeGroup1
End Project
See also:
The items on this page are in the following plugins: MBS WinFrameworks Plugin.