How to Set Different Footer on Last Page of Word Document using .NET

Hi Team,

We are working on a requirement where we need to show an image on the last page footer of a Word document. It should show only on the last page. There should not be any change on other page footers. Could you please help with it? Please find attached the current implementation
Docx, image for the last page footer, and expected sample(manually created).

Package.zip (676.6 KB)

Thanks,
Kushal

We also want to mention that we are generating header and footer in code. It’s not coming from HTML.

@kushalpaliwal847

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

  • Your input Word and HTML document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your 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 issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Thanks @tahir.manzoor for quick response. I have already attached all resources. Please see original/first comment.

I am looking for code snippets/blog link to put different footer( a image) on last page only. Thanks in advance.

Thanks
Kushal

@kushalpaliwal847

We are working over your query and will get back to you soon.

Hi @tahir.manzoor,

Did you get anything on my query?

Thanks
Kushal

@kushalpaliwal847

Following code example shows how to insert the image at the bottom of last page of document. You can use Shape.HorizontalAlignment and Shape.RelativeVerticalPosition properties to set the position of image according to your requirement. For more detail, please read the following article.

Document doc = new Document(MyDir + "Current Implementation Result.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
Shape shape  = builder.InsertImage(MyDir + "email_foot (1).png");
shape.Width = doc.LastSection.PageSetup.PageWidth;
shape.WrapType = WrapType.None;
shape.BehindText = true;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
shape.HorizontalAlignment = HorizontalAlignment.Center;
shape.VerticalAlignment = VerticalAlignment.Bottom;

doc.Save(MyDir + "21.9.docx");

Thanks, @tahir.manzoor for your help. It’s really appreciable. I can add an image footer with the above code but the actual footer text is still visible in the background. Can you please help in removing that also? Article link will help.

Please see copyright text in black color on last page 21.9.docx (211.8 KB)

Thanks,
Kusahl

@kushalpaliwal847

Your Expected Output.docx has only FooterFirst (footer for the first page of section). There is no primary footer in it. You can add footer for first page of section and remove primary footer using following code snippet.

Document doc = new Document(MyDir + "Current Implementation Result.docx");
doc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = true;

if (doc.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary] != null)
    doc.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary].Remove();

To work with headers and footers, please read the following article.

@tahir.manzoor- We don’t want to remove footer copyright text from all pages. We just want to remove it from the last page of the document where we are placing the image. But above code is removing primary footer from all pages. Please share suggestions to remove from only the last page of the document.

Thanks,
Kushal

@kushalpaliwal847

Please note that HeaderFooter is a section-level node and can only be a child of Section. There can only be one HeaderFooter or each HeaderFooterType in a Section. Your document contains only one section. You need to insert section break at second last page of document to insert new section into document. By using this approach, you can achieve your requirement.

Following code example shows how to insert section break at the second last page of document and insert image at the bottom of document. Hope this helps you.

Document doc = new Document(MyDir + "Current Implementation Result.docx");

doc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = true;

DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();

int page = doc.PageCount;
LayoutCollector collector = new LayoutCollector(doc);
Node node = builder.CurrentParagraph;
while (node != null)
{
    node = node.PreviousPreOrder(doc);
    if (node.NodeType == NodeType.Paragraph && collector.GetStartPageIndex(node) == page - 1)
    {
        node = node.PreviousPreOrder(doc); 
        builder.MoveTo(node);
        builder.InsertBreak(BreakType.SectionBreakContinuous);
        break;
    }
}
                                 
HeaderFooter header = new HeaderFooter(doc, HeaderFooterType.FooterPrimary);
header.IsLinkedToPrevious = false;
doc.LastSection.HeadersFooters.Add(header);

builder.MoveToDocumentEnd();
Shape shape = builder.InsertImage(MyDir + "email_foot (1).png");

shape.Width = doc.LastSection.PageSetup.PageWidth;
shape.WrapType = WrapType.None;
shape.BehindText = true;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
shape.HorizontalAlignment = HorizontalAlignment.Center;
shape.VerticalAlignment = VerticalAlignment.Bottom;

doc.Save(MyDir + "21.9.docx");