Skip to content

Persist header and footer edits on save (original work by @aashish in #42)#175

Merged
satoryu merged 1 commit into
masterfrom
add-header-footer-write
May 31, 2026
Merged

Persist header and footer edits on save (original work by @aashish in #42)#175
satoryu merged 1 commit into
masterfrom
add-header-footer-write

Conversation

@satoryu
Copy link
Copy Markdown
Member

@satoryu satoryu commented May 31, 2026

Summary

Makes header and footer edits persist on save/stream. Until now Document#headers and #footers (added in #173 / #174) were read-only — editing the returned Nokogiri documents had no effect because update only wrote back document.xml and styles.xml. This extends update to write each header/footer back into the archive.

doc = Docx::Document.open("example.docx")
doc.headers["header1"].at_xpath("//w:t").content = "New header text"
doc.save("example.docx")   # change is now persisted

Approach

Each header/footer is written back keyed by its original file name (the headers/footers Hash key, e.g. "header1"):

headers.each { |name, content| replace_entry "word/#{name}.xml", content.serialize(save_with: 0) }
footers.each { |name, content| replace_entry "word/#{name}.xml", content.serialize(save_with: 0) }

Using the stored file name (rather than reconstructing header#{index+1}.xml) means documents with multiple or non-sequential header/footer files round-trip correctly without risk of overwriting the wrong entry.

Credit

Based on the original read/write implementation by @aashish in #42. They are credited as a co-author on the commit and should be credited in the release notes when this ships.

Tests

  • Adds a round-trip spec: open multi_doc.docx, edit the header and footer text, save, reopen, and assert the changes persisted.
  • Full suite green locally (141 examples, 0 failures).

Closes #42

🤖 Generated with Claude Code

Extends Document#update to write modified headers and footers back into the
archive on save/stream, keyed by their original file name (so multiple and
non-sequential header/footer files round-trip correctly). Builds on the
header/footer reading support (#173, #174).

Based on the original read/write implementation by @aashish in #42.

Co-authored-by: aashish <aashish@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@satoryu satoryu merged commit 977ab05 into master May 31, 2026
5 checks passed
@satoryu satoryu deleted the add-header-footer-write branch May 31, 2026 11:00
pull Bot pushed a commit to NeatNerdPrime/docx that referenced this pull request May 31, 2026
Document#bookmarks previously only scanned the document body, so bookmarks
placed in headers/footers were invisible and couldn't be edited. This extends
the scan to headers and footers (keyed by name, like the body). Combined with
header/footer write-back (ruby-docx#175), the existing Bookmark insertion API now works
for headers and footers and persists on save.

Based on the original header-bookmark idea by @bramj in ruby-docx#22 (extended here to
cover footers as well).

Co-authored-by: bramj <bramj@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant