acrobat 0.0.8 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/acrobat/jso.rb CHANGED
@@ -1,17 +1,15 @@
1
1
  module Acrobat
2
-
3
- class Jso
4
-
2
+ class Jso
5
3
  attr_reader :doc, :ole_obj
6
4
 
7
- def initialize(doc,ole)
5
+ def initialize(doc, ole)
8
6
  @doc = doc
9
7
  @ole_obj = ole
10
8
  end
11
9
 
12
10
  def find_field(name_or_number)
13
11
  case name_or_number
14
- when String,Symbol
12
+ when String, Symbol
15
13
  ole_get_field(name_or_number.to_s)
16
14
  when Number
17
15
  ole_get_field(name_or_number)
@@ -33,7 +31,7 @@ module Acrobat
33
31
  def field_names
34
32
  result = []
35
33
  count = field_count
36
- 0.upto(count-1) do |i|
34
+ 0.upto(count - 1) do |i|
37
35
  result << ole_obj.getNthFieldName(i)
38
36
  end
39
37
  result
@@ -45,37 +43,34 @@ module Acrobat
45
43
  def import_fdf(path)
46
44
  end
47
45
 
48
-
49
46
  def fields_hash
50
47
  result = {}
51
- field_names.each_with_object( result ) do |name, h|
48
+ field_names.each_with_object(result) do |name, h|
52
49
  h[name] = get_field(name)
53
50
  end
54
51
  end
55
52
 
56
53
 
57
- # // Enumerate through all of the fields in the document.
58
- # for (var i = 0; i < this.numFields; i++)
59
- # console.println("Field[" + i + "] = " + this.getNthFieldName(i));
54
+ # // Enumerate through all of the fields in the document.
55
+ # for (var i = 0; i < this.numFields; i++)
56
+ # console.println("Field[" + i + "] = " + this.getNthFieldName(i));
60
57
 
61
- def set_field(name,value)
62
- begin
58
+ def set_field(name, value)
63
59
  field = find_field(name)
64
60
  field.Value = value.to_s if field
65
- rescue
66
- require 'pry'
67
- binding.pry
68
- nil
69
- end
61
+ rescue
62
+ require "pry"
63
+ binding.pry
64
+ nil
70
65
  end
71
66
 
72
67
  def get_field(name)
73
68
  field = find_field(name)
74
- field.Value if field
69
+ field&.Value
75
70
  end
76
71
 
77
72
  def field_count
78
- ole_obj.numFields().to_int
73
+ ole_obj.numFields.to_int
79
74
  end
80
75
 
81
76
  def clear_form
@@ -84,11 +79,9 @@ module Acrobat
84
79
 
85
80
  def fill_form(hash)
86
81
  clear_form
87
- hash.each do |k,v|
88
- set_field(k,v)
82
+ hash.each do |k, v|
83
+ set_field(k, v)
89
84
  end
90
85
  end
91
-
92
- end
93
-
86
+ end
94
87
  end
data/lib/acrobat/pdoc.rb CHANGED
@@ -1,17 +1,15 @@
1
1
  module Acrobat
2
-
3
2
  class PDoc
4
-
5
3
  attr_reader :app, :ole_obj, :path
6
4
 
7
- def initialize(app,ole,path=nil)
5
+ def initialize(app, ole, path = nil)
8
6
  @app = app
9
7
  @ole_obj = ole
10
8
  @path = path
11
9
  end
12
10
 
13
11
  def show(name = nil)
14
- name = name || ole_obj.GetFileName
12
+ name ||= ole_obj.GetFileName
15
13
  ole_obj.OpenAVDoc(name)
16
14
  end
17
15
 
@@ -21,7 +19,7 @@ module Acrobat
21
19
  end
22
20
 
23
21
  def last_page
24
- page_count -1
22
+ page_count(-1)
25
23
  end
26
24
 
27
25
  # merges the doc to the
@@ -30,9 +28,9 @@ module Acrobat
30
28
  # @overload merge(doc)
31
29
  # @param doc [PDoc] an open PDoc to merge
32
30
  # @return [Boolean] whether the doc was merged correctly
33
- def merge(doc, options = {})
31
+ def merge(doc)
34
32
  src = open_pdoc(doc)
35
- merge_pdoc(src,options)
33
+ merge_pdoc(src)
36
34
  end
37
35
 
38
36
  # opens and/or returns PDoc
@@ -42,25 +40,34 @@ module Acrobat
42
40
  # @param doc [PDoc] an already open PDoc file
43
41
  # @return doc [PDOC] the opened PDoc
