Skip to content

Commit 7d36927

Browse files
committed
adding support for readings headers and footers
1 parent 054ae80 commit 7d36927

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

lib/docx/document.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module Docx
2222
class Document
2323
include Docx::SimpleInspect
2424

25-
attr_reader :xml, :doc, :zip, :styles
25+
attr_reader :xml, :doc, :zip, :styles, :headers, :footers
2626

2727
def initialize(path_or_io, options = {})
2828
@replace = {}
@@ -40,6 +40,8 @@ def initialize(path_or_io, options = {})
4040

4141
@document_xml = document.get_input_stream.read
4242
@doc = Nokogiri::XML(@document_xml)
43+
@headers = fetch_headers
44+
@footers = fetch_footers
4345
load_styles
4446
yield(self) if block_given?
4547
ensure
@@ -75,6 +77,20 @@ def bookmarks
7577
bkmrks_hsh
7678
end
7779

80+
def fetch_headers
81+
@zip.glob('word/header*.xml').map do |entry|
82+
header_xml = entry.get_input_stream.read
83+
Nokogiri::XML(header_xml)
84+
end
85+
end
86+
87+
def fetch_footers
88+
@zip.glob('word/footer*.xml').map do |entry|
89+
footer_xml = entry.get_input_stream.read
90+
Nokogiri::XML(footer_xml)
91+
end
92+
end
93+
7894
def to_xml
7995
Nokogiri::XML(@document_xml)
8096
end
@@ -210,6 +226,12 @@ def load_rels
210226
def update
211227
replace_entry 'word/document.xml', doc.serialize(save_with: 0)
212228
replace_entry 'word/styles.xml', styles_configuration.serialize(save_with: 0)
229+
headers.each_with_index do |header, index|
230+
replace_entry "word/header#{index + 1}.xml", header.serialize(:save_with => 0) if header
231+
end
232+
footers.each_with_index do |footer, index|
233+
replace_entry "word/footer#{index + 1}.xml", footer.serialize(:save_with => 0) if footer
234+
end
213235
end
214236

215237
# generate Elements::Containers::Paragraph from paragraph XML node

0 commit comments

Comments
 (0)