AppendDocument showing second and subsequent page headers

Hi, I’ve got an issue when merging documents.

The documents are all just duplicates from the same template but are merged together for printing.

The 1st document doesn’t appear to show the header, but then the subsequent documents do which causes them to be in different positions on the page.

I’m attaching the template, 3 documents and output document and associated code. If you’re able to let me know what the issue is that would be great.

A work around appears to be a modification of the header size stops this from happening, though it still looks funny as the 1st page isn’t showing the header.

To see this I have markers toggled on.

Thanks

Nolan

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;

using Aspose.Words;
using HSL.Utils;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            new Aspose.Words.License().SetLicense("Aspose.Words.DEV.lic");

            List inputFiles = new List();
            string outputFile = "";

            inputFiles.Add(@"D:\Temp\MergeDocumentTest\InputDoc1.docx");
            inputFiles.Add(@"D:\Temp\MergeDocumentTest\InputDoc2.docx");
            inputFiles.Add(@"D:\Temp\MergeDocumentTest\InputDoc3.docx");
            outputFile = @"D:\Temp\MergeDocumentTest\Output.docx";

            // Load source documents
            Document doc = DocumentLoader.Execute(inputFiles[0]);

            for (int i = 1; i < inputFiles.Count; i++)
            {
                Document appendDoc = DocumentLoader.Execute(inputFiles[i]);

                appendDoc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
                appendDoc.FirstSection.HeadersFooters.LinkToPrevious(false);

                doc.AppendDocument(appendDoc, ImportFormatMode.KeepSourceFormatting);
            }

            if (File.Exists(outputFile))
                File.Delete(outputFile);
            doc.Save(outputFile);

            // PrinterSettings printerSettings = new PrinterSettings();
            // printerSettings.PrinterName = @"\Pancho\Xerox DocuCentre-III C2200 B&W";
            // printerSettings.Duplex = Duplex.Vertical;
            // doc.Print(printerSettings);
        }
    }

    internal class DocumentLoader
    {
        public static Document Execute(string fileName)
        {
            switch (Path.GetExtension(fileName))
            {
                case ".docx":
                case ".doc":
                case ".dotx":
                    return new Document(fileName);
                default:
                    return new Document();
            }
        }
    }
}

Hi Nolan,

Thanks for your inquiry.

I managed to reproduce the issue on my side. Upon closer inspection it appears this is not a bug since manually doing the same steps in MS Word results in an identical output.

Since your documents don’t have any headers or footers most likely what is happening is that when the headers from the appended documents aren’t linked to the previous section, MS Word treats this as there being no header but still leaves a space as if there was one there anyway.

To avoid this behaviour you can link the headers and footers to the first section instead:

doc.FirstSection.HeadersFooters.LinkToPrevious(true);

If you have any further queries, please feel free to ask.

Thanks,

Hi Adam,

Thanks for the prompt reply.

I did try your suggestion, but this didn’t appear to make any difference to the output document.

Also, how do you mean you manually appended the documents? Do you mean cut and paste one document into the 1st one, I’m not quite sure what test you did for that.

Thanks

Nolan

Hi Nolan,

Thanks for this additional information.

I have created a short video for you which demonstrates this behaviour in MS Word as well. The content is copied into a new section in MS Word (by default the same section formatting is used for newly inserted sections). Then you can inspect by turning Link to Previous on and off that the behaviour is the same in MS Word.

You’re correct that the issue still occurs even when LinkToPrevious is set to true in Aspose.Words. I have logged this as an issue. We will inform you when a fix is available.

In the mean time if you remove the call to LinkToPrevious completely this issue does not occur. This is what I meant to suggest previously :slight_smile:

Thanks,