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

Different First Page Header **Question** #72

Closed
tbell511 opened this issue May 7, 2018 · 21 comments
Closed

Different First Page Header **Question** #72

tbell511 opened this issue May 7, 2018 · 21 comments

Comments

@tbell511
Copy link
Contributor

tbell511 commented May 7, 2018

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!

@dolanmiu
Copy link
Owner

dolanmiu commented May 8, 2018

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

@tbell511
Copy link
Contributor Author

tbell511 commented May 8, 2018

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

@dolanmiu
Copy link
Owner

dolanmiu commented May 8, 2018

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

@tbell511
Copy link
Contributor Author

tbell511 commented May 8, 2018

Ok no problem, I'll work on it.

@tbell511
Copy link
Contributor Author

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.

@dolanmiu
Copy link
Owner

dolanmiu commented May 10, 2018

.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

@tbell511
Copy link
Contributor Author

tbell511 commented May 11, 2018

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 "<w:titlePg w:val="1"></w:titlePg>" 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?

@dolanmiu
Copy link
Owner

dolanmiu commented May 11, 2018

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 😄

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

@tbell511
Copy link
Contributor Author

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)

@dolanmiu
Copy link
Owner

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"

@tbell511
Copy link
Contributor Author

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.

@dolanmiu
Copy link
Owner

Nice, it seems good 👍

@tbell511
Copy link
Contributor Author

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.

@dolanmiu
Copy link
Owner

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.

@tbell511
Copy link
Contributor Author

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)

@dolanmiu
Copy link
Owner

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?

@tbell511
Copy link
Contributor Author

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.

@dolanmiu
Copy link
Owner

Sure! No problem 😄

I will have a look once I get time

@tbell511
Copy link
Contributor Author

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

@dolanmiu
Copy link
Owner

Good work

Can you call it demo14?

Thanks

@tbell511
Copy link
Contributor Author

Absolutely, I’ll make the change and do another commit. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants