Method: HexaPDF::Type::OptionalContentConfiguration#add_ocg_to_ui

Defined in:
lib/hexapdf/type/optional_content_configuration.rb

#add_ocg_to_ui(ocg, path: nil) ⇒ Object

Makes the given optional content group visible in an interactive PDF processor’s user interface.

The OCG is always added to the end of the specified path or, if path is not specified, the top level.

The optional argument path specifies the strings or OCGs under which the given OCG should hierarchically be nested. A string is used as a non-selectable label, an OCG reflects an actual nesting of the involved OCGs.

Examples:

configuration.add_ocg_to_ui(ocg)                   # Add the OCG as top-level item
configuration.add_ocg_to_ui(ocg, path: 'Debug')    # Add the OCG under the label 'Debug'
# Add the OCG under the label 'Page1' which is under the label 'Debug'
configuration.add_ocg_to_ui(ocg, path: ['Debug', 'Page1'])
configuration.add_ocg_to_ui(ocg, path: other_ocg)  # Add the OCG under the other OCG


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/hexapdf/type/optional_content_configuration.rb', line 134

def add_ocg_to_ui(ocg, path: nil)
  array = self[:Order] ||= []
  path = Array(path)
  until path.empty?
    item = path.shift
    index = array.index do |entry|
      if (entry.kind_of?(Array) || entry.kind_of?(PDFArray)) && item.kind_of?(String)
        entry.first == item
      else
        entry == item
      end
    end

    if item.kind_of?(String)
      unless index
        array << [item]
        index = -1
      end
      array = array[index]
    else
      unless index
        array << item << []
        index = -2
      end
      unless array[index + 1].kind_of?(Array) || array[index + 1].kind_of?(PDFArray)
        array.insert(index + 1, [])
      end
      array = array[index + 1]
    end
  end
  array << ocg
end