Class: PdfOutlineEditor::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf_outline_editor/loader.rb

Constant Summary collapse

JavaFile =
java.io.File
IOException =
java.io.IOException

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_pdf_path) ⇒ Loader

Returns a new instance of Loader.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pdf_outline_editor/loader.rb', line 25

def initialize(input_pdf_path)
  begin
    @doc = PDDocument.load(JavaFile.new(input_pdf_path))
  rescue IOException => e
    raise Error, e.message
  end

  @doc.set_all_security_to_be_removed(true)

  @pages = @doc.pages.to_a

  @closed = false
end

Instance Attribute Details

#closedObject (readonly)

Returns the value of attribute closed.



23
24
25
# File 'lib/pdf_outline_editor/loader.rb', line 23

def closed
  @closed
end

Class Method Details

.open(input_pdf_path) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/pdf_outline_editor/loader.rb', line 13

def self.open(input_pdf_path)
  loader = new(input_pdf_path)

  begin
    yield loader
  ensure
    loader.close
  end
end

Instance Method Details

#closeObject



53
54
55
56
57
58
# File 'lib/pdf_outline_editor/loader.rb', line 53

def close
  return if @closed

  @doc.close
  @closed = true
end

#load(entries) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/pdf_outline_editor/loader.rb', line 39

def load(entries)
  root_outline = PDDocumentOutline.new

  @doc.document_catalog.document_outline = root_outline

  entries.each do |entry|
    set_outline(root_outline, entry)
  end
end

#save(output_pdf_path) ⇒ Object



49
50
51
# File 'lib/pdf_outline_editor/loader.rb', line 49

def save(output_pdf_path)
  @doc.save(output_pdf_path)
end