Footer on only Last Page

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,

Hello Support

I think you misunderstood my issue. As i define in screenshot there is repeated border in continue table format.borders appears till data after that it shows blank lines.i require these border repeat till footer.like this:-

Thanks for your help.

but we still facing some issue data wise.

for your understanding we uploaded all running code on below link (as i am unable to upload 45MB file). which we are using to generate documents.
http://testonline200.exportcentral.com.au/AsposeSampleApp.zip

  1. If there is 40 total line items in repeated table , document generate with 2 pages and both pages have footer.but our requirement is first page footer show continue and second page should have footer. (output WithCountinued.pdf).

You can also generate same with my code by clicking on highlighted buttons.

  1. If there is 54 total line items in repeated table ,its work fine for me. first page have continued text in footer and second page have footer. (output WithCountinued.pdf)

you can also check the code for highlighted button:

Please help me out for this, why the same code generating differ differ results.

Hi

Can you please urgently reply to this issue. We have explained in detail to you this issue over last 4 weeks but to date we have not received a working option from you.

Hi,

Thanks for your inquiry.

Please note that there are max three types of headers/footers e.g. FooterFirst, FooterPrimary and FooterEven in a Section. Similarly, we have three types of header e.g. HeaderFirst, HeaderPrimary and HeaderEven. Here are a few details of these header/footer types:

Member Name Description
HeaderEven Header for even numbered pages.
HeaderPrimary Primary header, also used for odd numbered pages.
FooterEven Footer for even numbered pages.
FooterPrimary Primary footer, also used for odd numbered pages.
HeaderFirst Header for the first page of the section.
FooterFirst Footer for the first page of the section.

A Document may have multiple Sections. You need to wrap content of every footer of every Section in Document inside an IF field like this:

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

See my this post where sample code wraps just content of FooterPrimary of FirstSection inside IF field. You need to repeat this for every footer in your document.

Now, coming to the borders problem, it looks like your application can insert any number of rows dynamically. If you insert a new table row, you’ll notice vertical length of those borders increase. And if you remove any row(s), you’ll notice vertical length of those borders decrease. Because, those are table borders and not page borders.

Your application also generates empty table rows at the end that result in extra pages(s). The only way you can remove those extra pages is to delete those empty rows. And when you delete empty rows using the code mentioned in my this post, some of the empty rows from the end of previous page also got removed. Hence the problem of decrease in vertical length of those borders occurs.

You may be able to overcome this problem by implementing the following workflow:

Delete empty row(s) from the end of Document only if their Page is blank.

Identify start and end page nodes of a page (see LayoutCollector & LayoutEnumerator classes)

Convert every node between start and end nodes to string and check if it is empty or not.

If page content is empty, then delete row.

Hope, this helps.

Best regards,

Hi

Can you please elaborate on the below statement:

You may be able to overcome this problem by implementing the following workflow:

Delete empty row(s) from the end of Document only if their Page is blank.

Identify start and end page nodes of a page (see LayoutCollector & LayoutEnumerator classes)

Convert every node between start and end nodes to string and check if it is empty or not.

If page content is empty, then delete row.

Appreciate if you could provide some sample code to achieve the above. Please explain how the classes- (see LayoutCollector & LayoutEnumerator ) can be used by some example

Would appreciate if you could urgently repply.

Hi

Can you please provide a reply on the below. we have raised this issue for over 5 weeks now. Todate we have not received any satisfactory answer which works:

Hi

Can you please elaborate on the below statement:

You may be able to overcome this problem by implementing the following workflow:

Delete empty row(s) from the end of Document only if their Page is blank.

Identify start and end page nodes of a page (see LayoutCollector & LayoutEnumerator classes)

Convert every node between start and end nodes to string and check if it is empty or not.

If page content is empty, then delete row.

Appreciate if you could provide some sample code to achieve the above. Please explain how the classes- (see LayoutCollector & LayoutEnumerator ) can be used by some example

Hi,

Thanks for your inquiry. I believe, the only outstanding problem is removal of extra last page (while preserving the borders). Can you please provide a Word document where the following code does not work?

Document doc = new Document(MyDir + @"in.docx");
if (string.IsNullOrEmpty(doc.FirstSection.Body.LastParagraph.ToString(SaveFormat.Text).Trim()))
    doc.FirstSection.Body.LastParagraph.Remove();

int pageCount = doc.PageCount;
Table table = doc.FirstSection.Body.Tables[0];
foreach (Row row in table)
{
    if (string.IsNullOrEmpty(row.ToString(SaveFormat.Text).Trim()))
    {
        LayoutCollector collector = new LayoutCollector(doc);
        if (collector.GetEndPageIndex(doc.FirstSection.Body.LastParagraph) == pageCount)
        {
            row.RowFormat.ClearFormatting();
            foreach (Cell cell in row)
            {
                cell.FirstParagraph.ParagraphBreakFont.Size = 1;
            }
        }
        else
        {
            break;
        }
    }
}
doc.Save(MyDir + @"17.1.0.docx");
doc.Save(MyDir + @"17.1.0.pdf");

