Class: Pdfrb::Document::PageLabels

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/document/page_labels.rb

Overview

Page Labels facade. Generates the /PageLabels number tree on the Catalog for custom page numbering (roman numerals, prefixed arabic, etc.).

Usage:

doc.page_labels.set do
style :roman, count: 5      # i, ii, iii, iv, v
style :decimal, prefix: "A-" # A-1, A-2, ...
end

Defined Under Namespace

Classes: LabelRule

Constant Summary collapse

STYLES =
{
  decimal: :D,
  roman_lower: :r,
  roman_upper: :R,
  alpha_lower: :a,
  alpha_upper: :A,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ PageLabels

Returns a new instance of PageLabels.



27
28
29
30
31
# File 'lib/pdfrb/document/page_labels.rb', line 27

def initialize(document)
  @document = document
  @rules = []
  @current_index = 0
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



25
26
27
# File 'lib/pdfrb/document/page_labels.rb', line 25

def document
  @document
end

#rulesObject (readonly)

Returns the value of attribute rules.



25
26
27
# File 'lib/pdfrb/document/page_labels.rb', line 25

def rules
  @rules
end

Instance Method Details

#commit!Object

Build the /PageLabels number tree and set it on the Catalog.



52
53
54
55
56
57
# File 'lib/pdfrb/document/page_labels.rb', line 52

def commit!
  return if @rules.empty?

  nums = build_nums_array
  document.catalog.value[:PageLabels] = { Nums: nums }
end

#style(style, prefix: nil, start: 1, count: nil) ⇒ Object

Add a page labeling rule for the next batch of pages.

Parameters:

  • style (Symbol)

    :decimal, :roman_lower, :roman_upper, :alpha_lower, :alpha_upper.

  • prefix (String) (defaults to: nil)

    prefix prepended to each label.

  • start (Integer) (defaults to: 1)

    starting number (default 1).

  • count (Integer, nil) (defaults to: nil)

    number of pages this rule covers (nil = until next rule or end).



40
41
42
43
44
45
46
47
48
49
# File 'lib/pdfrb/document/page_labels.rb', line 40

def style(style, prefix: nil, start: 1, count: nil)
  @rules << LabelRule.new(
    style: STYLES.fetch(style),
    prefix: prefix,
    start: start,
    page_count: count
  )
  @current_index += count if count
  self
end