Platforms to show: All Mac Windows Linux Cross-Platform
/DynaPDF/Raster/DynaPDF Display PDF with Links
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/Raster/DynaPDF Display PDF with Links
This example is the version from Thu, 27th Mar 2019.
Project "DynaPDF Display PDF with Links.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub NewDocument()
dim file as FolderItem = GetOpenFolderItem(FileTypes1.Pdf)
if file<>nil then
OpenDocument file
end if
End EventHandler
EventHandler Sub OpenDocument(item As FolderItem)
dim p as new PDFWindow
p.Title = item.DisplayName
p.init item
p.show
End EventHandler
End Class
Class PDFWindow Inherits Window
EventHandler Function KeyDown(Key As String) As Boolean
// Next page with space
if asc(key)=32 then
page = page +1
render
redraw
Return true
end if
End EventHandler
EventHandler Function MouseDown(X As Integer, Y As Integer) As Boolean
render
redraw
Return true
End EventHandler
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect)
if pic<>Nil then
g.DrawPicture pic,0,0
end if
End EventHandler
EventHandler Sub Resized()
render
redraw
End EventHandler
EventHandler Sub Resizing()
render
redraw
End EventHandler
Sub init(file as FolderItem)
page = 1
pdf = new MyDynaPDFMBS
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)
render
Maximize
End Sub
Sub redraw()
Self.Invalidate
End Sub
Sub render()
dim t as integer = 0
dim o as new DynaPDFRasterImageMBS
o.InitWhite = true
// Rotation
o.DefScale = o.kpsFitBest
o.Flags = o.krfDefault
dim r as new DynaPDFRasterizerMBS(pdf, width, height)
dim p as DynaPDFPageMBS = pdf.GetPage(page)
if r.RenderPage(p, o) then
pic = r.Pic
dim PageSpace as DynapdfMatrixMBS = o.PageSpace
// now show rectangles around links
if pdf <> NIL and pic<>Nil then
dim g as Graphics = pic.Graphics
dim u as integer = pdf.GetAnnotCount-1
dim links, rechts, oben, unten as Double
for i as integer = 0 to u
dim b as DynaPDFAnnotationExMBS = pdf.GetAnnotEx(i)
if b.PageNum<>page then Continue
dim bbox as DynaPDFRectMBS = b.BBox
if b.Type = pdf.katWebLink then
links = bbox.Left
rechts = bbox.Right
oben = bbox.Top
unten = bbox.Bottom
PageSpace.Transform(links, oben)
PageSpace.Transform(rechts, unten)
dim URL as string = b.DestFile
dim w as integer = rechts-links
dim h as integer = unten-oben
if w<0 then
links = links + w
w = -w
end if
if h<0 then
oben = oben + h
h = -h
end if
g.ForeColor = &cFF0000
g.DrawRect links, oben, w, h
g.DrawString URL, links, oben
t = t + 1
end if
next
end
else
// failed
break
pic = nil
end if
Title = Str(t)+" links on page"
End Sub
Property page As Integer
Property 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.
![The biggest plugin in space...](https://www.monkeybreadsoftware.de/images/xojoplugin.png)