You can insert page breaks before and after HTML elements in the generated PDF document by setting the PdfDocumentOptionsPageBreakBeforeHtmlElementsSelectors and PdfDocumentOptionsPageBreakAfterHtmlElementsSelectors API properties. An object of PdfDocumentOptions type is exposed by the HtmlToPdfConverterPdfDocumentOptions property. If a HTML element is selected by PageBreakBeforeHtmlElementsSelectors selector a page break will be forced right before the position in PDF page where the element would be normally rendered. If a HTML element is selected by PageBreakAfterHtmlElementsSelectors selector a page break will be forced right after the position in PDF page where the element would be normally rendered.
protected void convertToPdfButton_Click(object sender, EventArgs e) { // Create a HTML to PDF converter object with default settings HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; // Set the CSS selectors of the HTML elements before which to insert page breaks htmlToPdfConverter.PdfDocumentOptions.PageBreakBeforeHtmlElementsSelectors = new string[] { htmlElementsBeforeSelectorTextBox.Text }; // Set the CSS selectors of the HTML elements after which to insert page breaks htmlToPdfConverter.PdfDocumentOptions.PageBreakAfterHtmlElementsSelectors = new string[] { htmlElementsAfterSelectorTextBox.Text }; byte[] outPdfBuffer = null; if (convertHtmlRadioButton.Checked) { string htmlWithForm = htmlStringTextBox.Text; string baseUrl = baseUrlTextBox.Text; // Convert the HTML string to a PDF document in a memory buffer outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlWithForm, baseUrl); } else { string url = urlTextBox.Text; // Convert the HTML page to a PDF document in a memory buffer outPdfBuffer = htmlToPdfConverter.ConvertUrl(url); } // Send the PDF as response to browser // Set response content type Response.AddHeader("Content-Type", "application/pdf"); // Instruct the browser to open the PDF file as an attachment or inline Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Insert_Page_Breaks_Before_After_HTML_Elements_Using_API.pdf; size={0}", outPdfBuffer.Length.ToString())); // Write the PDF document buffer to HTTP response Response.BinaryWrite(outPdfBuffer); // End the HTTP response and stop the current page processing Response.End(); }
<!DOCTYPE html> <html> <head> <title>Insert Page Breaks Before and After HTML Elements Using API</title> </head> <body style="width: 1010px; font-family: 'Times New Roman'; font-size: 20px; margin: 5px"> <div style="width: 100%; height: 500px; background-color: aliceblue; border: 2px solid gray; text-align: center"> <div style="width: 100%; height: 200px"></div> A block <b>without any page break</b> style<br /> <br /> [ Follows a block with page break before and page break after forced from API using <i>PageBreakBeforeHtmlElementsSelectors</i> and <i>PageBreakAfterHtmlElementsSelectors</i> properties ] </div> <div id="page_break_before_and_after_div" style="width: 100%; height: 500px; background-color: gainsboro; border: 2px solid gray; text-align: center"> <div style="width: 100%; height: 200px"></div> A block with page break before and page break after forced from API using <i>PageBreakBeforeHtmlElementsSelectors</i> and <i>PageBreakAfterHtmlElementsSelectors</i> properties<br /> <br /> <b>This block will be always rendered alone in a PDF page</b><br /> <br /> [ Follows a block with page break after forced from API using <i>PageBreakAfterHtmlElementsSelectors</i> property ] </div> <div id="page_break_after_div" style="width: 100%; height: 500px; background-color: beige; border: 2px solid gray; text-align: center"> <div style="width: 100%; height: 200px"></div> A block with page break after forced from API using <i>PageBreakAfterHtmlElementsSelectors</i> property<br /> <br /> <b>Nothing will be rendered after this block in PDF page</b> <br /> <br /> [ Follows a block <i>without any page break</i> style ] </div> <div style="width: 100%; height: 500px; background-color: aliceblue; border: 2px solid gray; text-align: center"> <div style="width: 100%; height: 200px"></div> A block <b>without any page break</b> style<br /> <br /> [ Follows a block with page break before forced from API using <i>PageBreakBeforeHtmlElementsSelectors</i> property ] </div> <div id="page_break_before_div" style="width: 100%; height: 500px; background-color: lightgray; border: 2px solid gray; text-align: center"> <div style="width: 100%; height: 200px"></div> A block with page break before forced from API using <i>PageBreakBeforeHtmlElementsSelectors</i> property<br /> <br /> <b>This block will always be rendered at the top of a PDF page</b> </div> </body> </html>