Working with headers/footers after AppendDocument


#1

Perhaps I’m hijacking an old thread here, but could you please help me with how to modify the code sample if the first document (BlankDocument.docx) already had content, and you don’t want to alter headers/footers in that part of the document?

I figure I should not use mergedDoc.FirstSection, but instead I need to find the point in mergdedDoc where the appended document (“originaldocument.docx”) starts…


Merge two documents with Even/Odd Header Footers
#2

@roseen,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word documents
  • Aspose.Words generated output document showing the undesired behavior
  • Your expected document showing the correct output. You can create expected document by using MS Word. Please also list the steps that you performed in MS Word to create expected document.
  • Please also create a simplified standalone application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your scenario and provide you more information. Thanks for your cooperation.


#3

This will take some time, the issue is deep in a very large application. While I am working on that, I will try to rephrase my question and make it more clear:

Here is a rough pseudo code of what I am doing:

var d = new Document();
loop over all “blocks” I want to add
for each block:
var newDoc = new Document (using a template file that is selected from a number of types)
newDoc.MailMerge.Execute()
d.AppendDocument(newDoc, ImportFormatMode.KeepDifferentStyles);

Now, the template files have alternating odd/even headers and footers.
If the mailmerge generates a newDoc that is more than one page, this looks fine with page numbers alternating to the left/right.

But here is the problem: Each first page from a new AppendDocument operation always starts to the right!
Which means that if I append a lot of one page documents, every page is a “right” page.

I have tried the following to no avail:

  • Setting d.FirstSection.PageSetup.OddAndEvenPagesHeaderFooter = true on the base document.
  • Executing d.UpdatePageLayout(); after appending
  • Executing newDoc.FirstSection.HeadersFooters.LinkToPrevious(true); on each new document

I will try to make a standalone demo of this, but if you have any ideas already, it would be greatly appreciated!

/Göran


#4

AsposeDemo.zip (1.3 MB)

OK, this gets more complicated… I have created a small sample app, and a simplified template document. When I use the simplified document, the headers are correct (see file “Generated1.docx”).

However, when I use one of the real template files (that uses mail merge, but I don’t think that is the cause), the headers are wrong, always on the right side. (see file “Generated2.docx”).

If you open the template files (they are in the Visual Studio solution, “Alternating.docx” and “Personprogram_punkt_2019-0720.docx”) and try adding text to them so that there are multiple pages, you will see that both behave as expected, putting the header alternatingly on the left and the right side.

Sincerely,
Göran Roseen


#5

@roseen,

We are checking this scenario further and will get back to you soon.


#6

Hello again!
I think the error is on our part, I’m sorry for that!

The designer that makes the Word templates brought to my attention that she uses “use a different header for first page” setting. This makes the first page of every included document always start on the right side.

So, I think it is us that has to fix this. Thanks anyway, you may close this issue!

Ps.
Do you have any idea on how this could be fixed using code, don’t hesitate to answer.
Basically, I have three types of headers: first, left and right. And then I want to be able to have “first” headers on some pages, but still not disturb the overall left/right logic that should run through the document.
If it’s not possible, then we’ll just have to think of another way of doing it…


#7

@roseen,

It is great that you were able to resolve this issue on your end. Generally, you can get any Header/Footer of any Section by using the following Aspose.Words’ code:

HeaderFooter hFirst = doc.Sections[0].HeadersFooters[HeaderFooterType.HeaderFirst];
foreach(Node node in hFirst.GetChildNodes(NodeType.Any, true))
{
    // Todo to manipulate any content inside 'header first'
}
HeaderFooter fPrimary = doc.Sections[1].HeadersFooters[HeaderFooterType.FooterPrimary];
// and so on

Please also take a look at a few more Aspose.Words APIs related to headers and footers.

doc.LastSection.HeadersFooters.LinkToPrevious(true);
doc.LastSection.PageSetup.DifferentFirstPageHeaderFooter = true;
doc.LastSection.PageSetup.OddAndEvenPagesHeaderFooter = true;

Please also consult API Reference Guide.


#8

Thanks for the pointers!