Footer on only Last Page

Hello Support

We have a requirement that we need to have the Header on All pages but FOOTER only on Last page.

Check attached sample. requirement .

Let us know how can this be done and how to create the template for this .

Regards

Atul

Hi Atul,

Thanks for your inquiry. We have PAGE field and NUMPAGES field. PAGE field represents number of current page while NUMPAGES represents the number of pages in document. You can make use of IF field in footer to detect last page e.g.

{ IF "{ PAGE }" = "{ NUMPAGES }" "true part" "false part" }

You can show/hide content of footer based on above condition. I hope, this helps.

Best regards,

I try this but its not working for me.

Attach BKL_Invoice.docx is input file.
Attach Commercial Invoice_SH_00026.pdf is output file

**on if condition its give “Error! Unknown op code for conditional.”.

Our main requirement is hide whole footer from first page, footer is only display on last page.**

Hi Manish,

Please try using the following code:

Document doc = new Document(MyDir + @"BKL_Invoice.docx");
doc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = true;
doc.FirstSection.HeadersFooters[HeaderFooterType.FooterFirst].Remove();
doc.Save(MyDir + @"16.12.0.docx");
doc.Save(MyDir + @"16.12.0.pdf");

Hope, this helps.

Best regards,

Its working fine if my document have 2 pages.
on single page no footer is coming. attach document : singlepage.pdf
on 6 pages no footer on 1st page but other all pages came with footer. Attach document: Commercial Invoice_SH_00027(1).pdf

source file:

our requirement is when single page then footer should visible on single page.
if multiple pages then footer should com eon last page.

Hi Manish,

Please try using the following code:

Document doc = new Document(MyDir + @"BKL_Invoice_NZ.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
HeaderFooter hf = doc.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary];
int childCount = hf.ChildNodes.Count;
Paragraph para = new Paragraph(doc);
hf.InsertBefore(para, hf.FirstChild);
builder.MoveTo(para);
// { IF "{PAGE}" = "{NUMPAGES}" "Content Visible on Last Page Only" "" }
Field field = builder.InsertField("IF \"", null);
builder.MoveTo(field.Start.NextSibling.NextSibling);
builder.InsertField("PAGE", null);
builder.Write("\" = \"");
builder.InsertField("NUMPAGES", null);
builder.Write("\" \"");
BookmarkStart bmStart = builder.StartBookmark("bm");
builder.Writeln();
builder.EndBookmark("bm");
builder.Write("\" \"\"");
Paragraph bmPara = bmStart.ParentNode as Paragraph;
for (int i = 0; i < childCount; i++)
{
    hf.InsertAfter(hf.LastChild, bmPara);
}
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.PageBreak);
builder.InsertBreak(BreakType.PageBreak);
doc.Save(MyDir + @"16.12.0.pdf");

Hope, this helps.

Best regards,

Thanks for your reply but code is not helpful to me. Here is my requirement:-

  1. A word template with fixed header footer in single page.middle content area is repeat area, showing repeat lines.
  2. if the lines are overflow first page content area , it automatically create second page and first page footer will display ‘Continued’ text and word template footer will go down under the next page and this process will follow till last page of document.
  3. for your reference i am adding my word template

please help me on this.

Hi Manish,

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

  • Aspose.Words generated output DOCX file showing the undesired behavior
  • Please attach your expected document here for our reference. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document using Microsoft Word.
  • Please create a standalone console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start further investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,

As per your requirement attach “Copy of Blackmores Documents and Fields.xlsx” is our requirement. for this we create a word document “BKL_Invoice_NZ.DOCX”.

Code also attach as (split rar files) go to pdfcontroller.cs/GenerateLastPageFooterPDF method.

if anything else require by your team to better understand my issue.please let me know.

Hi Manish,

Thanks for your inquiry. Please try using the following code (see attached PDF):

Document doc = new Document(MyDir + @"BKL_Invoice_NZ.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
HeaderFooter hf = doc.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary];
int childCount = hf.ChildNodes.Count;
Paragraph para = new Paragraph(doc);
hf.InsertBefore(para, hf.FirstChild);
builder.MoveTo(para);
// { IF "{PAGE}" = "{NUMPAGES}" "Content Visible on Last Page Only" "Continued..." }
Field field = builder.InsertField("IF \"", null);
builder.MoveTo(field.Start.NextSibling.NextSibling);
builder.InsertField("PAGE", null);
builder.Write("\" = \"");
builder.InsertField("NUMPAGES", null);
builder.Write("\" \"");
BookmarkStart bmStart = builder.StartBookmark("bm");
builder.Writeln();
builder.EndBookmark("bm");
builder.Write("\" \"Continued...\"");
Paragraph bmPara = bmStart.ParentNode as Paragraph;
for (int i = 0; i < childCount; i++)
{
    hf.InsertAfter(hf.LastChild, bmPara);
}
// Generate a couple of pages to simulate your scenario
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.PageBreak);
builder.InsertBreak(BreakType.PageBreak);
doc.Save(MyDir + @"16.12.0.pdf");

Best regards,

Thanks for help

but i am facing some issue.now bookmark is working fine , but there is some issue like
if my page content is less than 2 pages it still create 2 blank pages due to twice use of

builder.InsertBreak(BreakType.PageBreak);
builder.InsertBreak(BreakType.PageBreak);

when i remove 1 line from above code it creates 1 blank page

Input file: BKL_Invoice.docx

Output file: Commercial Invoice_SH_00026.docx

Please help me on this i think i am very close to finish my issue by your help.

Thanks and appreciate your help.

if you want something from my end please let me know.

Hi Manish,

Thanks for your inquiry. You’re not required to use builder.InsertBreak(BreakType.PageBreak); in your real world scenario. It was given to you to simulate the correctness of output.

