Platforms to show: All Mac Windows Linux Cross-Platform
/MacCocoa/PDFKit/PDFView/PDFView overlay/PDFView overlay
Function:
Required plugins for this example: MBS MacFrameworks Plugin, MBS MacBase Plugin, MBS MacControls Plugin, MBS Main Plugin, MBS MacCocoa Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCocoa/PDFKit/PDFView/PDFView overlay/PDFView overlay
Function:
Required plugins for this example: MBS MacFrameworks Plugin, MBS MacBase Plugin, MBS MacControls Plugin, MBS Main Plugin, MBS MacCocoa Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCocoa/PDFKit/PDFView/PDFView overlay/PDFView overlay
Project "PDFView overlay.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control CocoaControlMBS1 Inherits CocoaControlMBS
ControlInstance CocoaControlMBS1 Inherits CocoaControlMBS
EventHandler Function GetView() As NSViewMBS
// todo: Move to PDFViewControlMBS for Xojo
dim file as FolderItem = FindFile("Castles.pdf")
dim doc as PDFDocumentMBS = new PDFDocumentMBS(file)
n=new MyPDFView(0,0,me.Width,me.Height)
n.document = doc
self.doc = doc
self.file = file
MakeOverlays
Return n
End EventHandler
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control BevelButton1 Inherits BevelButton
ControlInstance BevelButton1 Inherits BevelButton
EventHandler Sub Action()
MakeOverlays
// refresh via hide
'n.isHidden = true
'n.isHidden = false
// refresh via needsDisplay
n.needsDisplay = true
End EventHandler
End Control
Control BevelButton2 Inherits BevelButton
ControlInstance BevelButton2 Inherits BevelButton
EventHandler Sub Action()
dim d as new PDFDocumentMBS(file)
dim newPages() as NewPDFPageMBS
dim pageCount as integer = d.pageCount
for pageIndex as integer = 0 to pageCount-1
dim page as PDFPageMBS = d.pageAtIndex(pageIndex)
// make wrapper page
dim newPage as new NewPDFPageMBS
newPage.page = page
newPage.overlay = n.Overlay(page)
newPages.Append newPage
// replace page
d.removePageAtIndex pageIndex
d.insertPage newPage, pageIndex
next
// Check if we got an error and no pages
if d.pageCount = 0 then
break
return
end if
// save to new file
dim newFile as FolderItem = SpecialFolder.Desktop.child("new document.pdf")
call d.write(newFile)
End EventHandler
End Control
EventHandler Sub Open()
if not CocoaControlMBS1.Available then
MsgBox "This example requires Mac OS X 10.5."
quit
end if
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
Sub MakeOverlays()
// add picture as overlay
dim pageCount as integer = doc.pageCount
for pageIndex as integer = 0 to pageCount - 1
dim page as PDFPageMBS = doc.pageAtIndex(pageIndex)
dim bounds as NSRectMBS = page.boundsForBox(page.kPDFDisplayBoxMediaBox)
dim pic as new Picture(bounds.Width, bounds.Height)
dim g as Graphics = pic.Graphics
g.ForeColor = rgb(rnd*256, rnd*256, rnd*256, 100)
g.FillRect 0, 0, 100, 100
g.ForeColor = &c000000
g.TextSize = 20
g.DrawString "You can draw in a picture, NSImage or PDFPage to overlay a PDF page.", 10, 200
n.overlay(page, true) = pic
next
End Sub
Property doc As PDFDocumentMBS
Property file As FolderItem
Property n As MyPDFView
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
Class MyPDFView Inherits CustomPDFViewMBS
End Class
Class NewPDFPageMBS Inherits PDFPageMBS
EventHandler Sub drawRect(box as integer, g as NSGraphicsMBS)
// draw custom page content
if page <> nil then
// draw old PDF page
page.drawWithBox(box)
end if
if overlay isa NSImageMBS then
// draw overlay image
dim image as NSImageMBS = overlay
dim b as NSRectMBS = boundsForBox(box)
g.drawInRect(image, 0, 0, b.Width, b.Height, 0, 0, image.width, image.height, g.NSCompositeSourceOver, 1.0)
end if
if overlay isa PDFPageMBS then
// draw overlay page
dim p as PDFPageMBS = overlay
p.drawWithBox(box)
end if
End EventHandler
Property overlay As Variant
Property page As PDFPageMBS
End Class
End Project
See also:
The items on this page are in the following plugins: MBS MacCocoa Plugin.