acrobat 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/acrobat/pdoc.rb CHANGED
@@ -1,137 +1,155 @@
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 :ole_obj
4
+
5
+ attr_reader :path #: Path | Nil
6
+
7
+ def self.from_path(path)
8
+ filepath = Pathname(path).expand_path
9
+ raise FileNotFound.new(filepath) unless filepath.file?
10
+ pdoc = WIN32OLE.new("AcroExch.PDDoc")
11
+ result = pdoc.open FileSystemObject.windows_path(filepath)
12
+ raise "Error opening Acrobat::Pdoc from #{filepath}" if result == 0
13
+ new(pdoc, path)
14
+ end
15
+
16
+ def initialize(ole, path = nil)
17
+ @ole_obj = ole
18
+ @path = path
19
+ end
20
+
21
+ def show(name = nil)
22
+ name ||= ole_obj.GetFileName
23
+ ole_obj.OpenAVDoc(name)
24
+ end
25
+
26
+ # @rbs return Fixnum -- the number of pages in the pdf
27
+ def page_count
28
+ ole_obj.GetNumPages()
29
+ end
30
+
31
+ def last_page # : Fixnum
32
+ page_count - 1
33
+ end
34
+
35
+ # merges the doc to the
36
+ # @rbs doc: PDoc|String|Pathname -- an open PDoc to merge or filename
37
+ # @rbs return Boolean -- whether the doc was merged correctly
38
+ def merge(doc)
39
+ src = open_ole_pdoc(doc)
40
+ ole_insert_pages(src, self_start_page: last_page, merge_doc_page_start: 0,
41
+ number_pages: src.page_count, bookmarks: true)
42
+ end
43
+
44
+ # opens and/or returns PDoc
45
+ # @rbs doc: String|Pathname|Pdoc -- the String path of a pdf file
46
+ def open_ole_pdoc(doc) # : Pdoc
47
+ case doc
48
+ when PDoc
49
+ doc
50
+ when String, Pathname
51
+ self.class.from_path(doc)
52
+ end
53
+ end
54
+
55
+ def fill_and_save(results, name: nil, dir: nil)
56
+ fill_form(results)
57
+ is_saved = save_as(name: name, dir: dir)
58
+ puts "saved file: %s" % [dir + name] if is_saved
59
+ true
60
+ end
61
+
62
+ def prepend_pages(src_path:, src_page_start: 1, src_page_end: nil)
63
+ insert_pages(insert_after: 0, src_path: src_path, src_page_start: src_page_start, src_page_end: src_page_end)
64
+ end
65
+
66
+ # returns [Pathname] of d
67
+ # @rbs dir: [String, Nil] the String path
68
+ # @rbs return dir [Pathname] Pathname of dir or of working directory
69
+ def default_dir(dir)
70
+ Pathname(dir || Pathname.getw)
71
+ end
72
+
73
+ def save_as(name, dir: nil)
74
+ name ||= path.basename
75
+ dir = Pathname(dir || Pathname.getwd)
76
+ dir.mkpath
77
+ windows_path = FileSystemObject.windows_path(dir + name)
78
+ ole_obj.save(ACRO::PDSaveFull | ACRO::PDSaveCopy, windows_path)
79
+ end
80
+
81
+ def name
82
+ ole_obj.GetFileName
83
+ end
84
+
85
+ def close # : Bool -- true if closes successfully
86
+ result = ole_obj.Close
87
+ result == -1
88
+ rescue
89
+ false
90
+ end
91
+
92
+ def replace_pages(doc, start: 0, replace_start: 0, num_of_pages: 1, merge_annotations: true)
93
+ src = open_ole_pdoc(doc)
94
+
95
+ ole_obj.ReplacePages(start, src.ole_obj, replace_start, num_of_pages, merge_annotations)
96
+ end
97
+
98
+ # return the instance of JSO object
99
+ # @rbs return [Jso] a WIN32OLE wrapped Jso object 'javascript object'
100
+ def jso
101
+ @jso ||= Jso.new(self, ole_obj.GetJSObject)
102
+ end
103
+
104
+ # return the field_names of the pdf form
105
+ def field_names
106
+ jso.field_names
107
+ end
108
+
109
+ def fill_form(results)
110
+ jso.fill_form(results)
111
+ end
112
+
113
+ protected
114
+
115
+ def ole_insert_pages(merge_doc, self_start_page: 0, merge_doc_page_start: 0,
116
+ number_pages: nil, bookmarks: true)
117
+ bookmarks_ole = bookmarks ? 1 : 0
118
+ result = ole_obj.InsertPages(self_start_page, merge_doc.ole_obj, merge_doc_page_start, number_pages, bookmarks_ole)
119
+ if result == 0
120
+ raise "ole.InsertPages unable to insert merge doc #{merge_doc.name} into #{name}"
121
+ end
122
+ true
123
+ end
124
+
125
+ def ole_replace_pages(merge_doc, self_start_page: nil, merge_doc_page_start: nil,
126
+ number_pages: nil, bookmarks: true)
127
+ self_start_page_ole = self_start_page ? self_start_page - 1 : page_count - 1
128
+ page_start_ole = merge_doc_page_start ? merge_doc_page_start - 1 : merge_doc.GetNumPages - 1
129
+ number_pages_ole = number_pages || merge_doc
130
+ bookmarks_ole = bookmarks ? 1 : 0
131
+ result = ole_obj.ReplacePages(self_start_page_ole, merge_doc.ole, page_start_ole, number_pages_ole, bookmarks_ole)
132
+ end
133
+
134
+ def insert_pages(src:, insert_after: nil, src_page_start: nil, src_page_end: nil)
135
+ insert_hash = {"nPage" => insert_after - 1}
136
+ insert_hash["nStart"] = src_page_start + 1 if src_page_start
137
+ insert_hash["nEnd"] = src_page_end + 1 if src_page_end
138
+ ole_obj.InsertPages(**insert_hash)
139
+ end
140
+
141
+ def merge_pdoc(doc, **options)
142
+ if options
143
+ start = options[:start]
144
+ start_page
145
+ options[:pages]
146
+ ole_obj.InsertPages(start, doc.ole_obj, 0, doc.page_count, true)
147
+ else
148
+ ole_obj.InsertPages(page_count - 1, doc.ole_obj, 0, doc.page_count, true)
149
+
150
+ end
151
+ rescue
152
+ false
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Acrobat
4
+ VERSION = "0.3.0"
5
+ end
data/lib/acrobat.rb CHANGED
@@ -1,48 +1,9 @@
1
- require 'win32ole'
2
- require 'acrobat/app'
1
+ # frozen_string_literal: true; -*- rbs_inline: enabled -*-
3
2
 
3
+ require_relative "acrobat/version"
4
+ require_relative "acrobat/app"
4
5
 
5
6
  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
-
7
+ class Error < StandardError; end
8
+ # Your code goes here...
48
9
  end
@@ -0,0 +1,19 @@
1
+ # Download sources
2
+ sources:
3
+ - type: git
4
+ name: ruby/gem_rbs_collection
5
+ remote: https://github.com/ruby/gem_rbs_collection.git
6
+ revision: main
7
+ repo_dir: gems
8
+
9
+ # You can specify local directories as sources also.
10
+ # - type: local
11
+ # path: path/to/your/local/repository
12
+
13
+ # A directory to install the downloaded RBSs
14
+ path: .gem_rbs_collection
15
+
16
+ # gems:
17
+ # # If you want to avoid installing rbs files for gems, you can specify them here.
18
+ # - name: GEM_NAME
19
+ # ignore: true
@@ -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