Docx: Different First Page Header **Question**

Created on 7 May 2018  路  21Comments  路  Source: dolanmiu/docx

Hello,

When creating word documents with Word or Google Docs, there is a "Different First Page" header option. Can this functionality be recreated in this package?

Thanks!

feature request question

All 21 comments

Sorry, this isn't available in this package as of yet

Has any progress been made on this? If not, I can contribute. I desperately need this feature.

no progress has been made

I wasnt aware of "Different First Page" until you mentioned actually

If you can make a PR, that would be awesome

Ok no problem, I'll work on it.

In your opinion what is the best way to add and test new features? Are you creating files, like the demo files, and then compiling the code everytime you want to run it?

Any advice would be appreciated. This is my first time working with TypeScript and WebpackLoader.

.spec.ts files to unit test to see if its creating the correct structure

And demo files to essentially e2e test the whole thing

But yes, what I generally do is run the demo each time I make a change to see if it creates the correct result, as well as write the spec file along the way.

Because you are working with headers section, I would run the header and footer demo to test if it broke existing functionality or not

Happy to report back with findings on Page Numbers and Different First Page Headers. I have both Features working.

Different First Page Header:
1) Requires an additional header file called header2.xml
2) Requires header2.xml to be added in [Content_Types].xml
3) Requires header2 relationship to be added in document.xml.rels
4) Requires header2 reference to be added in body sectionProperties (in document.xml)
5) Requires "" to also be added in body sectionProperties

Content added to header2.xml is the content of the first-page header. Content in header1.xml is 2nd-page onward.

Page Numbers:
The following "Run" is what designates a page number. This increments the numbers automatically.

    <w:r>
        <w:rPr></w:rPr>
        <w:t xml:space="preserve"></w:t>
        <w:fldChar w:fldCharType="begin"></w:fldChar>
        <w:instrText xml:space="preserve">PAGE</w:instrText>
        <w:fldChar w:fldCharType="separate"></w:fldChar>
        <w:fldChar w:fldCharType="end"></w:fldChar>
    </w:r>

I have code working for page numbers and different first-page header. I don't feel comfortable doing a PR though because the code is a mess! I would like to discuss how you would like to organize the code, and how the features should be set.

For example, how would you like page numbers to be set? Currently, I have a function in the Run class that sets them. However, this probably is not the best place since realistically page numbers only go in headers.

Same for the different first-page header option, what makes the most sense to for that? Maybe something similar to the current header... And if the the firstpageheader is not empty, then use it.

 var doc = new docx.Document();
 doc.header.createParagraph("My Text")
 doc.firstpageheader.createParagraph("My First Page Header Text")

Any thoughts or recommendations?

Is this feature exclusive to the "first page" only?

What about second page header, third page header?

If so, maybe it makes more sense making headers into an array like:

doc.Header // for main header
doc.PageHeaders.getForPage(1), doc.PageHeaders.getForPage(2) // etc

If not, then this is fine:

doc.FirstPageHeader

Or something like that

Edit:

From some research, word only supports first page header, and odd/even headers:

https://www.howtogeek.com/103481/how-to-use-multiple-headers-and-footers-in-a-single-document/

image

So do:

doc.FirstPageHeader

and leave doc.Header alone to not break existing users :smile:

We will worry about odd/even headers some point in the future

Okay, sounds good. I'll get to work on that.

For the page numbers, I verified that they are only possible in headers. This means that they should only be available when adding to paragraphs that will be added to a header. How can we do that?

Currently, this is how I am setting them.

//example
var pageoneheader = new docx.Paragraph("First Page Header ").right()
var pageNumber = new docx.TextRun().pageNumber()
pageoneheader.addRun(pageNumber)
doc.FirstPageHeader.addParagraph(pageoneheader)

var normalheader = new docx.Paragraph("Normal Header ").right()
normalheader.addRun(pageNumber)
doc.Header.addParagraph(normalheader)

What does pageNumber() do?

I cannot see it in your code:

https://github.com/formatically/docx/blob/master/src/file/paragraph/run/run.ts
https://github.com/formatically/docx/blob/master/src/file/paragraph/run/text-run.ts

Does it add 1, 2, 3, etc? What about customising it, so it's "Page 1", "Page 2"

Sorry, I have not committed my code yet. I'll finish cleaning somethings and commit.

pageNumber() just adds the number, like 1, 2, 3.

To customize to say "Page 1", you must do

var header = new docx.Paragraph("Page ")
var pageNumber = new docx.TextRun().pageNumber()
header.addRun(pageNumber)
doc.Header.addParagraph(header)

I think this is preferred since many people will want custom text before the page number. Also, they may want to right align, left align, etc. This gives them ability to fully customize.

Nice, it seems good 馃憤

In order for there to be a "different first page" header, the following properties must be in the sectionProperties for the body.

<w:headerReference w:type="first" r:id="rId5"></w:headerReference>
<w:titlePg w:val="1"></w:titlePg>

How do you recommend adding these properties after the sectionProperties has already been initialized?

For example if someone makes a different first page header by doing,
doc.firstPageHeader = ...
then there needs to be a way to add those sectionProperties to the body.

For testing purposes I just hard coded them in, to verify my methods work.

I think to simplify things, hard code it in:

https://github.com/dolanmiu/docx/blob/master/src/file/document/body/section-properties/section-properties.ts

Make sure it does not break existing demos

Yes, it may seem bad, but it will make the code cleaner and nicer to work with. Otherwise we would have to make conditional setters depending on whether the user have made a first page header and what not. Not worth it IMO.

Unfortunately hardcoding it in is not an option b/c those properties should not be present when all the headers are the same. Those properties should only be in the section properties when the user wants a "different first page" header.

Maybe the easiest solution would be to add an option that can be passed in when creating a new document. That way not much would have to change in the code. The sectionProperties would still be initialized the same way.

Maybe adding a field in SectionPropertiesOptions that designates a "different first page header".
new Document(sectionPropertiesOptions?: SectionPropertiesOptions)

Hmm I see what you mean

Yeah that sounds good

Easy to use for the user

are you going to add a add(component: XmlComponent) method in SectionProperties to add that headerReference in?

I'll work something out and commit the code to my fork so you can make sure everything looks okay. Ill be traveling for the next 24 hours so it might not be till Tuesday until I have something.

Sure! No problem :smile:

I will have a look once I get time

I added a differentFirstPageHeader option to sectionProperties. I have the code committed to my fork and demo8 has an example of using it.

Good work

Can you call it demo14?

Thanks

Absolutely, I鈥檒l make the change and do another commit. Thanks!

Was this page helpful?
0 / 5 - 0 ratings