Platforms to show: All Mac Windows Linux Cross-Platform
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DynaPDF/DynaPDF PDF Viewer
This example is the version from Fri, 4th May 2023.
Project "DynaPDF 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
Dim pdf As New DynaPDFMBS
pdf.SetLicenseKey "Pro" // For this example you can use a Pro or Enterprise License
InitColorManagement pdf
Call pdf.CreateNewPDF(Nil)
If pdf.OpenImportFile(item) >= 0 Then
Dim n As Integer = pdf.ImportPDFFile(1)
dim w as new MainWindow
w.Load item, pdf
end if
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
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
Sub InitColorManagement(PDF as DynaPDFMBS)
// init color management, if you have the profile files
Dim profiles As New DynaPDFColorProfilesMBS
// we use some generic profiles here in case none are in the PDF
Dim CMYK_Profile_File As FolderItem = findFile("Generic CMYK Profile.icc")
Dim RGB_Profile_File As FolderItem = findFile("Generic RGB Profile.icc")
Dim Gray_Profile_File As FolderItem = findFile("Generic Gray Profile.icc")
Profiles.DefInCMYK = CMYK_Profile_File
Profiles.DefInGray = Gray_Profile_File
Profiles.DefInRGB = RGB_Profile_File
Profiles.DeviceProfile = Nil // CMYK_Profile_File
Profiles.SoftProof = Nil
// for me seems better with kcsDeviceRGB here and DeviceProfile nil!?
// better than kcsDeviceCMYK and passing CMYK profile for device profile.
If pdf.InitColorManagement(profiles, pdf.kcsDeviceRGB, pdf.kicmBPCompensation) Then
// okay
End If
End Sub
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)
// use built-in PDFKit for macOS
If image = Nil Then
Dim page As DynaPDFPageMBS = List.RowTagAt(row)
Dim pic As Picture = pdf.RenderPagePicture(page.Page, g.Width * 2, g.Height * 2) // render at 2x
If pic <> Nil Then
image = pic
List.CellTagAt(row, 0) = pic
End If
End If
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 DynaPDFMBS)
Self.file = file
Self.pdf = thePDF
Title = file.DisplayName
// use Windows built-in PDF reader
Dim pageCount As Integer = pdf.GetPageCount
For i As Integer = 1 To pageCount
Dim page As DynaPDFPageMBS = pdf.GetPage(i)
List.AddRow ""
List.RowTagAt(List.LastAddedRowIndex) = page
next
List.HeaderAt(0) = pageCount.ToString+" pages"
List.SelectedRowIndex = 0
End Sub
Sub LoadPage(index as integer)
Dim page As DynaPDFPageMBS = List.RowTagAt(index)
If page <> Nil Then
Dim desiredHeight As Integer
Dim desiredWidth As Integer
Dim image As Picture = pdf.RenderPagePicture(page.Page, out.Width * 2, out.Height * 2)
If image <> Nil Then
currentimage = image
out.Refresh
End If
End If
End Sub
Property CurrentImage As Picture
Property PDF As DynaPDFMBS
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:
- /DynaPDF/DynaPDF Database Invoice Example
- /DynaPDF/DynaPDF Graphics/Create numbered pages
- /DynaPDF/DynaPDF Graphics/DynaPDF Graphics
- /DynaPDF/DynaPDF Graphics/DynaPDF Graphics iOS
- /DynaPDF/DynaPDF Graphics/Reporting/List Of Orders
- /DynaPDF/DynaPDF Graphics/Reporting/List Of Products Preview
- /DynaPDF/DynaPDF Graphics/Reporting/ListBoxReport
- /DynaPDF/DynaPDF Graphics/VectorGraphics Alignments
- /DynaPDF/DynaPDF Transparency Color Test
- /DynaPDF/DynaPDF transparent images/DynaPDF transparent images
The items on this page are in the following plugins: MBS DynaPDF Plugin.