Skip to content

Commit a1915aa

Browse files
authored
Merge pull request #177 from ruby-docx/update-readme-headers-footers
Update README for headers/footers and fix stale examples
2 parents e2f8745 + 2cfb420 commit a1915aa

1 file changed

Lines changed: 27 additions & 3 deletions

File tree

README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
[![Coverage Status](https://coveralls.io/repos/github/ruby-docx/docx/badge.svg?branch=master)](https://coveralls.io/github/ruby-docx/docx?branch=master)
66
[![Gitter](https://badges.gitter.im/ruby-docx/community.svg)](https://gitter.im/ruby-docx/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
77

8-
A ruby library/gem for interacting with `.docx` files. currently capabilities include reading paragraphs/bookmarks, inserting text at bookmarks, reading tables/rows/columns/cells and saving the document.
8+
A ruby library/gem for interacting with `.docx` files. currently capabilities include reading paragraphs/bookmarks, inserting text at bookmarks, reading and writing headers/footers, reading tables/rows/columns/cells and saving the document.
99

1010
## Usage
1111

1212
### Prerequisites
1313

14-
- Ruby 2.6 or later
14+
- Ruby 2.7 or later
1515

1616
### Install
1717

@@ -63,6 +63,26 @@ doc = Docx::Document.open(buffer)
6363
# Everything about reading is the same as shown above
6464
```
6565

66+
### Reading headers and footers
67+
68+
``` ruby
69+
require 'docx'
70+
71+
doc = Docx::Document.open('example.docx')
72+
73+
# Headers and footers are returned as hashes keyed by their file name
74+
# (e.g. "header1", "footer1"), with Nokogiri documents as values.
75+
doc.headers.each do |name, header|
76+
puts name
77+
puts header.text
78+
end
79+
80+
doc.footers.each do |name, footer|
81+
puts name
82+
puts footer.text
83+
end
84+
```
85+
6686
### Rendering html
6787
``` ruby
6888
require 'docx'
@@ -116,7 +136,11 @@ doc = Docx::Document.open('example.docx')
116136
doc.bookmarks['example_bookmark'].insert_text_after("Hello world.")
117137

118138
# Insert multiple lines of text at our bookmark
119-
doc.bookmarks['example_bookmark_2'].insert_multiple_lines_after(['Hello', 'World', 'foo'])
139+
doc.bookmarks['example_bookmark_2'].insert_multiple_lines(['Hello', 'World', 'foo'])
140+
141+
# Bookmarks placed in headers and footers are included too, and edits to them
142+
# are saved along with the document.
143+
doc.bookmarks['header_bookmark'].insert_text_after("Hello from the header.")
120144

121145
# Remove paragraphs
122146
doc.paragraphs.each do |p|

0 commit comments

Comments
 (0)