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.
data/lib/acrobat/pdoc.rb CHANGED
@@ -1,137 +1,134 @@
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
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
@@ -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,48 +1,45 @@
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
- app = Acrobat::App.run do |app|
14
-
15
- data = Pathname(__dir__).parent + 'data'
16
- antenna_form = data + 'faa.6030.17.antenna.pdf'
17
-
18
-
19
- doc1 = app.open(antenna_form)
20
- doc1.show
21
-
22
- fields = {'city' => 'OGD', 'state' => 'Utah',
23
- 'lid' => 'OGD',
24
- 'fac' => 'RTR',
25
- }
26
- doc1.fill_form(fields)
27
-
28
-
29
- doc1.save_as(name: 'ogd.rtr.pdf', dir: 'tmp')
30
- binding.pry
31
- jso = doc1.jso
32
- jso.show_console
33
- puts "field count: #{jso.field_count}"
34
- puts "field names: \n#{jso.field_names}"
35
- binding.pry
36
-
37
-
38
-
39
- doc2 = app.open(data + 'faa.6030.17.cm300.uhf.tx.pdf')
40
- doc2.show
41
- doc2.fill_form(fields)
42
-
43
- doc1.merge(doc2)
44
- doc1.save_as(name: 'ogd.merged_antenna_tx.pdf', dir: 'tmp')
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