In contrast to Windows (where you have to buy 3rd party products like the high
priced Adobe Acrobat or other products or where you have to fight with the poorly
documented API of the Adobe Reader) MacOS X offers all the necessary API functions
to display PDF documents in your own applications. The following code example
demonstrates the basics of a very simple PDF viewer:
Code: Select all
EnableExplicit
ImportC ""
CFStringCreateWithCString(CFAllocatorRef.L, CString, CFStringEncoding.L)
CFRelease(CFTypeRef.L)
CFURLCreateWithFileSystemPath(CFAllocatorRef.L, CFStringFilePath.L, PathStyle.L, IsDirectory.L)
CGContextDrawPDFPage(CGContextRef.L, CGPDFPageRef.L)
CGPDFDocumentCreateWithURL(CFURLRef.L)
CGPDFDocumentGetNumberOfPages(PDFDocumentRef.L)
CGPDFDocumentGetPage(PDFDocumentRef.L, PageNumber.L)
CGPDFDocumentRelease(PDFDocumentRef.L)
GetWindowPort(WindowRef.L)
QDBeginCGContext(*ColorGraphicsPort, *CGContextRef)
QDEndCGContext(*ColorGraphicsPort, *CGContextRef)
EndImport
#kCFAllocatorDefault = 0
#kCFStringEncodingMacRoman = 0
#kCFURLPOSIXPathStyle = 0
Procedure.L OpenPDFDocument(PathAndPDFFilename.S)
Protected FilePathRef.L
Protected PDFDocumentRef.L
Protected URLRef.L
; ----- Convert PDF file path from PureBasic String into Core Foundation
; string (CFString)
FilePathRef = CFStringCreateWithCString(#kCFAllocatorDefault, @PathAndPDFFilename, #kCFStringEncodingMacRoman)
If FilePathRef
; ----- Create CFURL object using CFString with local file system path
URLRef = CFURLCreateWithFileSystemPath(#kCFAllocatorDefault, FilePathRef, #kCFURLPOSIXPathStyle, #False)
If URLRef
; ----- Create Quartz PDF document with URL of PDF file
PDFDocumentRef = CGPDFDocumentCreateWithURL(URLRef)
CFRelease(URLRef)
EndIf
CFRelease(FilePathRef)
EndIf
ProcedureReturn PDFDocumentRef
EndProcedure
Procedure DrawPDFPage(PDFDocumentRef.L, PageNumber.L)
Protected ContextRef.L
Protected *GraphicsPort
Protected PDFPageRef.L
; ----- Read page from Quartz PDF document
PDFPageRef = CGPDFDocumentGetPage(PDFDocumentRef, PageNumber)
If PDFPageRef
; ----- Obtain ColorGraphicsPort of window
*GraphicsPort = GetWindowPort(WindowID(0))
If *GraphicsPort
; ----- Obtain Quartz 2D drawing environment associated with graphics port
QDBeginCGContext(*GraphicsPort, @ContextRef)
If ContextRef
; ----- Draw PDF page into current graphics context
CGContextDrawPDFPage(ContextRef, PDFPageRef)
; ----- Terminate Quartz 2D drawing environment
QDEndCGContext(*GraphicsPort, @ContextRef)
EndIf
EndIf
EndIf
EndProcedure
Define CurrentPage.L
Define LastPage.L
Define PathAndPDFFilename.S
Define PDFDocumentRef.L
Define WindowEvent.L
OpenWindow(0, 200, 50, 580, 790, "PDF-Viewer")
SetWindowColor(0, $FFFFFF)
; ----- Draw ToolBar buttons for one page back and forward
CreateImage(0,16,16)
StartDrawing(ImageOutput(0))
Box(0, 0, 16, 16, $FFFFFF)
DrawText(3, 0, "<", 0, $FFFFFF)
StopDrawing()
CreateImage(1,16,16)
StartDrawing(ImageOutput(1))
Box(0, 0, 16, 16, $FFFFFF)
DrawText(5, 0, ">", 0, $FFFFFF)
StopDrawing()
CreateToolBar(0, WindowID(0))
ToolBarImageButton(0,ImageID(0))
ToolBarImageButton(1,ImageID(1))
; ----- Create Quartz PDF object
PathAndPDFFilename = "/Developer/Documentation/CHUD/Saturn/SaturnUserGuide.pdf"
PDFDocumentRef = OpenPDFDocument(PathAndPDFFilename)
If PDFDocumentRef
CurrentPage = 1
LastPage = CGPDFDocumentGetNumberOfPages(PDFDocumentRef)
DrawPDFPage(PDFDocumentRef, CurrentPage)
Repeat
WindowEvent = WaitWindowEvent()
Select WindowEvent
Case #PB_Event_Repaint
DrawPDFPage(PDFDocumentRef, CurrentPage)
Case #PB_Event_Menu
Select EventMenu()
Case 0
If CurrentPage > 1
CurrentPage - 1
EndIf
Case 1
If CurrentPage < LastPage
CurrentPage + 1
EndIf
EndSelect
SetWindowColor(0, $FFFFFF)
DrawPDFPage(PDFDocumentRef, CurrentPage)
EndSelect
Until WindowEvent = #PB_Event_CloseWindow
; ----- Release Quartz PDF document
CGPDFDocumentRelease(PDFDocumentRef)
EndIf