Merge documents: Continue always on odd pages but header and footer and page number are missing

We produce word documents and want to merge them and finally save it as PDF. There is always a “main” document where we define header and footer and page number. Then we attach other documents to the main document. These other documents (attachments) should always start on odd pages in the new document.

Example 1) This works

  • Main document with 4 pages
  • Second document with 2 pages with is going to be attached to the main document
  • The result should be a merged document with 6 pages (4 pages from main document followed by 2 pages from the second document including header and footer and page number from main document)

Example 2) Empty page is added, but no header and footer and page number

  • Main document with 3 pages
  • Second document with 2 pages with is going to be attached to the main document
  • The result should be a merged document with 6 pages (3 pages from main document followed by a blank page with header and footer and page number from the main document followed by 2 pages from the attached document with header and footer and page number from the main document)

Tried to achieve my goal by following code:

var mainDocument = new Document(@"c:\temp\MainDocument.docx");
var documentToAttach = new Document(@"c:\temp\Attachment.docx");

// ensure header and footer and page number is applied
documentToAttach.FirstSection.HeadersFooters.LinkToPrevious(true);
// ensure we continue on odd pages
documentToAttach.FirstSection.PageSetup.SectionStart = SectionStart.OddPage;

mainDocument.AppendDocument(anhang, ImportFormatMode.KeepDifferentStyles);
mainDocument.Save(@"c:\temp\Aspose_Merged_Document.pdf", SaveFormat.Pdf);

Problem: Empty page (see Example 2) does not have header and footer and pager numer. How can I solve that?

Example2.zip (52.7 KB)

@pulla2908 In this case Aspose.Words mimics MS Word behavior, If you insert section break Even or Odd and it produces an empty page, MS Word also exports this page as empty (without header and footers) to PDF. To work this around, you can calculate number of pages and insert a section break. Please see the following code:

var mainDocument = new Document(@"c:\temp\MainDocument.docx");
var documentToAttach = new Document(@"c:\temp\Attachment.docx");

if (mainDocument.PageCount % 2 > 0)
{
    Section empty = (Section)mainDocument.LastSection.Clone(false);
    empty.PageSetup.SectionStart = SectionStart.NewPage;
    empty.EnsureMinimum();
    mainDocument.AppendChild(empty);
}

// ensure header and footer and page number is applied
documentToAttach.FirstSection.HeadersFooters.LinkToPrevious(true);
// ensure we continue on odd pages
documentToAttach.FirstSection.PageSetup.SectionStart = SectionStart.OddPage;

mainDocument.AppendDocument(documentToAttach, ImportFormatMode.KeepDifferentStyles);
mainDocument.UpdatePageLayout();
mainDocument.Save(@"c:\temp\Aspose_Merged_Document.pdf", SaveFormat.Pdf);

Note, in this case it is required to call Document.UpdatePageLayout() before saving the final document to PDF. Here is the document produced by this code on my side: Aspose_Merged_Document.pdf (34.8 KB)

1 Like

Hi @alexey.noskov

thank you, that works as expected. Discussed this with my customer and I have a few more requirements to this:

  • main document and documents to attach have their own header and footer. They are different (different images, different text, different styling). They should be kept when merging. I can see that merged document gets header and footer from the main document and overrides header and footer from attached documents.The solution for that should be:

    documentToAttach.FirstSection.HeadersFooters.LinkToPrevious(false);
    
  • to make it even a bit more complicated main document and documents to attach have a different header and footer starting on page 2 (or the other away around: on the first page there is more information than on the following pages). Don’t know if ths is important to consider, I just want to mention that.

  • last but not least the most important thing for the customer is to have the page number on this empty page (and nothing more if easily possible).

I am thinking of following options.

Option a)
For the empty page only the numbering is applied (from header and/or footer if present). If there is no page number found then there is just a blank page with blank header and footer.

Option b)
For the empty page entire content from header and footer from the last page of the main document is applied to the empty page (due to the fact that header and footer are different starting from page 2).

Don’t know which option can be implemented at all or is easier to implement. Hope I made it clear. If not, I can provide further samples and upload them.

best regards,
Thomas

@pulla2908 The option b is easier to implement. Please see the following modified code:

var mainDocument = new Document(@"c:\temp\MainDocument.docx");
var documentToAttach = new Document(@"c:\temp\Attachment.docx");

if (mainDocument.PageCount % 2 > 0)
{
    Section empty = (Section)mainDocument.LastSection.Clone(false);

    // Make sure the first page header is not inherited by an empty page.
    empty.PageSetup.DifferentFirstPageHeaderFooter = false;

    empty.PageSetup.SectionStart = SectionStart.NewPage;
    empty.EnsureMinimum();
    mainDocument.AppendChild(empty);
}

// ensure header and footer are not inherited from the main document.
documentToAttach.FirstSection.HeadersFooters.LinkToPrevious(false);
// ensure we continue on odd pages
documentToAttach.FirstSection.PageSetup.SectionStart = SectionStart.OddPage;

mainDocument.AppendDocument(documentToAttach, ImportFormatMode.KeepDifferentStyles);
mainDocument.UpdatePageLayout();
mainDocument.Save(@"c:\temp\Aspose_Merged_Document.pdf", SaveFormat.Pdf);
1 Like

@alexey.noskov option b works as you wrote, perfect! We will discuss this now internally if the customer is happy with option b as well.