Best regards,

Hello Support

We have 2 types of Document :

  1. Templates With Page Borders

In this case the Codes provided by You works perfectly fine and removes the extra blank page .

  1. Templates With Table Borders

In this case the Extra blank page is removed but the Vertical lines of the table only shows for the rows in which the data is there .

We tried to overcome this issue by adding Dummy Blank rows to that the vertical lines is filled till the end of the TABLE BODY but this also failed.

Attached is the Template for this Document with Table Borders and Vertical lines in Table Also attached is the Output.

Below is the Code which we are using :

Code For vertical Line

collector = new LayoutCollector(doc);
int pagenumber = collector.GetStartPageIndex(table.LastRow.FirstCell.LastParagraph);
int nextpagenumber = pagenumber + 1;
Row lastrow = (Row)table.LastRow.Clone(true);
foreach (Cell cell in lastrow.Cells)
{
    cell.RemoveAllChildren();
    cell.EnsureMinimum();
}
while (pagenumber != nextpagenumber)
{
    table.Rows.Add(lastrow.Clone(true));
    collector = new LayoutCollector(doc);
    pagenumber = collector.GetStartPageIndex(table.LastRow.FirstCell.LastParagraph);
}
table.LastRow.Remove();

Code For Remove Extra Page

if (string.IsNullOrEmpty(doc.FirstSection.Body.LastParagraph.ToString(SaveFormat.Text).Trim()))
    doc.FirstSection.Body.LastParagraph.Remove();

int pageCount = doc.PageCount;
Table table1 = doc.FirstSection.Body.Tables[0];
foreach (Row row in table1)
{
    if (string.IsNullOrEmpty(row.ToString(SaveFormat.Text).Trim()))
    {
        LayoutCollector collector1 = new LayoutCollector(doc);
        string FQ = row.ToString(SaveFormat.Text).Trim();
        if (doc.FirstSection.Body.LastParagraph != null)
        {
            if (collector1.GetEndPageIndex(doc.FirstSection.Body.LastParagraph) == pageCount)
            {
                row.RowFormat.ClearFormatting();
                foreach (Cell cell in row)
                {
                    cell.FirstParagraph.ParagraphBreakFont.Size = 1;
                }
            }
        }
        else
        {
            break;
        }
    }
}

Please let us know what is the issue in this .

Regards

Hi

This issue is very serious now, as our client is very upset and we require to resolve it ASAP

Appreciate your quick response.

Manish

Hi,

Thanks for your inquiry. Please use the following code to fill page with dummy Rows until the vertical Table borders are displayed for whole page.

Document doc = new Document(MyDir + @"Commercial+Invoice_Grain.docx");
int pageCount = doc.PageCount;
Table table = doc.FirstSection.Body.Tables[0];
LayoutCollector collector = new LayoutCollector(doc);
while (collector.GetEndPageIndex(doc.FirstSection.Body.LastParagraph) < (pageCount + 1))
{
    collector = new LayoutCollector(doc);
    Row row = (Row)table.LastRow.Clone(true);
    foreach (Cell cell in row)
    {
        cell.RemoveAllChildren();
    }
    table.Rows.Add(row);
}
table.LastRow.Remove();
doc.UpdatePageLayout();
doc.Save(MyDir + @"17.1.0.pdf");

Hope, this helps.

Best regards,

Thanks for this Code. This Helped us a lot . Initially it looks like the issue is resolved to some extent.

However we are checking this with various scenarios , data and templates. I will confirm this in next 2 days

https://forum.aspose.com/t/footer-on-only-last-page/26372/24

I am trying to find your code that wraps the footer in a if statement? Any help would be greatly appreciated.

@ffemt30 You can use a regular MS Word IF field to achieve this. For example:
{ IF "{ PAGE }" = "{ NUMPAGES }" "true part" "false part" }

Looking for the code to wrap the text in the footer with that if statement using Aspose word .net.

@ffemt30 You can use code like the following to show footer only on the last page programmatically:

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();

// Move to primary footer.
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
// Construct a conditional field
Field fieldIf = builder.InsertField("IF ");
builder.MoveTo(fieldIf.Separator);
builder.InsertField(FieldType.FieldNumPages, false);
builder.Write(" = ");
builder.InsertField(FieldType.FieldPage, false);
builder.Write(" \"This Footer will be shown only on the last page\" \"\"");

doc.Save(@"C:\Temp\out.docx");