44
42
  def open_pdoc(doc)
45
- src = case doc
46
- when PDoc
47
- doc
48
- when String, Pathname
49
- docpath = Pathname(doc)
50
- raise 'File not found' unless docpath.file?
51
- doc2 = app.open(docpath)
52
- doc2
53
- end
54
- src
55
- end
56
-
57
- def fill_and_save(results,name: nil, dir: nil)
43
+ case doc
44
+ when PDoc
45
+ doc
46
+ when String, Pathname
47
+ docpath = Pathname(doc)
48
+ raise "File not found" unless docpath.file?
49
+ app.open(docpath)
50
+
51
+ end
52
+ end
53
+
54
+ def fill_and_save(results, name: nil, dir: nil)
58
55
  fill_form(results)
59
56
  is_saved = save_as(name: name, dir: dir)
60
57
  puts "saved file: %s" % [dir + name] if is_saved
61
58
  true
62
59
  end
63
60
 
61
+ def insert_pages(src:, insert_after: nil, src_page_start: nil, src_page_end: nil)
62
+ insert_hash = {"nPage" => insert_after - 1}
63
+ insert_hash["nStart"] = src_page_start + 1 if src_page_start
64
+ insert_hash["nEnd"] = src_page_end + 1 if src_page_end
65
+ ole_obj.InsertPages(**insert_hash)
66
+ end
67
+
68
+ def prepend_pages(src_path:, src_page_start: 1, src_page_end: nil)
69
+ insert_pages(insert_after: 0, src_path: src_path, src_page_start: src_page_start, src_page_end: src_page_end)
70
+ end
64
71
 
65
72
  # returns [Pathname] of d
66
73
  # @param dir [String, Nil] the String path
@@ -69,12 +76,12 @@ module Acrobat
69
76
  Pathname(dir || Pathname.getw)
70
77
  end
71
78
 
72
- def save_as(name,dir:nil)
73
- name = path.basename unless name
79
+ def save_as(name, dir: nil)
80
+ name ||= path.basename
74
81
  dir = Pathname(dir || Pathname.getwd)
75
82
  dir.mkpath
76
- windows_path = FileSystemObject.windows_path(dir + name )
77
- ole_obj.save(ACRO::PDSaveFull | ACRO::PDSaveCopy,windows_path)
83
+ windows_path = FileSystemObject.windows_path(dir + name)
84
+ ole_obj.save(ACRO::PDSaveFull | ACRO::PDSaveCopy, windows_path)
78
85
  end
79
86
 
80
87
  def name
@@ -82,19 +89,21 @@ module Acrobat
82
89
  end
83
90
 
84
91
  def close
85
- ole_obj.Close rescue nil
92
+ ole_obj.Close
93
+ rescue
94
+ nil
86
95
  end
87
96
 
88
- def replace_pages(doc, start: 0, replace_start: 0, num_of_pages: 1,merge_annotations: true)
97
+ def replace_pages(doc, start: 0, replace_start: 0, num_of_pages: 1, merge_annotations: true)
89
98
  src = open_pdoc(doc)
90
99
 
91
- ole_obj.ReplacePages(start,src.ole_obj,replace_start,num_of_pages,merge_annotations)
100
+ ole_obj.ReplacePages(start, src.ole_obj, replace_start, num_of_pages, merge_annotations)
92
101
  end
93
102
 
94
103
  # return the instance of JSO object
95
104
  # @return [Jso] a WIN32OLE wrapped Jso object 'javascript object'
96
105
  def jso
97
- @jso ||= Jso.new(self,ole_obj.GetJSObject)
106
+ @jso ||= Jso.new(self, ole_obj.GetJSObject)
98
107
  end
99
108
 
100
109
  # return the field_names of the pdf form
@@ -107,21 +116,19 @@ module Acrobat
107
116
  end
108
117
 
109
118
  protected
110
- def merge_pdoc(doc,options = {})
111
- begin
112
- unless options
113
- merged = ole_obj.InsertPages(page_count - 1, doc.ole_obj, 0, doc.page_count, true)
114
- return merged
115
- else
116
- start = options[:start]
117
- start_page
118
- pages = options[:pages]
119
- ole_obj.InsertPages(start, doc.ole_obj, 0, doc.page_count, true)
120
- end
121
- rescue
122
- return false
119
+
120
+ def merge_pdoc(doc, **options)
121
+ if options
122
+ start = options[:start]
123
+ start_page
124
+ options[:pages]
125
+ ole_obj.InsertPages(start, doc.ole_obj, 0, doc.page_count, true)
126
+ else
127
+ ole_obj.InsertPages(page_count - 1, doc.ole_obj, 0, doc.page_count, true)
128
+
123
129
  end
