Class: HotCocoa::Graphics::Pdf

Inherits:
Object
  • Object
show all
Defined in:
lib/hotcocoa/graphics/pdf.rb

Overview

parse a PDF file to determine pages, width, height

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original, password = nil) ⇒ Pdf

create a new Pdf object given the original pathname, and password if needed



23
24
25
26
27
28
29
30
31
# File 'lib/hotcocoa/graphics/pdf.rb', line 23

def initialize(original, password = nil)
  # http://developer.apple.com/documentation/GraphicsImaging/Reference/CGPDFDocument/Reference/reference.html
  # http://developer.apple.com/documentation/GraphicsImaging/Reference/CGPDFPage/Reference/reference.html
  @pdf = CGPDFDocumentCreateWithURL(NSURL.fileURLWithPath(original)) # => CGPDFDocumentRef
  result = CGPDFDocumentUnlockWithPassword(@pdf, password) if password # unlock if necessary
  @pages = CGPDFDocumentGetNumberOfPages(@pdf) # => 4
  puts "pdf.new #{original} (#{@pages} pages)" if @verbose
  self
end

Instance Attribute Details

#pagesObject (readonly)

Returns the value of attribute pages.



20
21
22
# File 'lib/hotcocoa/graphics/pdf.rb', line 20

def pages
  @pages
end

#pdfObject (readonly)

Returns the value of attribute pdf.



20
21
22
# File 'lib/hotcocoa/graphics/pdf.rb', line 20

def pdf
  @pdf
end

Instance Method Details

#draw(ctx, x = 0, y = 0, w = width(pagenum), h = height(pagenum), pagenum = 1) ⇒ Object

draw pagenum of the pdf document into a rectangle at x,y with dimensions w,h of drawing context ctx



55
56
57
58
59
60
# File 'lib/hotcocoa/graphics/pdf.rb', line 55

def draw(ctx, x=0, y=0, w=width(pagenum), h=height(pagenum), pagenum=1)
  rect = CGRectMake(x,y,w,h)
  puts "pdf.draw page #{pagenum} at [#{x},#{y}] with #{w}x#{h}" if @verbose
  CGContextDrawPDFDocument(ctx, rect, @pdf, pagenum)
  true
end

#height(pagenum = 1) ⇒ Object

get the height of the specified pagenum



47
48
49
50
51
52
# File 'lib/hotcocoa/graphics/pdf.rb', line 47

def height(pagenum=1)
  cgpdfpage = page(pagenum)
  mediabox = CGPDFPageGetBoxRect(cgpdfpage, KCGPDFMediaBox) # => CGRect
  height = mediabox.size.height # CGRectGetHeight(mediabox)
  height
end

#verbose(tf) ⇒ Object

print drawing functions to console if verbose is true



34
35
36
# File 'lib/hotcocoa/graphics/pdf.rb', line 34

def verbose(tf)
  @verbose = tf
end

#width(pagenum = 1) ⇒ Object

get the width of the specified pagenum



39
40
41
42
43
44
# File 'lib/hotcocoa/graphics/pdf.rb', line 39

def width(pagenum=1)
  cgpdfpage = page(pagenum)
  mediabox = CGPDFPageGetBoxRect(cgpdfpage, KCGPDFMediaBox) # => CGRect
  width = mediabox.size.width # CGRectGetWidth(mediabox)
  width
end