acrobat 0.1.0 → 0.2.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/.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/lib/acrobat/app.rb +172 -187
- data/lib/acrobat/jso.rb +87 -94
- data/lib/acrobat/pdoc.rb +134 -137
- data/lib/acrobat/version.rb +5 -0
- data/lib/acrobat.rb +45 -48
- data/samples_other/background.js +40 -0
- data/samples_other/find_word.vb +440 -0
- data/samples_other/form.vb +393 -0
- data/vba/overlay.vba +40 -0
- metadata +27 -190
- 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/pdoc.rb
CHANGED
@@ -1,137 +1,134 @@
|
|
1
|
-
module Acrobat
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
@
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
#
|
28
|
-
# @overload merge(doc)
|
29
|
-
# @param doc [
|
30
|
-
# @
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
#
|
39
|
-
# @overload open(
|
40
|
-
# @param doc [
|
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
|
-
insert_hash
|
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
|
-
# return
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
def merge_pdoc(doc, **options)
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
end
|
136
|
-
|
137
|
-
end
|
1
|
+
module Acrobat
|
2
|
+
class PDoc
|
3
|
+
attr_reader :app, :ole_obj, :path
|
4
|
+
|
5
|
+
def initialize(app, ole, path = nil)
|
6
|
+
@app = app
|
7
|
+
@ole_obj = ole
|
8
|
+
@path = path
|
9
|
+
end
|
10
|
+
|
11
|
+
def show(name = nil)
|
12
|
+
name ||= ole_obj.GetFileName
|
13
|
+
ole_obj.OpenAVDoc(name)
|
14
|
+
end
|
15
|
+
|
16
|
+
# @return [Fixnum] the number of pages in the pdf
|
17
|
+
def page_count
|
18
|
+
ole_obj.GetNumPages()
|
19
|
+
end
|
20
|
+
|
21
|
+
def last_page
|
22
|
+
page_count(-1)
|
23
|
+
end
|
24
|
+
|
25
|
+
# merges the doc to the
|
26
|
+
# @overload merge(doc)
|
27
|
+
# @param doc [String] the String path of a pdf file
|
28
|
+
# @overload merge(doc)
|
29
|
+
# @param doc [PDoc] an open PDoc to merge
|
30
|
+
# @return [Boolean] whether the doc was merged correctly
|
31
|
+
def merge(doc)
|
32
|
+
src = open_pdoc(doc)
|
33
|
+
merge_pdoc(src)
|
34
|
+
end
|
35
|
+
|
36
|
+
# opens and/or returns PDoc
|
37
|
+
# @overload open(doc)
|
38
|
+
# @param doc [String] the String path of a pdf file
|
39
|
+
# @overload open(PDoc] and open PDoc file
|
40
|
+
# @param doc [PDoc] an already open PDoc file
|
41
|
+
# @return doc [PDOC] the opened PDoc
|
42
|
+
def open_pdoc(doc)
|
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)
|
55
|
+
fill_form(results)
|
56
|
+
is_saved = save_as(name: name, dir: dir)
|
57
|
+
puts "saved file: %s" % [dir + name] if is_saved
|
58
|
+
true
|
59
|
+
end
|
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
|
71
|
+
|
72
|
+
# returns [Pathname] of d
|
73
|
+
# @param dir [String, Nil] the String path
|
74
|
+
# @return dir [Pathname] Pathname of dir or of working directory
|
75
|
+
def default_dir(dir)
|
76
|
+
Pathname(dir || Pathname.getw)
|
77
|
+
end
|
78
|
+
|
79
|
+
def save_as(name, dir: nil)
|
80
|
+
name ||= path.basename
|
81
|
+
dir = Pathname(dir || Pathname.getwd)
|
82
|
+
dir.mkpath
|
83
|
+
windows_path = FileSystemObject.windows_path(dir + name)
|
84
|
+
ole_obj.save(ACRO::PDSaveFull | ACRO::PDSaveCopy, windows_path)
|
85
|
+
end
|
86
|
+
|
87
|
+
def name
|
88
|
+
ole_obj.GetFileName
|
89
|
+
end
|
90
|
+
|
91
|
+
def close
|
92
|
+
ole_obj.Close
|
93
|
+
rescue
|
94
|
+
nil
|
95
|
+
end
|
96
|
+
|
97
|
+
def replace_pages(doc, start: 0, replace_start: 0, num_of_pages: 1, merge_annotations: true)
|
98
|
+
src = open_pdoc(doc)
|
99
|
+
|
100
|
+
ole_obj.ReplacePages(start, src.ole_obj, replace_start, num_of_pages, merge_annotations)
|
101
|
+
end
|
102
|
+
|
103
|
+
# return the instance of JSO object
|
104
|
+
# @return [Jso] a WIN32OLE wrapped Jso object 'javascript object'
|
105
|
+
def jso
|
106
|
+
@jso ||= Jso.new(self, ole_obj.GetJSObject)
|
107
|
+
end
|
108
|
+
|
109
|
+
# return the field_names of the pdf form
|
110
|
+
def field_names
|
111
|
+
jso.field_names
|
112
|
+
end
|
113
|
+
|
114
|
+
def fill_form(results)
|
115
|
+
jso.fill_form(results)
|
116
|
+
end
|
117
|
+
|
118
|
+
protected
|
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
|
+
|
129
|
+
end
|
130
|
+
rescue
|
131
|
+
false
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
data/lib/acrobat.rb
CHANGED
@@ -1,48 +1,45 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
|
4
|
-
|
5
|
-
module Acrobat
|
6
|
-
VERSION = "0.1.0"
|
7
|
-
end
|
8
|
-
|
9
|
-
|
10
|
-
if $0 ==
|
11
|
-
require
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
doc1
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
doc1.
|
30
|
-
|
31
|
-
|
32
|
-
jso.
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
doc2
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
1
|
+
require "win32ole"
|
2
|
+
require "acrobat/app"
|
3
|
+
|
4
|
+
|
5
|
+
module Acrobat
|
6
|
+
VERSION = "0.1.0"
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
if $0 == __FILE__
|
11
|
+
require "pry"
|
12
|
+
|
13
|
+
Acrobat::App.run do |app|
|
14
|
+
data = Pathname(__dir__).parent + "data"
|
15
|
+
antenna_form = data + "faa.6030.17.antenna.pdf"
|
16
|
+
|
17
|
+
|
18
|
+
doc1 = app.open(antenna_form)
|
19
|
+
doc1.show
|
20
|
+
|
21
|
+
fields = {"city" => "OGD", "state" => "Utah",
|
22
|
+
"lid" => "OGD",
|
23
|
+
"fac" => "RTR"}
|
24
|
+
doc1.fill_form(fields)
|
25
|
+
|
26
|
+
|
27
|
+
doc1.save_as(name: "ogd.rtr.pdf", dir: "tmp")
|
28
|
+
binding.pry
|
29
|
+
jso = doc1.jso
|
30
|
+
jso.show_console
|
31
|
+
puts "field count: #{jso.field_count}"
|
32
|
+
puts "field names: \n#{jso.field_names}"
|
33
|
+
binding.pry
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
doc2 = app.open(data + "faa.6030.17.cm300.uhf.tx.pdf")
|
38
|
+
doc2.show
|
39
|
+
doc2.fill_form(fields)
|
40
|
+
|
41
|
+
doc1.merge(doc2)
|
42
|
+
doc1.save_as(name: "ogd.merged_antenna_tx.pdf", dir: "tmp")
|
43
|
+
end
|
44
|
+
|
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
|