Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
This tutorial demonstrates how to manage page breaks when converting HTML to PDF using the Iron PDF library in a C#.NET application.
Ensure that the Iron PDF NuGet package is installed in your project. You can add it from the package manager console using:
Install-Package IronPdf
Import the Iron PDF Namespace
Begin by importing the Iron PDF namespace at the top of your Program.cs
file.
using IronPdf;
using IronPdf;
Imports IronPdf
Set the License Key
Add your Iron PDF license key if you have one, to unlock the full potential of the library:
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY_HERE";
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY_HERE";
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY_HERE"
Create HTML Content
Construct your HTML content. You can include various elements such as tables and images. Use the CSS page-break-after
property to enforce page breaks after specific elements.
string htmlContent = @"
<html>
<head>
<style>
.page-break { page-break-after: always; }
.avoid-break-inside { page-break-inside: avoid; }
</style>
</head>
<body>
<div class='avoid-break-inside'>
<h1>My Document Title</h1>
<table>
<thead class='avoid-break-inside'>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</tbody>
</table>
</div>
<div class='page-break'></div>
<p>Additional content here...</p>
</body>
</html>
";
string htmlContent = @"
<html>
<head>
<style>
.page-break { page-break-after: always; }
.avoid-break-inside { page-break-inside: avoid; }
</style>
</head>
<body>
<div class='avoid-break-inside'>
<h1>My Document Title</h1>
<table>
<thead class='avoid-break-inside'>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</tbody>
</table>
</div>
<div class='page-break'></div>
<p>Additional content here...</p>
</body>
</html>
";
Dim htmlContent As String = "
<html>
<head>
<style>
.page-break { page-break-after: always; }
.avoid-break-inside { page-break-inside: avoid; }
</style>
</head>
<body>
<div class='avoid-break-inside'>
<h1>My Document Title</h1>
<table>
<thead class='avoid-break-inside'>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</tbody>
</table>
</div>
<div class='page-break'></div>
<p>Additional content here...</p>
</body>
</html>
"
Instantiate the Renderer and Generate PDF
Use the ChromePdfRenderer
to convert the HTML content into a PDF document.
var renderer = new ChromePdfRenderer();
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
var renderer = new ChromePdfRenderer();
var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
Dim renderer = New ChromePdfRenderer()
Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
Save the Generated PDF
Specify the path where you want to save the PDF file.
pdfDocument.SaveAs("MyDocument.pdf");
pdfDocument.SaveAs("MyDocument.pdf");
pdfDocument.SaveAs("MyDocument.pdf")
page-break-inside: avoid;
on elements that should not be split across pages, like images or tables.display: table-header-group;
and display: table-footer-group;
for headers and footers, respectively, to maintain their visibility throughout the document.page-break-after: auto;
for more precise control over page breaks within your content.Running your application will show how these CSS settings impact the PDF output. You can explore more advanced features of Iron PDF for comprehensive document generation.
For more information on managing page breaks, refer to the Iron PDF documentation.
Sign up for a trial on the Iron Software website and download the package to experience its full capabilities.
Further Reading: Add or Avoid Page Breaks in HTML PDFs