However, you may try removing last paragraph or child nodes of that paragraph to fix this issue:

Document doc = new Document(MyDir + @"Commercial+Invoice_SH_00026.docx");
//doc.LastSection.Body.LastParagraph.RemoveAllChildren();
//doc.LastSection.Body.LastParagraph.Remove();
doc.Save(MyDir + @"16.12.0.docx");

Best regards,

i did changes as per your suggestion but it behave very strange.

Case : if my merge content coming in single page
Docx Format: Commercial Invoice_SH_00021.docx (when open in word its not showing any footer)
PDF Format: Commercial Invoice_SH_00021.pdf (when open as pdf it shows footer in first page which is perfect but create extra page)

Hi Manish,

You can try removing a few empty rows from the end of document to generate a one page PDF:

Document doc = new Document(MyDir + @"Commercial+Invoice_SH_00021.docx");
for (int i = 0; i < 5; i++)
    if (string.IsNullOrEmpty(doc.LastSection.Body.Tables[0].LastRow.ToString(SaveFormat.Text).Trim()))
        doc.LastSection.Body.Tables[0].LastRow.Remove();

doc.Save(MyDir + @"16.12.0.pdf");

Best regards,

Hello

Thanks for providing help.but its not working for me.
may be i am using wrong code.please find below code and verify it.

DocumentBuilder builder = new DocumentBuilder(doc);
HeaderFooter hf = doc.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary];
int childCount = hf.ChildNodes.Count;
Paragraph para = new Paragraph(doc);
hf.InsertBefore(para, hf.FirstChild);
builder.MoveTo(para);
// { IF "{PAGE}" = "{NUMPAGES}" "Content Visible on Last Page Only" "Continued…" }
Field field = builder.InsertField("IF \"", null);
builder.MoveTo(field.Start.NextSibling.NextSibling);
builder.InsertField("PAGE", null);
builder.Write("\" = \"");
builder.InsertField("NUMPAGES", null);
builder.Write("\" \"");
BookmarkStart bmStart = builder.StartBookmark("bm");
builder.Writeln();
builder.EndBookmark("bm");
builder.Write("\" \"Continued…\"");
Paragraph bmPara = bmStart.ParentNode as Paragraph;
for (int i = 0; i < childCount; i++)
{
    hf.InsertAfter(hf.LastChild, bmPara);
}
// Generate a couple of pages to simulate your scenario

builder.MoveToDocumentEnd();
// doc.LastSection.Body.LastParagraph.RemoveAllChildren();
// doc.LastSection.Body.LastParagraph.Remove();

for (int i = 0; i < 5; i++)
{
    if (string.IsNullOrEmpty(doc.LastSection.Body.Tables[0].LastRow.ToString(SaveFormat.Text).Trim()))
        doc.LastSection.Body.Tables[0].LastRow.Remove();
}

Hi Manish,

Please see attached input and output Word documents here with this post.

Document doc = new Document(MyDir + @"BKL_Invoice_NZ.DOCX");
DocumentBuilder builder = new DocumentBuilder(doc);
HeaderFooter hf = doc.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary];
int childCount = hf.ChildNodes.Count;
Paragraph para = new Paragraph(doc);
hf.InsertBefore(para, hf.FirstChild);
builder.MoveTo(para);
// { IF "{PAGE}" = "{NUMPAGES}" "Content Visible on Last Page Only" "Continued..." }
Field field = builder.InsertField("IF \"", null);
builder.MoveTo(field.Start.NextSibling.NextSibling);
builder.InsertField("PAGE", null);
builder.Write("\" = \"");
builder.InsertField("NUMPAGES", null);
builder.Write("\" \"");
BookmarkStart bmStart = builder.StartBookmark("bm");
builder.Writeln();
builder.EndBookmark("bm");
builder.Write("\" \"Continued...\"");
Paragraph bmPara = bmStart.ParentNode as Paragraph;
for (int i = 0; i < childCount; i++)
{
    hf.InsertAfter(hf.LastChild, bmPara);
}
doc.Save(MyDir + @"17.1.0.docx");

Now open 17.1.0.docx with MS Word. Press Ctrl+End key to go to the end of document. Insert > PageBreak. Repeat it a few times to generate 4-5 pages. Now double click inside the footer of last page. Now, click Close Header and Footer button inside Headers & Footers Tools ribbon. You will observe the IF blocks are calculating correctly.

Best regards,

Thanks for your reply.

As now footer work as i needed. but there is new issue it creates an extra page and also formatting issue.

i am sending you some example which i generate from your provided code.

Thanks

Hi,

Thanks for your inquiry. Please create comparison screenshots highlighting (encircle) the problematic areas in your documents and attach them here for our reference. We will investigate the issue(s) further on our end and provide you more information.

Best regards,

Hi,

The extra pages are because of blank table rows at the end. You can remove all such empty rows by using the following code:

Document doc = new Document(MyDir + @"5_page.docx");
while (string.IsNullOrEmpty(doc.LastSection.Body.Tables[0].LastRow.ToString(SaveFormat.Text).Trim()))
    doc.LastSection.Body.Tables[0].LastRow.Remove();
doc.Save(MyDir + @"17.1.0.pdf");

Best regards,

Hi,

Borders are removed because there are no Rows till the end of Page. I think, you can try specifying Page Borders as follows:

Document doc = new Document(MyDir + @"BKL_Invoice.docx");
PageSetup ps = doc.FirstSection.PageSetup;
ps.Borders.Left.LineWidth = 1;
ps.Borders.Right.LineWidth = 1;
ps.BorderDistanceFrom = PageBorderDistanceFrom.PageEdge;
ps.Borders.DistanceFromText = 30;
doc.Save(MyDir + @"17.1.0.docx");

Hope, this helps.

Best regards,