Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Page numbers header footer #133

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

benjaminketron
Copy link

Hi @jdugan. Hope you are well and enjoying the holiday season!

We are working on a basic page header and footer implementation, and hoped to get your thoughts. We haven't yet planned for the more complicated scenarios with headers / footers on even / odd pages, sections, etc since it is outside the scope of our work, but thought this might be a good foundation to build upon.

The footer implementation is a bit interesting since for backward compatibility it can be superceded by page_numbers.

We've also added a rudimentary field text helper that supports PAGE and NUMPAGES, but not yet formatting or any of the other fields.

I am having second thoughts about adding footer_content and header_content instead of adding them directly to the contents array, but it does simplify the rendering logic....

Any time you can spend on this would be much appreciated. 😄

This appears as helper methods for paragraphs and table cells.

Works really well in the headers and footers of subsequent commits,
but not so well in the body of a page. The information needed to
calculate the fields is apparently not ready until the pages are
completely rendered requiring a user to invoke the update fields
command manually after the doc is opened in Word or other Offiiceish
product.

We could eventually add support for the dirty flag on `w:fldChar`,
however, it does cause Word to prompt the user in an alarming fashion
when the document loads, doesn't solve the issue for documents meant
to be perfect when they are opened, and isn't as likely a use case
as headers or footers.

We should consider how best to add other fields and their formats
http://officeopenxml.com/WPfieldInstructions.php.
@benjaminketron benjaminketron force-pushed the page-numbers-header-footer branch 2 times, most recently from 2ba5633 to 5895814 Compare January 9, 2020 23:34
Ben Ketron added 2 commits January 10, 2020 13:08
This helper method provides a footer object to its
block with functionality similar to a table cell
but rendering as a document footer.

It is superceded by `.page_numbers`. Also, it writes
content to a document level header_content object
rendered separately as `word/footer1.xml`.

This allows us to write

```
docx.footer do |footer|
  footer.p 'hello there'
end
```

or

```
docx.footer do |footer|
  footer.table [[a cell]]
end
```
Provides a document level `.header` method passing its
block a header object having essentially the same
functionality as a table cell but rendering as a document
header.

It writes content to a document level header_content object
rendered separately as `word/header.xml`.

This means you can write

```
docx.header do |header|
  header.p 'hello there'
end
```

or

```
docx.header do |header|
  header.table [[one cell]]
end
```
@benjaminketron benjaminketron force-pushed the page-numbers-header-footer branch from 5895814 to f9e44f1 Compare January 10, 2020 20:09
@shubham-yhakur-96
Copy link

shubham-yhakur-96 commented Mar 2, 2022

Hello @benjaminketron,

Thanks for this solution, But I found one issue while implementing this change

Issue: If I open a file(page number: Page 1 of 5) in the WPS office so it is showing me Page PAGE of NUMPAGES (Instead of actual number).

Could you please confirm?

@vitaliel
Copy link

Hi @benjaminketron,

I tried this PR to generate a sample document with header, but MS Word 365 failed to open it. Do you know what the problem might be?

Caracal::Document.save 'example.docx' do |docx|
  docx.header do |header|
    header.p 'hello there'
  end

  docx.font name: 'Arial'

  docx.h1 'Page 1 Header'
  docx.hr
  docx.p
  docx.h2 'Section 1'
  docx.p  'Lorem ipsum dolor....'
  docx.p
end

example.docx

@jamesridgway
Copy link

If anyone is interested/still needs this, we have a fork that we're maintaining that has a working implementation of this:
https://github.com/the-curve-consulting/caracal

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.

4 participants