Page numbers are not getting displayed in the pdf when page-break-after css is used

We are using aspose.word to generate pdf from html string in our project. We have used the ‘page-break-after: always’ css property and because of this page number and footer are not getting displayed on few pages of pdf. If I don’t use this page break css property, then numbers get displayed as expected. Please help us to solve this issue asap since it’s impacting our important feature.

@snpatil,

Can you upload your html source so I can try to replicate the issue?

What version of aspose.words are you using?

doc.zip (93.6 KB)
Attached are html and pdf file. In the pdf, page numbers get displayed from page 6.

Here is C# code added for page numbers:

var documentBuilder = new DocumentBuilder();
var pageSetup = documentBuilder.PageSetup;
pageSetup.PaperSize = PaperSize.A4;
pageSetup.Orientation = Orientation.Portrait;
pageSetup.PageNumberStyle = NumberStyle.DecimalFullWidth;
var pageMargin = ConvertUtil.InchToPoint(0.5);
pageSetup.TopMargin = pageSetup.BottomMargin = pageMargin;
pageSetup.LeftMargin = pageSetup.RightMargin = pageMargin;
documentBuilder.InsertHtml(html);
documentBuilder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
documentBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
documentBuilder.InsertField("PAGE", "");
MemoryStream stream = new MemoryStream();
documentBuilder.Document.Save(stream, SaveFormat.Pdf);

@snpatil,

I am using Aspose.Words 23.2

I tried the following code and works.

private void Logic()
{
    Document doc = new Document($@"{PartialPath}_input.html");

    DocumentBuilder builder = new DocumentBuilder(doc);            

    var pageSetup = builder.PageSetup;
    pageSetup.PaperSize = PaperSize.A4;
    pageSetup.Orientation = Orientation.Portrait;
    pageSetup.PageNumberStyle = NumberStyle.DecimalFullWidth;
    var pageMargin = ConvertUtil.InchToPoint(0.5);
    pageSetup.TopMargin = pageSetup.BottomMargin = pageMargin;
    pageSetup.LeftMargin = pageSetup.RightMargin = pageMargin;
    builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
    builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
    builder.InsertField(FieldType.FieldPage, false);

    doc.Save($"{PartialPath}_output.pdf", SaveFormat.Pdf);
}

The ouput:
AddingPageNumberToDocFromHtml_output.pdf (82.1 KB)

@snpatil The problem on your side occurs because the following style used in your source HTML produces section break:

.container {
    position: relative;
    page-break-after: always;
}

And PAGE field is inserted only into the last section, which starts on the 6th page. You can move the DocumentBuilder's cursor to the beginning of the document and insert PAGE field into the first section’s footer (just what @carlos.molina code does), in this case footer will be inherited by the subsequent sections in the document. Or, alternatively, you can loop through all sections and insert footer in each section.

Thank you @alexey.noskov and @carlos.molina.
Moving cursor to the start of the document solved the issue.

1 Like