Skip to content

Commit 977ab05

Browse files
authored
Merge pull request #175 from ruby-docx/add-header-footer-write
Persist header and footer edits on save (original work by @aashish in #42)
2 parents 3d5cd6e + e2dd9de commit 977ab05

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

lib/docx/document.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ def load_rels
245245
def update
246246
replace_entry 'word/document.xml', doc.serialize(save_with: 0)
247247
replace_entry 'word/styles.xml', styles_configuration.serialize(save_with: 0)
248+
headers.each do |name, content|
249+
replace_entry "word/#{name}.xml", content.serialize(save_with: 0)
250+
end
251+
footers.each do |name, content|
252+
replace_entry "word/#{name}.xml", content.serialize(save_with: 0)
253+
end
248254
end
249255

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

spec/docx/document_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,27 @@
8484
end
8585
end
8686

87+
describe 'updating headers and footers' do
88+
before do
89+
@doc = Docx::Document.open(@fixtures_path + '/multi_doc.docx')
90+
@new_doc_path = @fixtures_path + '/multi_doc_saved.docx'
91+
end
92+
93+
after do
94+
File.delete(@new_doc_path) if File.exist?(@new_doc_path)
95+
end
96+
97+
it 'persists changes to headers and footers after save' do
98+
@doc.headers['header1'].at_xpath('//w:t').content = 'Edited header.'
99+
@doc.footers['footer1'].at_xpath('//w:t').content = 'Edited footer.'
100+
@doc.save(@new_doc_path)
101+
102+
reopened = Docx::Document.open(@new_doc_path)
103+
expect(reopened.headers['header1'].text).to eq 'Edited header.'
104+
expect(reopened.footers['footer1'].text).to eq 'Edited footer.'
105+
end
106+
end
107+
87108
describe 'read tables' do
88109
before do
89110
@doc = Docx::Document.open(@fixtures_path + '/tables.docx')

0 commit comments

Comments
 (0)