Free Support Forum - aspose.com

AppendDocument and headers

I am attempting to combine multiple Word documents. Some of them have headers and some don’t, and I’m having problems because the pages without headers get headers added to them which (apart from being wrong) pushes the content of the page down onto the next page.


I tried adding

docSrc.FirstSection.HeadersFooters.LinkToPrevious(False)

but while that stops adding the header to the documents without headers, it adds space as if the header was there, and this pushes the content down.

I found some code on this forum to disable headers for documents without headers:

Dim docDest As Document = New Document
docDest.RemoveAllChildren()

For Each s As String In pSA
Dim docSrc As New Document(s)
docSrc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage
docSrc.FirstSection.PageSetup.RestartPageNumbering = True
Dim containsHeaderFooter As Boolean = False

For Each sec As Section In docSrc
If sec.HeadersFooters.Count > 0 Then
containsHeaderFooter = True
Exit For
End If
Next

If Not containsHeaderFooter Then
For Each sec As Section In docSrc
sec.PageSetup.DifferentFirstPageHeaderFooter = True
Next
End If

docDest.AppendDocument(docSrc, ImportFormatMode.KeepDifferentStyles)
Next
docDest.Save(pS_Dest)
but this only seems to work sometimes - if you merge the attached documents 1 and 4 then this works, but if you merge 3 and 4 it doesn’t and I don’t know why…

Hi John,


Please also create your expected Word document which shows the correct output here for our reference. Please create this document using Microsoft Word application. We will then provide you code to achieve the same using Aspose.Words.

Best regards,

I have attached a corrected version of combining documents 3 and 4 - the first document has a header but the second one does not and they fit on the page instead of overflowing.

Hi John,


Thanks for your inquiry. Have you tried following simple code? Are you using latest version i.e. 15.7.0? This code seems to produce correct output (see attached 15.7.0.docx):

Dim doc3 As New Document(MyDir & “3.docx”)

Dim doc4 As New Document(MyDir & "4.docx")

doc3.AppendDocument(doc4, ImportFormatMode.KeepDifferentStyles)

doc3.Save(MyDir & "15.7.0.docx")


Best regards,

Yes I am using 15.7.


That code works when combining documents 3 and 4 but not when combining 1 and 4.

Hi John,


Thanks for your inquiry. For 1 and 4, please use the following code:

Dim doc1 As New Document(MyDir & “1.docx”)

Dim doc4 As New Document(MyDir & "4.docx")

For Each sec As Section In doc4.Sections

sec.HeadersFooters.LinkToPrevious(False)

sec.PageSetup.HeaderDistance = 0

For Each hf As HeaderFooter In sec.HeadersFooters

hf.FirstParagraph.ParagraphBreakFont.Size = 1

Next

Next

doc1.AppendDocument(doc4, ImportFormatMode.KeepDifferentStyles)

doc1.Save(MyDir & "out.docx")


I hope, this helps.

Best regards,

That makes the header smaller, but it still takes up enough space to push the contents of the document 4 onto the next page

Hi John,

Please create your expected Word document (1.docx and 4.docx) which shows the correct output here for our reference. Please create this document using Microsoft Word application and list steps you used to create it. We will then provide you code to achieve the same using Aspose.Words.

Best regards,
When appending document 4 to 1, the header from document 1 is applied to the pages from document 4 making those pages overflow. When appending document 4 to 3, the header from document 3 is not applied to the pages from document 4 so there is no overflow. I don't know what the difference is.

After appending document 4 to 1 if I click Different First Page in Word (or set sec.PageSetup.DifferentFirstPageHeaderFooter = True in code) then the header from document 1 is no longer applied to the pages from document 4. But doing this when appending document 4 to 3 has the opposite effect - it adds the header from document 3 to the pages from document 4 making those pages overflow.

I need a solution that works for all documents.

Hi John,


Thanks for your inquiry.
John:
When appending document 4 to 1, the header from document 1 is applied to the pages from document 4 making those pages overflow. When appending document 4 to 3, the header from document 3 is not applied to the pages from document 4 so there is no overflow. I don’t know what the difference is.
This is because 1.docx has “primary header” while 3.docx has “first header”. Also, “Different First Page” setting is turned on for 3.docx.
John:
After appending document 4 to 1 if I click Different First Page in Word (or set sec.PageSetup.DifferentFirstPageHeaderFooter = True in code) then the header from document 1 is no longer applied to the pages from document 4. But doing this when appending document 4 to 3 has the opposite effect - it adds the header from document 3 to the pages from document 4 making those pages overflow.
Please note that there are three types of headers/footers e.g. FooterFirst, FooterPrimary and FooterEven. Similarly we have three types of header e.g. HeaderFirst, HeaderPrimary and HeaderEven. Here are a few details of these header/footer types:

Member Name<o:p></o:p>

Description<o:p></o:p>

HeaderEven<o:p></o:p>

Header for even numbered pages.<o:p></o:p>

HeaderPrimary<o:p></o:p>

Primary header, also used for odd numbered pages.<o:p></o:p>

FooterEven<o:p></o:p>

Footer for even numbered pages.<o:p></o:p>

FooterPrimary<o:p></o:p>

Primary footer, also used for odd numbered pages.<o:p></o:p>

HeaderFirst<o:p></o:p>

Header for the first page of the section.<o:p></o:p>

FooterFirst<o:p></o:p>

Footer for the first page of the section.<o:p></o:p>


The scope of Headers/Footers is within a Section. 4.docx does not have any Headers/Footers When you append it to 1.docx or 3.docx, Headers/Footers are inherited from them. The correct output is produced in case when appending to 3.docx because 3.docx has HeaderFirst which is by design displayed only on the first page of document.

Best regards,
What I need is some code that will allow each page of the combined documents to keep whatever headers they originally had - we're likely to be combining documents with headers we don't know about beforehand.

Is the solution to change all HeaderFirst headers to HeaderPrimary?

Hi John,


Thanks for your inquiry. Please refer to the following page:
http://www.aspose.com/docs/display/wordsnet/Controlling+How+Header+and+Footers+Appear

And sometimes to simulate no added vertical space, you’ll need to apply the workaround from here:
http://www.aspose.com/community/forums/permalink/652276/651648/showthread.aspx#651648

Best regards,