How to Handle HTML to PDF Page Breaks Using C#

Managing Page Breaks in HTML to PDF Conversions Using Iron PDF

This tutorial demonstrates how to manage page breaks when converting HTML to PDF using the Iron PDF library in a C#.NET application.

Prerequisites

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

Steps to Implement

  1. 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
    $vbLabelText   $csharpLabel
  2. 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"
    $vbLabelText   $csharpLabel
  3. 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>
    "
    $vbLabelText   $csharpLabel
  4. 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)
    $vbLabelText   $csharpLabel
  5. 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")
    $vbLabelText   $csharpLabel

Advanced CSS for Page Control

  • Prevent Page Breaks Within Elements: Use page-break-inside: avoid; on elements that should not be split across pages, like images or tables.
  • Control Table Headers and Footers: Apply display: table-header-group; and display: table-footer-group; for headers and footers, respectively, to maintain their visibility throughout the document.
  • It's beneficial to experiment with page-break-after: auto; for more precise control over page breaks within your content.

Running the Application

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.

Further Exploration

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

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of all products is growing daily, as he finds new ways to support customers. He enjoys how collaborative life is at Iron Software, with team members from across the company bringing their varied experience to contribute to effective, innovative solutions. When Chipego is away from his desk, he can often be found enjoying a good book or playing football.
< PREVIOUS
How to Convert HTML to PDF with Responsive CSS using C#
NEXT >
How to Render an HTML File to PDF in C#