It is currently Thu May 23, 2013 3:57 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Simple PDF viewer
PostPosted: Wed Apr 20, 2011 9:59 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Apr 21, 2005 2:38 pm
Posts: 814
Location: Germany
Have you ever had the need to display PDF documents in your own Mac application?
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:
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


Top
 Profile  
 
 Post subject: Re: Simple PDF viewer
PostPosted: Wed Nov 23, 2011 8:07 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Aug 04, 2008 10:56 pm
Posts: 849
Location: Seattle, USA
Nice! Works fine here.

Code:
; to load your own PDF
;PathAndPDFFilename = "/Developer/Documentation/CHUD/Saturn/SaturnUserGuide.pdf"
PathAndPDFFilename = OpenFileRequester("Load PDF file...", "", "", 0) ; pattern$ doesn't work on Mac
PDFDocumentRef = OpenPDFDocument(PathAndPDFFilename)


Thanks Shardik :)

@zabenka
Are you on a Mac? You don't need (or want) Acrobat Reader to get involved this, the PDF viewer runs in PureBasic.

_________________
MacBook Pro/Retina, OSX 10.8.3 Mountain Lion, PB-5.11x64


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye