acrobat 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop/minitest.yml +2 -0
- data/.rubocop/strict.yml +4 -0
- data/.rubocop.yml +23 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/Guardfile +28 -30
- data/{LICENSE.adoc → LICENSE.txt} +3 -4
- data/README.md +107 -0
- data/Rakefile +6 -54
- data/examples/6030.17.antenna.pdf +0 -0
- data/examples/6030.17.cm300.tx.pdf +0 -0
- data/examples/form_field.rb +21 -0
- data/examples/merge.rb +11 -0
- data/exe/acrobat +3 -0
- data/gemfiles/rubocop.gemfile +6 -0
- data/lib/acrobat/app.rb +197 -187
- data/lib/acrobat/avdoc.rb +22 -0
- data/lib/acrobat/jso.rb +87 -94
- data/lib/acrobat/pdoc.rb +155 -137
- data/lib/acrobat/version.rb +5 -0
- data/lib/acrobat.rb +5 -44
- data/rbs_collection.yaml +19 -0
- data/samples_other/background.js +40 -0
- data/samples_other/find_word.vb +440 -0
- data/samples_other/form.vb +393 -0
- data/sig/generated/acrobat/app.rbs +94 -0
- data/sig/generated/acrobat/avdoc.rbs +15 -0
- data/sig/generated/acrobat/jso.rbs +37 -0
- data/sig/generated/acrobat/pdoc.rbs +63 -0
- data/sig/generated/acrobat/version.rbs +5 -0
- data/sig/generated/acrobat.rbs +6 -0
- data/sig/prototype/lib/acrobat/app.rbs +3 -0
- data/sig/prototype/lib/acrobat/jso.rbs +5 -0
- data/sig/prototype/lib/acrobat/pdoc.rbs +5 -0
- data/vba/overlay.vba +40 -0
- metadata +45 -180
- data/.gitignore +0 -32
- data/History.adoc +0 -6
- data/Manifest.txt +0 -14
- data/README.adoc +0 -117
- data/bin/acrobat +0 -3
- data/test/acrobat_test.rb +0 -36
- data/test/test_helper.rb +0 -13
data/lib/acrobat/app.rb
CHANGED
@@ -1,187 +1,197 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
require_relative
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
@ole_obj
|
38
|
-
|
39
|
-
|
40
|
-
@
|
41
|
-
|
42
|
-
end
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
#
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
#
|
81
|
-
# @
|
82
|
-
#
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
end
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
#
|
127
|
-
# @
|
128
|
-
#
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
def
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
#
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
1
|
+
# rbs_inline: enable
|
2
|
+
|
3
|
+
require "win32ole"
|
4
|
+
require_relative "pdoc"
|
5
|
+
require_relative "avdoc"
|
6
|
+
require_relative "jso"
|
7
|
+
|
8
|
+
module Acrobat
|
9
|
+
class FileNotFound < StandardError
|
10
|
+
def initialize(path)
|
11
|
+
super("File not found: #{path}")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module FileSystemObject
|
17
|
+
@instance = nil
|
18
|
+
def self.instance
|
19
|
+
@instance ||= WIN32OLE.new("Scripting.FileSystemObject")
|
20
|
+
@instance
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.windows_path(path)
|
24
|
+
FileSystemObject.instance.GetAbsolutePathname(path.to_s)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module ACRO; end
|
29
|
+
|
30
|
+
module Acrobat
|
31
|
+
class App
|
32
|
+
# [WIN32_OLE] ole_obj
|
33
|
+
attr_reader :ole_obj # :WIN32OLE
|
34
|
+
|
35
|
+
def initialize # : Void
|
36
|
+
@ole_obj = WIN32OLE.new("AcroExch.App")
|
37
|
+
unless @ole_obj
|
38
|
+
@ole_obj = WIN32OLE.connect("AcroExch.App")
|
39
|
+
end
|
40
|
+
load_constants(@ole_obj)
|
41
|
+
@docs = []
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.close(path)
|
45
|
+
name = File.basename(path)
|
46
|
+
run do |app|
|
47
|
+
av = app.avdocs.find{|a| a.pdoc_name == name}
|
48
|
+
av&.close
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
# Runs the adobe app and quits at the end
|
55
|
+
# @example
|
56
|
+
# Acrobat::App.run do |app|
|
57
|
+
# doc = app.open('doc.pdf')
|
58
|
+
# doc.fill_form( city: 'City', state: 'ST')
|
59
|
+
# doc.save_as('filled.pdf')
|
60
|
+
# end
|
61
|
+
#
|
62
|
+
# @rbs return Void
|
63
|
+
def self.run
|
64
|
+
the_app = new
|
65
|
+
yield the_app
|
66
|
+
ensure
|
67
|
+
the_app&.quit
|
68
|
+
GC.start
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.replace_pages(pdf_file, replacement, output_name:, **opts)
|
72
|
+
run do |app|
|
73
|
+
app.open(pdf_file) do |doc|
|
74
|
+
doc.replace_pages(replacement, **opts)
|
75
|
+
doc.save_as(output_name)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Fills the form with updates in a hash
|
81
|
+
# @example
|
82
|
+
# Acrobat::App.fill_form(myform.pdf, output_name: 'filled.pdf
|
83
|
+
# , update_hash: { name: 'dom', filled_date: 1/20/2013
|
84
|
+
# @rbs pdf_form: String -- the String path of a fillable pdf file
|
85
|
+
# @rbs output_name: String -- the name of the saved filled pdf file
|
86
|
+
# @rbs update_hash: Hash -- the hash with updates
|
87
|
+
def self.fill_form(pdf_form, output_name:, update_hash:)
|
88
|
+
run do |app|
|
89
|
+
doc = app.open(pdf_form)
|
90
|
+
doc.fill_form(update_hash)
|
91
|
+
doc.save_as(output_name)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# @rbs return Array[AvDoc]
|
96
|
+
# @rbs &: (AvDoc) -> Void
|
97
|
+
def avdocs
|
98
|
+
count = ole_obj.GetNumAVDocs
|
99
|
+
return [] if count == 0
|
100
|
+
return to_enum(:avdocs) unless block_given?
|
101
|
+
av_array = []
|
102
|
+
count.times do |i|
|
103
|
+
ole = ole_obj.GetAVDoc(i)
|
104
|
+
raise "Wrong index for GetNumAVDocs #{i -1}" unless ole
|
105
|
+
av = AvDoc.new(ole)
|
106
|
+
if block_given?
|
107
|
+
yield av
|
108
|
+
else
|
109
|
+
av_array << av
|
110
|
+
end
|
111
|
+
end
|
112
|
+
av_array unless block_given?
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
# show the Adobe Acrobat application
|
117
|
+
def show # : Void
|
118
|
+
ole_obj.Show
|
119
|
+
end
|
120
|
+
|
121
|
+
# hide the Adobe Acrobat application
|
122
|
+
def hide # : Void
|
123
|
+
ole_obj.Hide
|
124
|
+
end
|
125
|
+
|
126
|
+
# Finds the pdfs in a dir
|
127
|
+
# @rbs dir: String -- the directory to find pdfs in
|
128
|
+
# @rbs return Array[Pathname] -- of pdf files
|
129
|
+
def find_pdfs_in_dir(dir)
|
130
|
+
Pathname.glob(dir + "/*.pdf")
|
131
|
+
end
|
132
|
+
|
133
|
+
def merge_pdfs(*pdfs)
|
134
|
+
pdf_array = Array(pdfs).flatten
|
135
|
+
raise "Not enough pdfs to merge" if pdf_array.size < 2
|
136
|
+
first, *rest = pdf_array
|
137
|
+
doc = self.open(first)
|
138
|
+
rest.each do |path|
|
139
|
+
doc.merge(path)
|
140
|
+
end
|
141
|
+
doc
|
142
|
+
end
|
143
|
+
|
144
|
+
# merges the pdfs in directory
|
145
|
+
# @rbs dir: String -- the path of the directory
|
146
|
+
# @rbs name: String | Nil -- the name of the returned pdf file
|
147
|
+
# if the name is nil, the name is "merged.pdf"
|
148
|
+
# @rbs output_dir: String | Nil -- the name of the output dir
|
149
|
+
# if the output_dir is nil, the output dir is the dir param
|
150
|
+
# return [Boolean] if the merge was successful or not
|
151
|
+
def merge_pdfs_in_dir(dir, name: nil, output_dir: nil)
|
152
|
+
name || "merged.pdf"
|
153
|
+
dir = output_dir || dir
|
154
|
+
merge_pdfs(find_pdfs_in_dir(dir))
|
155
|
+
end
|
156
|
+
|
157
|
+
# quit the Adobe App.
|
158
|
+
# closes the open adobe documents and quits the program
|
159
|
+
# @rbs return nil
|
160
|
+
def quit
|
161
|
+
begin
|
162
|
+
docs.each { |d| d.close }
|
163
|
+
ole_obj.Exit
|
164
|
+
rescue
|
165
|
+
return nil
|
166
|
+
end
|
167
|
+
nil
|
168
|
+
end
|
169
|
+
|
170
|
+
attr_reader :docs
|
171
|
+
|
172
|
+
# open the file.
|
173
|
+
# @rbs file: String | Pathname -- #to_path
|
174
|
+
# @rbs &: (PDoc) -> Nil
|
175
|
+
# @rbs return PDoc -- the open file as a Pdoc instance
|
176
|
+
def open(file)
|
177
|
+
doc = PDoc.from_path(file)
|
178
|
+
docs << doc
|
179
|
+
return doc unless block_given?
|
180
|
+
begin
|
181
|
+
yield doc
|
182
|
+
ensure
|
183
|
+
doc.close
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
def form
|
188
|
+
Form.new(self, WIN32OLE.new("AFormAut.App"))
|
189
|
+
end
|
190
|
+
|
191
|
+
private
|
192
|
+
|
193
|
+
def load_constants(ole_obj)
|
194
|
+
WIN32OLE.const_load(ole_obj, ACRO) unless ACRO.constants.size > 0
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Acrobat
|
2
|
+
class AvDoc
|
3
|
+
attr_reader :ole_obj #: WIN32OLE
|
4
|
+
|
5
|
+
attr_reader :pdoc_name #: String
|
6
|
+
|
7
|
+
def initialize( ole)
|
8
|
+
@ole_obj = ole
|
9
|
+
@pdoc_name = ole.GetPDDoc.GetFileName
|
10
|
+
end
|
11
|
+
|
12
|
+
# Close the AvDoc
|
13
|
+
# @rbs save: Boolean -- if true asks to save if false closes without saving
|
14
|
+
def close(save = false)
|
15
|
+
ole_save = save ? 0 : 1
|
16
|
+
@ole_obj.Close(ole_save)
|
17
|
+
@ole_obj = nil
|
18
|
+
true
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
data/lib/acrobat/jso.rb
CHANGED
@@ -1,94 +1,87 @@
|
|
1
|
-
module Acrobat
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
when
|
15
|
-
ole_get_field(name_or_number
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
end
|
71
|
-
|
72
|
-
def
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
set_field(k,v)
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
end
|
93
|
-
|
94
|
-
end
|
1
|
+
module Acrobat
|
2
|
+
class Jso
|
3
|
+
attr_reader :doc, :ole_obj
|
4
|
+
|
5
|
+
def initialize(doc, ole)
|
6
|
+
@doc = doc
|
7
|
+
@ole_obj = ole
|
8
|
+
end
|
9
|
+
|
10
|
+
def find_field(name_or_number)
|
11
|
+
case name_or_number
|
12
|
+
when String, Symbol
|
13
|
+
ole_get_field(name_or_number.to_s)
|
14
|
+
when Number
|
15
|
+
ole_get_field(name_or_number)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def ole_get_field(field)
|
20
|
+
ole_obj.getField(field)
|
21
|
+
end
|
22
|
+
|
23
|
+
def console
|
24
|
+
@console ||= ole_obj.console
|
25
|
+
end
|
26
|
+
|
27
|
+
def show_console
|
28
|
+
console.show
|
29
|
+
end
|
30
|
+
|
31
|
+
def field_names
|
32
|
+
result = []
|
33
|
+
count = field_count
|
34
|
+
0.upto(count - 1) do |i|
|
35
|
+
result << ole_obj.getNthFieldName(i)
|
36
|
+
end
|
37
|
+
result
|
38
|
+
end
|
39
|
+
|
40
|
+
def export_as_fdf(name)
|
41
|
+
end
|
42
|
+
|
43
|
+
def import_fdf(path)
|
44
|
+
end
|
45
|
+
|
46
|
+
def fields_hash
|
47
|
+
result = {}
|
48
|
+
field_names.each_with_object(result) do |name, h|
|
49
|
+
h[name] = get_field(name)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
|
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));
|
57
|
+
|
58
|
+
def set_field(name, value)
|
59
|
+
field = find_field(name)
|
60
|
+
field.Value = value.to_s if field
|
61
|
+
rescue
|
62
|
+
require "pry"
|
63
|
+
binding.pry
|
64
|
+
nil
|
65
|
+
end
|
66
|
+
|
67
|
+
def get_field(name)
|
68
|
+
field = find_field(name)
|
69
|
+
field&.Value
|
70
|
+
end
|
71
|
+
|
72
|
+
def field_count
|
73
|
+
ole_obj.numFields.to_int
|
74
|
+
end
|
75
|
+
|
76
|
+
def clear_form
|
77
|
+
ole_obj.resetForm
|
78
|
+
end
|
79
|
+
|
80
|
+
def fill_form(hash)
|
81
|
+
clear_form
|
82
|
+
hash.each do |k, v|
|
83
|
+
set_field(k, v)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|