Class: PDF::Writer::Object::Pages

Inherits:
PDF::Writer::Object show all
Defined in:
lib/pdf/writer/object/pages.rb

Overview

object which is a parent to the pages in the document

Instance Attribute Summary collapse

Attributes inherited from PDF::Writer::Object

#oid

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Pages



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pdf/writer/object/pages.rb', line 13

def initialize(parent)
  super(parent)

  @parent.catalog.pages = self

  @pages      = []
  @procset    = nil
  @media_box  = nil
  @fonts      = []
  @xObjects   = []
  @bleed_box  = nil
  @trim_box   = nil
end

Instance Attribute Details

#bleed_boxObject

Returns the value of attribute bleed_box



83
84
85
# File 'lib/pdf/writer/object/pages.rb', line 83

def bleed_box
  @bleed_box
end

#media_boxObject

Each of the following should be an array of 4 numbers, the x and y coordinates of the lower left and upper right bounds of the box.



82
83
84
# File 'lib/pdf/writer/object/pages.rb', line 82

def media_box
  @media_box
end

#procsetObject

Returns the value of attribute procset



79
80
81
# File 'lib/pdf/writer/object/pages.rb', line 79

def procset
  @procset
end

#trim_boxObject

Returns the value of attribute trim_box



84
85
86
# File 'lib/pdf/writer/object/pages.rb', line 84

def trim_box
  @trim_box
end

Instance Method Details

#<<(p) ⇒ Object

Add the page ID to the end of the page list.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pdf/writer/object/pages.rb', line 36

def <<(p)
  if p.kind_of?(PDF::Writer::Object::Page)
    @pages << p
  elsif p.kind_of?(PDF::Writer::Object::Font)
    @fonts << p
  elsif p.kind_of?(PDF::Writer::External)
    @xObjects << p
  else
    raise ArgumentError, PDF::Message[:req_FPXO]
  end
end

#add(p) ⇒ Object

Add a page to the page list. If p is just a Page, then it will be added to the page list. Otherwise, it will be treated as a Hash with keys :page, :pos, and :rpage. :page is the Page to be added to the list; :pos is :before or :after; :rpage is the Page to which the new Page will be added relative to.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pdf/writer/object/pages.rb', line 53

def add(p)
  if p.kind_of?(PDF::Writer::Object::Page)
    @pages << p
  elsif p.kind_of?(PDF::Writer::FontMetrics)
    @fonts << p
  elsif p.kind_of?(PDF::Writer::External)
    @xObjects << p
  elsif p.kind_of?(Hash)
    # Find a match.
    i = @pages.index(p[:rpage])
    unless i.nil?
      # There is a match; insert the page.
      case p[:pos]
      when :before
        @pages[i, 0] = p[:page]
      when :after
        @pages[i + 1, 0] = p[:page]
      else
        raise ArgumentError, PDF::Message[:invalid_pos]
      end
    end
  else
    raise ArgumentError, PDF::Message[:req_FPXOH]
  end
end

#first_pageObject



31
32
33
# File 'lib/pdf/writer/object/pages.rb', line 31

def first_page
  @pages[0]
end

#sizeObject



27
28
29
# File 'lib/pdf/writer/object/pages.rb', line 27

def size
  @pages.size
end

#to_sObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/pdf/writer/object/pages.rb', line 86

def to_s
  unless @pages.empty?
    res = "\n#{@oid} 0 obj\n<< /Type /Pages\n/Kids ["
    @pages.uniq! # uniqify the data...
    @pages.each { |p| res << "#{p.oid} 0 R\n" }
    res << "]\n/Count #{@pages.size}"
    unless @fonts.empty? and @procset.nil?
      res << "\n/Resources <<"
      res << "\n/ProcSet #{@procset.oid} 0 R" unless @procset.nil?
      unless @fonts.empty?
        res << "\n/Font << "
        @fonts.each { |f| res << "\n/F#{f.font_id} #{f.oid} 0 R" }
        res << " >>"
      end
      unless @xObjects.empty?
        res << "\n/XObject << "
        @xObjects.each { |x| res << "\n/#{x.label} #{x.oid} 0 R" }
        res << " >>"
      end
      res << "\n>>"
      res << "\n/MediaBox [#{@media_box.join(' ')}]" unless @media_box.nil? or @media_box.empty?
      res << "\n/BleedBox [#{@bleed_box.join(' ')}]" unless @bleed_box.nil? or @bleed_box.empty?
      res << "\n/TrimBox [#{@trim_box.join(' ')}]" unless @trim_box.nil? or @trim_box.empty?
    end
    res << "\n >>\nendobj"
  else
    "\n#{@oid} 0 obj\n<< /Type /Pages\n/Count 0\n>>\nendobj"
  end
end