Duplicated content inside header and footer after joining files

hi,
I got duplicated content inside header and footer after joining 3 files.
the last file i got duplicate text at the header and the footer.
i have the last words versionexample.zip (30.5 KB)

i am adding sample of code and the files

 Dim fileUrl As String = Request("fileUrl")       : if fileUrl = "" then errText = "Error: fileUrl is empty"
      Dim fileUrlArr As Array 
      fileUrlArr = split(fileUrl,",")
      Dim PathFile As String
      Dim srcFilePath As String
      
     Dim addHeader As String = "I Am Header"
     Dim addFooter As String = "I Am Footer"
      
      srcFilePath =fileUrlArr(0)
      Dim srcDoc As New Aspose.Words.Document(srcFilePath)
      Dim dstDoc  As New Aspose.Words.Document()
      Dim doc_output As New Aspose.Words.Document()
    '================================================================================================================
      ' Try
          ' for x = 2 to ubound(fileUrlArr)
          ' srcDoc = New Aspose.Words.Document(srcFilePath)
          ' PathFile = fileUrlArr(x)
          ' dstDoc = New Aspose.Words.Document(PathFile)
          ' margeFiles(dstDoc, srcDoc , srcFilePath)
          ' next
          ' ====-----------------------------------------------
          ' ====--- Here i join 3 files that i got path
          ' ====-----------------------------------------------
          for x = 2 to ubound(fileUrlArr)
          ' srcDoc = New Aspose.Words.Document(srcFilePath)
          PathFile = fileUrlArr(x)
          dstDoc = New Aspose.Words.Document(PathFile)
          ' margeFiles(dstDoc, srcDoc , srcFilePath)
          
          Dim builder As DocumentBuilder = New DocumentBuilder(srcDoc)
          builder.PageSetup.ClearFormatting()
          srcDoc.AppendDocument(dstDoc, ImportFormatMode.KeepSourceFormatting)
            builder.PageSetup.ClearFormatting()
          ' builder.MoveToDocumentEnd()
          ' builder.MoveToSection(0)
          next
   
          srcDoc.Save(srcFilePath, Aspose.Words.SaveFormat.Docx)
          
          ' ====--------------------------------------------------------------
          ' ====--- After i saved them. i add a header and footer to the file
          ' ====--------------------------------------------------------------
          
          Dim doc As Document = New Document(srcFilePath)
          Dim builder2 As DocumentBuilder = New DocumentBuilder(doc)
          
          builder2.PageSetup.DifferentFirstPageHeaderFooter = false
          builder2.PageSetup.OddAndEvenPagesHeaderFooter = false
          
          For Each section As Section In doc 
          Dim footer As HeaderFooter
          
          if addHeader <> "" Then
            '  clean header
            builder2.MoveToHeaderFooter(HeaderFooterType.HeaderFirst)
            builder2.InsertHtml(addHeader)
            ' builder2.MoveToHeaderFooter(HeaderFooterType.HeaderEven)
            ' builder2.InsertHtml(addHeader)
            builder2.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary)
            builder2.InsertHtml(addHeader)
            
            
          End if
          if addFooter <> "" Then
            builder2.MoveToHeaderFooter(HeaderFooterType.FooterFirst)
            builder2.InsertHtml(addFooter)
            ' builder2.MoveToHeaderFooter(HeaderFooterType.FooterEven)
            ' builder2.InsertHtml(addFooter)
            builder2.MoveToHeaderFooter(HeaderFooterType.FooterPrimary)
            builder2.InsertHtml(addFooter)
            
          End if
        builder2.MoveToDocumentEnd()
        Next
        doc.Save(srcFilePath)
          
        '   Next
          Response.Write(srcFilePath)
          response.end

@eranlipi,

Please note that Headers Footers belong to Sections and each Section in Word document can have its own Headers or Footers. So, please first move cursor to inside Section and then move to its Header or Footer.

...
...
Dim doc As Document = New Document(srcFilePath)
Dim builder2 As DocumentBuilder = New DocumentBuilder(doc)

builder2.PageSetup.DifferentFirstPageHeaderFooter = False
builder2.PageSetup.OddAndEvenPagesHeaderFooter = False

For Each section As Section In doc
    builder2.MoveTo(section.Body.FirstParagraph)
    If addHeader <> "" Then
        builder2.MoveToHeaderFooter(HeaderFooterType.HeaderFirst)
        builder2.InsertHtml(addHeader)

        builder2.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary)
        builder2.InsertHtml(addHeader)
    End If
    If addFooter <> "" Then
        builder2.MoveToHeaderFooter(HeaderFooterType.FooterFirst)
        builder2.InsertHtml(addFooter)

        builder2.MoveToHeaderFooter(HeaderFooterType.FooterPrimary)
        builder2.InsertHtml(addFooter)
    End If
    'builder2.MoveToDocumentEnd()
Next

doc.Save("C:\Temp\example\final.docx")
1 Like

thank you :slight_smile: