acrobat 0.0.8 → 0.1.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/.gitignore +31 -31
- data/History.adoc +6 -6
- data/LICENSE.adoc +22 -22
- data/Manifest.txt +14 -15
- data/README.adoc +117 -117
- data/Rakefile +58 -48
- data/bin/acrobat +3 -3
- data/lib/acrobat.rb +48 -48
- data/lib/acrobat/app.rb +187 -187
- data/lib/acrobat/jso.rb +94 -94
- data/lib/acrobat/pdoc.rb +137 -127
- data/test/acrobat_test.rb +36 -36
- data/test/test_helper.rb +13 -13
- metadata +43 -63
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -1
- data/.autotest +0 -25
- metadata.gz.sig +0 -2
data/lib/acrobat/jso.rb
CHANGED
@@ -1,94 +1,94 @@
|
|
1
|
-
module Acrobat
|
2
|
-
|
3
|
-
class Jso
|
4
|
-
|
5
|
-
attr_reader :doc, :ole_obj
|
6
|
-
|
7
|
-
def initialize(doc,ole)
|
8
|
-
@doc = doc
|
9
|
-
@ole_obj = ole
|
10
|
-
end
|
11
|
-
|
12
|
-
def find_field(name_or_number)
|
13
|
-
case name_or_number
|
14
|
-
when String,Symbol
|
15
|
-
ole_get_field(name_or_number.to_s)
|
16
|
-
when Number
|
17
|
-
ole_get_field(name_or_number)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def ole_get_field(field)
|
22
|
-
ole_obj.getField(field)
|
23
|
-
end
|
24
|
-
|
25
|
-
def console
|
26
|
-
@console ||= ole_obj.console
|
27
|
-
end
|
28
|
-
|
29
|
-
def show_console
|
30
|
-
console.show
|
31
|
-
end
|
32
|
-
|
33
|
-
def field_names
|
34
|
-
result = []
|
35
|
-
count = field_count
|
36
|
-
0.upto(count-1) do |i|
|
37
|
-
result << ole_obj.getNthFieldName(i)
|
38
|
-
end
|
39
|
-
result
|
40
|
-
end
|
41
|
-
|
42
|
-
def export_as_fdf(name)
|
43
|
-
end
|
44
|
-
|
45
|
-
def import_fdf(path)
|
46
|
-
end
|
47
|
-
|
48
|
-
|
49
|
-
def fields_hash
|
50
|
-
result = {}
|
51
|
-
field_names.each_with_object( result ) do |name, h|
|
52
|
-
h[name] = get_field(name)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
|
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));
|
60
|
-
|
61
|
-
def set_field(name,value)
|
62
|
-
begin
|
63
|
-
field = find_field(name)
|
64
|
-
field.Value = value.to_s if field
|
65
|
-
rescue
|
66
|
-
require 'pry'
|
67
|
-
binding.pry
|
68
|
-
nil
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def get_field(name)
|
73
|
-
field = find_field(name)
|
74
|
-
field.Value if field
|
75
|
-
end
|
76
|
-
|
77
|
-
def field_count
|
78
|
-
ole_obj.numFields().to_int
|
79
|
-
end
|
80
|
-
|
81
|
-
def clear_form
|
82
|
-
ole_obj.resetForm
|
83
|
-
end
|
84
|
-
|
85
|
-
def fill_form(hash)
|
86
|
-
clear_form
|
87
|
-
hash.each do |k,v|
|
88
|
-
set_field(k,v)
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
end
|
93
|
-
|
94
|
-
end
|
1
|
+
module Acrobat
|
2
|
+
|
3
|
+
class Jso
|
4
|
+
|
5
|
+
attr_reader :doc, :ole_obj
|
6
|
+
|
7
|
+
def initialize(doc,ole)
|
8
|
+
@doc = doc
|
9
|
+
@ole_obj = ole
|
10
|
+
end
|
11
|
+
|
12
|
+
def find_field(name_or_number)
|
13
|
+
case name_or_number
|
14
|
+
when String,Symbol
|
15
|
+
ole_get_field(name_or_number.to_s)
|
16
|
+
when Number
|
17
|
+
ole_get_field(name_or_number)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def ole_get_field(field)
|
22
|
+
ole_obj.getField(field)
|
23
|
+
end
|
24
|
+
|
25
|
+
def console
|
26
|
+
@console ||= ole_obj.console
|
27
|
+
end
|
28
|
+
|
29
|
+
def show_console
|
30
|
+
console.show
|
31
|
+
end
|
32
|
+
|
33
|
+
def field_names
|
34
|
+
result = []
|
35
|
+
count = field_count
|
36
|
+
0.upto(count-1) do |i|
|
37
|
+
result << ole_obj.getNthFieldName(i)
|
38
|
+
end
|
39
|
+
result
|
40
|
+
end
|
41
|
+
|
42
|
+
def export_as_fdf(name)
|
43
|
+
end
|
44
|
+
|
45
|
+
def import_fdf(path)
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
def fields_hash
|
50
|
+
result = {}
|
51
|
+
field_names.each_with_object( result ) do |name, h|
|
52
|
+
h[name] = get_field(name)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
|
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));
|
60
|
+
|
61
|
+
def set_field(name,value)
|
62
|
+
begin
|
63
|
+
field = find_field(name)
|
64
|
+
field.Value = value.to_s if field
|
65
|
+
rescue
|
66
|
+
require 'pry'
|
67
|
+
binding.pry
|
68
|
+
nil
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def get_field(name)
|
73
|
+
field = find_field(name)
|
74
|
+
field.Value if field
|
75
|
+
end
|
76
|
+
|
77
|
+
def field_count
|
78
|
+
ole_obj.numFields().to_int
|
79
|
+
end
|
80
|
+
|
81
|
+
def clear_form
|
82
|
+
ole_obj.resetForm
|
83
|
+
end
|
84
|
+
|
85
|
+
def fill_form(hash)
|
86
|
+
clear_form
|
87
|
+
hash.each do |k,v|
|
88
|
+
set_field(k,v)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
data/lib/acrobat/pdoc.rb
CHANGED
@@ -1,127 +1,137 @@
|
|
1
|
-
module Acrobat
|
2
|
-
|
3
|
-
class PDoc
|
4
|
-
|
5
|
-
attr_reader :app, :ole_obj, :path
|
6
|
-
|
7
|
-
def initialize(app,ole,path=nil)
|
8
|
-
@app = app
|
9
|
-
@ole_obj = ole
|
10
|
-
@path = path
|
11
|
-
end
|
12
|
-
|
13
|
-
def show(name = nil)
|
14
|
-
name = name || ole_obj.GetFileName
|
15
|
-
ole_obj.OpenAVDoc(name)
|
16
|
-
end
|
17
|
-
|
18
|
-
# @return [Fixnum] the number of pages in the pdf
|
19
|
-
def page_count
|
20
|
-
ole_obj.GetNumPages()
|
21
|
-
end
|
22
|
-
|
23
|
-
def last_page
|
24
|
-
page_count -1
|
25
|
-
end
|
26
|
-
|
27
|
-
# merges the doc to the
|
28
|
-
# @overload merge(doc)
|
29
|
-
# @param doc [String] the String path of a pdf file
|
30
|
-
# @overload merge(doc)
|
31
|
-
# @param doc [PDoc] an open PDoc to merge
|
32
|
-
# @return [Boolean] whether the doc was merged correctly
|
33
|
-
def merge(doc
|
34
|
-
src = open_pdoc(doc)
|
35
|
-
merge_pdoc(src
|
36
|
-
end
|
37
|
-
|
38
|
-
# opens and/or returns PDoc
|
39
|
-
# @overload open(doc)
|
40
|
-
# @param doc [String] the String path of a pdf file
|
41
|
-
# @overload open(PDoc] and open PDoc file
|
42
|
-
# @param doc [PDoc] an already open PDoc file
|
43
|
-
# @return doc [PDOC] the opened PDoc
|
44
|
-
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)
|
58
|
-
fill_form(results)
|
59
|
-
is_saved = save_as(name: name, dir: dir)
|
60
|
-
puts "saved file: %s" % [dir + name] if is_saved
|
61
|
-
true
|
62
|
-
end
|
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
|
-
ole_obj.
|
92
|
-
end
|
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
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
1
|
+
module Acrobat
|
2
|
+
|
3
|
+
class PDoc
|
4
|
+
|
5
|
+
attr_reader :app, :ole_obj, :path
|
6
|
+
|
7
|
+
def initialize(app,ole,path=nil)
|
8
|
+
@app = app
|
9
|
+
@ole_obj = ole
|
10
|
+
@path = path
|
11
|
+
end
|
12
|
+
|
13
|
+
def show(name = nil)
|
14
|
+
name = name || ole_obj.GetFileName
|
15
|
+
ole_obj.OpenAVDoc(name)
|
16
|
+
end
|
17
|
+
|
18
|
+
# @return [Fixnum] the number of pages in the pdf
|
19
|
+
def page_count
|
20
|
+
ole_obj.GetNumPages()
|
21
|
+
end
|
22
|
+
|
23
|
+
def last_page
|
24
|
+
page_count -1
|
25
|
+
end
|
26
|
+
|
27
|
+
# merges the doc to the
|
28
|
+
# @overload merge(doc)
|
29
|
+
# @param doc [String] the String path of a pdf file
|
30
|
+
# @overload merge(doc)
|
31
|
+
# @param doc [PDoc] an open PDoc to merge
|
32
|
+
# @return [Boolean] whether the doc was merged correctly
|
33
|
+
def merge(doc)
|
34
|
+
src = open_pdoc(doc)
|
35
|
+
merge_pdoc(src)
|
36
|
+
end
|
37
|
+
|
38
|
+
# opens and/or returns PDoc
|
39
|
+
# @overload open(doc)
|
40
|
+
# @param doc [String] the String path of a pdf file
|
41
|
+
# @overload open(PDoc] and open PDoc file
|
42
|
+
# @param doc [PDoc] an already open PDoc file
|
43
|
+
# @return doc [PDOC] the opened PDoc
|
44
|
+
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)
|
58
|
+
fill_form(results)
|
59
|
+
is_saved = save_as(name: name, dir: dir)
|
60
|
+
puts "saved file: %s" % [dir + name] if is_saved
|
61
|
+
true
|
62
|
+
end
|
63
|
+
|
64
|
+
def insert_pages(src: , insert_after: nil, src_page_start: nil, src_page_end: nil)
|
65
|
+
insert_hash = { 'nPage' => insert_after -1 }
|
66
|
+
insert_hash['nStart'] = src_page_start + 1 if src_page_start
|
67
|
+
insert_hash['nEnd'] = src_page_end + 1 if src_page_end
|
68
|
+
ole_obj.InsertPages(**insert_hash)
|
69
|
+
end
|
70
|
+
|
71
|
+
def prepend_pages(src_path: , src_page_start: 1, src_page_end: nil)
|
72
|
+
insert_pages( insert_after: 0, src_path: src_path, src_page_start: src_page_start, src_page_end: src_page_end)
|
73
|
+
end
|
74
|
+
|
75
|
+
# returns [Pathname] of d
|
76
|
+
# @param dir [String, Nil] the String path
|
77
|
+
# @return dir [Pathname] Pathname of dir or of working directory
|
78
|
+
def default_dir(dir)
|
79
|
+
Pathname(dir || Pathname.getw)
|
80
|
+
end
|
81
|
+
|
82
|
+
def save_as(name,dir:nil)
|
83
|
+
name = path.basename unless name
|
84
|
+
dir = Pathname(dir || Pathname.getwd)
|
85
|
+
dir.mkpath
|
86
|
+
windows_path = FileSystemObject.windows_path(dir + name )
|
87
|
+
ole_obj.save(ACRO::PDSaveFull | ACRO::PDSaveCopy,windows_path)
|
88
|
+
end
|
89
|
+
|
90
|
+
def name
|
91
|
+
ole_obj.GetFileName
|
92
|
+
end
|
93
|
+
|
94
|
+
def close
|
95
|
+
ole_obj.Close rescue nil
|
96
|
+
end
|
97
|
+
|
98
|
+
def replace_pages(doc, start: 0, replace_start: 0, num_of_pages: 1,merge_annotations: true)
|
99
|
+
src = open_pdoc(doc)
|
100
|
+
|
101
|
+
ole_obj.ReplacePages(start,src.ole_obj,replace_start,num_of_pages,merge_annotations)
|
102
|
+
end
|
103
|
+
|
104
|
+
# return the instance of JSO object
|
105
|
+
# @return [Jso] a WIN32OLE wrapped Jso object 'javascript object'
|
106
|
+
def jso
|
107
|
+
@jso ||= Jso.new(self,ole_obj.GetJSObject)
|
108
|
+
end
|
109
|
+
|
110
|
+
# return the field_names of the pdf form
|
111
|
+
def field_names
|
112
|
+
jso.field_names
|
113
|
+
end
|
114
|
+
|
115
|
+
def fill_form(results)
|
116
|
+
jso.fill_form(results)
|
117
|
+
end
|
118
|
+
|
119
|
+
protected
|
120
|
+
def merge_pdoc(doc, **options)
|
121
|
+
begin
|
122
|
+
unless options
|
123
|
+
merged = ole_obj.InsertPages(page_count - 1, doc.ole_obj, 0, doc.page_count, true)
|
124
|
+
return merged
|
125
|
+
else
|
126
|
+
start = options[:start]
|
127
|
+
start_page
|
128
|
+
pages = options[:pages]
|
129
|
+
ole_obj.InsertPages(start, doc.ole_obj, 0, doc.page_count, true)
|
130
|
+
end
|
131
|
+
rescue
|
132
|
+
return false
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|