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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
// Use EVO PDF Namespace
using EvoPdfClient;
namespace EvoHtmlToPdfDemo.Controllers.HTML_to_PDF.Page_Breaks
{
public class Insert_Page_Breaks_Before_After_Using_APIController : Controller
{
[HttpPost]
public ActionResult ConvertHtmlToPdf(IFormCollection collection)
{
// Get the server options
string serverIP = collection["textBoxServerIP"];
uint serverPort = uint.Parse(collection["textBoxServerPort"]);
string servicePassword = collection["textBoxServicePassword"];
bool useServicePassword = servicePassword.Length > 0;
bool useTcpService = collection["ServerType"] == "radioButtonUseTcpService";
string webServiceUrl = collection["textBoxWebServiceUrl"];
// Create the HTML to PDF converter object
HtmlToPdfConverter htmlToPdfConverter = null;
if (useTcpService)
htmlToPdfConverter = new HtmlToPdfConverter(serverIP, serverPort);
else
htmlToPdfConverter = new HtmlToPdfConverter(true, webServiceUrl);
// Set optional service password
if (useServicePassword)
htmlToPdfConverter.ServicePassword = servicePassword;
// 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[] { collection["htmlElementsBeforeSelectorTextBox"] };
// Set the CSS selectors of the HTML elements after which to insert page breaks
htmlToPdfConverter.PdfDocumentOptions.PageBreakAfterHtmlElementsSelectors = new string[] { collection["htmlElementsAfterSelectorTextBox"] };
byte[] outPdfBuffer = null;
if (collection["HtmlPageSource"] == "convertHtmlRadioButton")
{
string htmlWithForm = collection["htmlStringTextBox"];
string baseUrl = collection["baseUrlTextBox"];
// Convert the HTML string to a PDF document in a memory buffer
outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlWithForm, baseUrl);
}
else
{
string url = collection["urlTextBox"];
// Convert the HTML page to a PDF document in a memory buffer
outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);
}
// Send the PDF file to browser
FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf");
fileResult.FileDownloadName = "Insert_Page_Breaks_Before_After_HTML_Elements_Using_API.pdf";
return fileResult;
}
}
}<!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>