|
5 | 5 | [](https://coveralls.io/github/ruby-docx/docx?branch=master) |
6 | 6 | [](https://gitter.im/ruby-docx/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) |
7 | 7 |
|
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. |
9 | 9 |
|
10 | 10 | ## Usage |
11 | 11 |
|
12 | 12 | ### Prerequisites |
13 | 13 |
|
14 | | -- Ruby 2.6 or later |
| 14 | +- Ruby 2.7 or later |
15 | 15 |
|
16 | 16 | ### Install |
17 | 17 |
|
@@ -63,6 +63,26 @@ doc = Docx::Document.open(buffer) |
63 | 63 | # Everything about reading is the same as shown above |
64 | 64 | ``` |
65 | 65 |
|
| 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 | + |
66 | 86 | ### Rendering html |
67 | 87 | ``` ruby |
68 | 88 | require 'docx' |
@@ -116,7 +136,11 @@ doc = Docx::Document.open('example.docx') |
116 | 136 | doc.bookmarks['example_bookmark'].insert_text_after("Hello world.") |
117 | 137 |
|
118 | 138 | # 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.") |
120 | 144 |
|
121 | 145 | # Remove paragraphs |
122 | 146 | doc.paragraphs.each do |p| |
|
0 commit comments