Module: HexaPDF::Task::ImportPages

Defined in:
lib/hexapdf/task/import_pages.rb

Overview

Task for importing pages from another document that preserves the visual appearance.

It takes care of

  • importing the specified pages with all associated objects,
  • handling optional content groups and their default state,
  • and merging form fields.

Note that the /Order, /AS and /Locked fields of the default optional content configuration dictionary are not preserved.

The imported page can optionally be resized to a given page size. Note, however, that this will make all interactive elements (e.g. annotation widgets and form fields) static.

Example:

doc.task(:import_pages, source: source_doc, pages: [1..-2])

Class Method Summary collapse

Class Method Details

.call(doc, source:, pages: :all, append: true, ocgs: :preserve, acro_form: :merge, resize_to: nil) ⇒ Object

Performs the necessary steps to import the pages from the source docment into the target document doc. Returns the imported pages.

source

Specifies the source PDF document from which the pages should be imported.

pages

Specifies the pages that should be imported. The argument has to be one of the following:

+:all+:: Imports all pages from the +source+ document.
Integer value:: Imports the page with the given zero-based index.
Range value:: Imports the pages from the zero-based range.
Array of Integer or Range values:: Imports all specified pages or page ranges.
Array of source page objects:: Imports the given pages.

:append:: Specifies whether the imported pages should be appended to the target document's page tree.

ocgs

Specifies the handling of optional content groups:

+:preserve+:: Preserve the on/off state for all used OCGs.
+:ignore+:: Ignore the on/off state.

:acro_form:: Specifies whether AcroForm fields should be merged into the target document.

+:merge+:: Merge AcroForm fields using the MergeAcroForm task.
+:ignore+:: Ignore AcroForm fields.

:resize_to:: Resizes the pages to the given page size (either a pre-defined name or an array specifying the media box), potentially deforming it. This is done by converting the page to a Form XObject and then painting it.

Note 1: By specifying this argument, the argument +acro_form+ is handled as if it were
:ignore.

Note 2: Annotations without an appearance stream are ignored. If that is something that
needs to be considered, +page.flatten_annotations+ should be called beforehand for each
source page and the results handled.


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/hexapdf/task/import_pages.rb', line 105

def self.call(doc, source:, pages: :all, append: true, ocgs: :preserve, acro_form: :merge,
              resize_to: nil)
  # Retrieve all specified source pages
  pages = if pages == :all
            source.pages.each.to_a
          elsif pages.kind_of?(Integer)
            [source.pages[pages]]
          elsif pages.kind_of?(Array) && pages[0].kind_of?(HexaPDF::Type::Page)
            pages
          else
            result = Set.new
            all_pages = source.pages.each.to_a
            pages = [pages] unless pages.kind_of?(Array)
            pages.each {|selector| result.merge(Array(all_pages[selector])) }
            result
          end

  # Import the source pages and optionally append them to the target page tree
  pages = pages.map do |page|
    imported_page = doc.import(page)
    imported_page = resize_page(doc, imported_page, resize_to) if resize_to
    doc.pages << imported_page if append
    imported_page
  end

  doc.task(:merge_acro_form, source: source, pages: pages) if acro_form == :merge
  preserve_ocgs(doc, source, pages) if ocgs == :preserve

  pages
end