130
+ rescue
131
+ false
124
132
  end
125
133
  end
126
-
127
134
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Acrobat
4
+ VERSION = "0.2.0"
5
+ end
data/lib/acrobat.rb CHANGED
@@ -1,32 +1,30 @@
1
- require 'win32ole'
2
- require 'acrobat/app'
1
+ require "win32ole"
2
+ require "acrobat/app"
3
3
 
4
4
 
5
5
  module Acrobat
6
- VERSION = "0.0.8"
6
+ VERSION = "0.1.0"
7
7
  end
8
8
 
9
9
 
10
- if $0 == __FILE__
11
- require 'pry'
10
+ if $0 == __FILE__
11
+ require "pry"
12
12
 
13
- app = Acrobat::App.run do |app|
14
-
15
- data = Pathname(__dir__).parent + 'data'
16
- antenna_form = data + 'faa.6030.17.antenna.pdf'
13
+ Acrobat::App.run do |app|
14
+ data = Pathname(__dir__).parent + "data"
15
+ antenna_form = data + "faa.6030.17.antenna.pdf"
17
16
 
18
17
 
19
18
  doc1 = app.open(antenna_form)
20
19
  doc1.show
21
20
 
22
- fields = {'city' => 'OGD', 'state' => 'Utah',
23
- 'lid' => 'OGD',
24
- 'fac' => 'RTR',
25
- }
21
+ fields = {"city" => "OGD", "state" => "Utah",
22
+ "lid" => "OGD",
23
+ "fac" => "RTR"}
26
24
  doc1.fill_form(fields)
27
25
 
28
26
 
29
- doc1.save_as(name: 'ogd.rtr.pdf', dir: 'tmp')
27
+ doc1.save_as(name: "ogd.rtr.pdf", dir: "tmp")
30
28
  binding.pry
31
29
  jso = doc1.jso
32
30
  jso.show_console
@@ -36,13 +34,12 @@ if $0 == __FILE__
36
34
 
37
35
 
38
36
 
39
- doc2 = app.open(data + 'faa.6030.17.cm300.uhf.tx.pdf')
37
+ doc2 = app.open(data + "faa.6030.17.cm300.uhf.tx.pdf")
40
38
  doc2.show
41
39
  doc2.fill_form(fields)
42
40
 
43
41
  doc1.merge(doc2)
44
- doc1.save_as(name: 'ogd.merged_antenna_tx.pdf', dir: 'tmp')
45
-
42
+ doc1.save_as(name: "ogd.merged_antenna_tx.pdf", dir: "tmp")
46
43
  end
47
44
 
48
45
  end
@@ -0,0 +1,40 @@
1
+ Private Function ApplyBackgroundToPDF(BasePDF As String, BackgroundPDF As String)
2
+ Dim pdDoc As Acrobat.CAcroPDDoc
3
+ Dim pdTemplate As Acrobat.CAcroPDDoc
4
+ Dim template As Variant
5
+ Dim lngPage As Long
6
+
7
+ 'Open base document
8
+ Set pdDoc = CreateObject("AcroExch.PDDoc")
9
+ pdDoc.Open BasePDF
10
+ DoEvents
11
+
12
+ 'Open background document
13
+ Set pdTemplate = CreateObject("AcroExch.PDDoc")
14
+ pdTemplate.Open BackgroundPDF
15
+ DoEvents
16
+
17
+ 'Add background document to base document
18
+ pdDoc.InsertPages pdDoc.GetNumPages - 1, pdTemplate, 0, 1, 0
19
+
20
+ 'Create a template from the inserted background document
21
+ Set template = pdDoc.GetJSObject.CreateTemplate("background", pdDoc.GetNumPages - 1)
22
+
23
+ 'Place the template as a background to all pages
24
+ For lngPage = 0 To pdDoc.GetNumPages - 2
25
+ template.Spawn lngPage, True, True
26
+ Next
27
+
28
+ 'Delete last page (used for template creation purposes only)
29
+ pdDoc.DeletePages pdDoc.GetNumPages - 1, pdDoc.GetNumPages - 1
30
+
31
+ 'Save
32
+ pdDoc.Save 1, BasePDF
33
+
34
+ 'Close & Destroy Objects
35
+ pdDoc.Close
36
+ Set pdDoc = Nothing
37
+
38
+ pdTemplate.Close
39
+ Set pdTemplate = Nothing
40
+ End Function