Combining Documents - Keeping Headers and Footers Intact


#1

I have been going in circles trying to get this code to work. When I combine documents, I need to have the headers and footers stay intact in the destination document when copied from the source document. The headers and footers are not too fancy, but each document will have an individual primary header and/or footer. When I use this combining code, the footers keep spilling into the next appended document. And if a source document has a blank header or footer, it should stay that way. Please help.... I have several different attempts inside this code. Also, the styles I am using will be consistent, so the issue is just the headers and footers. Also, I will not have a different first page header or footer.

Also, is there a way to add the page break without using the DocumentBuilder? In a previous post, you stated that I should not use the DocumentBuilder for this process, but the page break is necessary.

Private Sub AppendDoc(ByVal dstDoc As Document, ByVal srcDoc As Document)

Dim dstdocBuilder As DocumentBuilder = New DocumentBuilder(dstDoc)

Try

Dim i As Integer = 0

Do While i < srcDoc.Sections.Count

' Add a page break first.

If i = 0 Then

dstdocBuilder.MoveToDocumentEnd()

dstdocBuilder.InsertBreak(BreakType.SectionBreakContinuous)

dstdocBuilder.InsertBreak(BreakType.PageBreak)

End If

If i = 0 Then

'Dim srcsection As Section = srcDoc.Sections(srcDoc.Sections.Count - 1)

'Dim srcPrimaryHeader As HeaderFooter = srcsection.HeadersFooters(HeaderFooterType.HeaderPrimary)

'If Not srcPrimaryHeader Is Nothing Then srcPrimaryHeader.IsLinkedToPrevious = False

'Dim srcPrimaryFooter As HeaderFooter = srcsection.HeadersFooters(HeaderFooterType.FooterPrimary)

'If Not srcPrimaryFooter Is Nothing Then srcPrimaryFooter.IsLinkedToPrevious = False

'Dim srcdocBuilder As DocumentBuilder = New DocumentBuilder(srcDoc)

'srcdocBuilder.MoveToDocumentEnd()

'srcdocBuilder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary)

End If

Dim section As Section = CType(dstDoc.ImportNode(srcDoc.Sections(i), True, ImportFormatMode.KeepSourceFormatting), Section)

dstDoc.Sections.Add(section)

If i = 0 Then

'Dim primaryHeader As HeaderFooter = section.HeadersFooters(HeaderFooterType.HeaderPrimary)

'If Not primaryHeader Is Nothing Then primaryHeader.IsLinkedToPrevious = False

'Dim primaryFooter As HeaderFooter = section.HeadersFooters(HeaderFooterType.FooterPrimary)

'If Not primaryFooter Is Nothing Then primaryFooter.IsLinkedToPrevious = False

Dim dstPrimaryFooter As HeaderFooter = dstDoc.Sections(dstDoc.Sections.Count - 1).HeadersFooters(HeaderFooterType.FooterPrimary)

If Not dstPrimaryFooter Is Nothing Then dstPrimaryFooter.IsLinkedToPrevious = False

End If

section.PageSetup.SectionStart = SectionStart.Continuous

For Each paragraph As Paragraph In section.GetChildNodes(NodeType.Paragraph, True)

paragraph.ParagraphFormat.WidowControl = True

Next paragraph

i += 1

Loop

Catch ex As Exception

MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)

End Try

End Sub


#2

One more comment on this. The following code worked great in VBA. It would set these items in the source document before it got appended to the destination document. wrdDocCurrent is the source document. These steps are what I have been trying to reproduce in Aspose. After LinkToPrevious is set in the source document, when appended to the destination document, it would leave the headers and footers intact in each individual document. I inserted a continuous section break and then a page break in each source document before it was appended to the destination document. Now Apose seems to add an extra section break in the append process, so that might be causing some difficulty. How do you think this can be solved?

Set myRange = wrdDocCurrent.Content
myRange.Collapse wdCollapseEnd
myRange.Sections.Last.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
myRange.Collapse wdCollapseEnd
myRange.Sections.Last.Footers(wdHeaderFooterPrimary).LinkToPrevious = False


#3

Here is a complete example:

Private Sub CombinDocuments()

' combine pre-built documents 1.doc - 12.doc

Dim dstDoc As Document = New Document

For k As Integer = 1 To 10

For i As Integer = 1 To 12

AppendDoc(dstDoc, New Document(Application.StartupPath & "\" & i & ".doc"))

Next

Next

dstDoc.FirstSection.Remove()

dstDoc.Save(Application.StartupPath + "\CombinedDoc.doc")

End Sub

Private Sub AppendDoc(ByVal dstDoc As Document, ByVal srcDoc As Document)

Dim i As Integer = 0

Do While i < srcDoc.Sections.Count

Dim section As Section = CType(dstDoc.ImportNode(srcDoc.Sections(i), True, ImportFormatMode.UseDestinationStyles), Section)

dstDoc.Sections.Add(section)

' Choose one of the following:

' either separate the combined documents with the page break

' or not.

' We should do this only for the first imported section

' Other section breaks should be left as they are in the source document.

If i = 0 Then

section.PageSetup.SectionStart = SectionStart.NewPage

'section.PageSetup.SectionStart = SectionStart.Continuous

End If

Dim primaryHeader As HeaderFooter = section.HeadersFooters(HeaderFooterType.HeaderPrimary)

If Not primaryHeader Is Nothing Then

primaryHeader.IsLinkedToPrevious = False

End If

Dim primaryFooter As HeaderFooter = section.HeadersFooters(HeaderFooterType.HeaderPrimary)

If Not primaryFooter Is Nothing Then

primaryFooter.IsLinkedToPrevious = False

End If

For Each paragraph As Paragraph In section.GetChildNodes(NodeType.Paragraph, True)

paragraph.ParagraphFormat.WidowControl = True

Next paragraph

i += 1

Loop

End Sub