Add Page Breaks in HTML to PDF Files with C# .NET

One major difference between PDF documents and HTML is that HTML documents tend to scroll whereas PDFs are multi paged.

Create a Page Break

To create a page-break in HTML you can use this code:

<div style='page-break-after: always;'> </div>

Avoiding page breaks in Images

To avoid a page-break within an image or table you may use the css page-break-inside attribute applied to a wrapping DIV element.

<div style='page-break-inside: avoid'>.

 <img src='no-break-me.png'>


Page Breaks and Tables

As shown above, page breaks within tables can be avoided by using the CSS:

page-break-inside: avoid. This is better applied to a wrapping DIV than to the table itself to ensure the style is applied to a block level html node.

To duplicate table headers and footers across every page of a large HTML table spanning multiple pdf pages you may use an <thead> group within the table:



                <th>C Sharp</th><th>VB</th>



Advanced CSS 3

To give greater control you may wish to use CSS3 in addition to your thead group:

<style type="text/css">

    table { page-break-inside:auto }

    tr    { page-break-inside:avoid; page-break-after:auto }

    thead { display:table-header-group }

    tfoot { display:table-footer-group }


Alternative Options

IronPDF's API can also be used to construct and manipulate multipage documents by merging several documents together using the IronPDF.PdfDocument class editing methods.