I've recently inherited some reports that are rendered with SelectPDF and am trying to resolve an issue. In this particular case, a pretty basic MVC page that renders a set of tables with page breaks in between is rendering a random assortment of duplicated data as hidden fields behind the scenes. When you search the document via Ctrl+F or try to drag and select (See image), the data shows up as searchable and when copied, the data shows to be some combination of fields/values from the report. For example in the image below, those yellow fields are data from previous or following pages and can be searched, copied, etc. The file has no hidden fields, no display:none, nothing of that nature. I'm trying to see if this is a SelectPDF setting perhaps? Why is it re-rendering in hidden fields data found elsewhere in the report?
The code is pretty basic boostrap layout, with a containing div and a for loop, which in each process renders a table. After each table is a page break, such as :
<div class="new-page"></div>
Where the class is
.new-page {
page-break-after: always;
Rendering also appears to be pretty straightforward according to docs:
public static string RenderRazorViewToPDFWithHeader(RazorHeaderPartialView headerPartialView, string contentViewName, RazorFooterPartialView? footerPartialView, object model, ControllerContext ControllerContext, ViewDataDictionary ViewData, TempDataDictionary TempData, PdfPageOrientation pageOrientation)
{
string filePath = Path.GetTempPath() + Path.GetRandomFileName();
var header = RenderRazorViewToString(headerPartialView.PartialViewName, model, ControllerContext, ViewData, TempData);
var PDFView = RenderRazorViewToString(contentViewName, model, ControllerContext, ViewData, TempData);
var footer = footerPartialView.HasValue ? RenderRazorViewToString(footerPartialView.Value.PartialViewName, model, ControllerContext, ViewData, TempData) : String.Empty;
var pdf = new HtmlToPdf();
pdf.Options.PdfPageOrientation = pageOrientation;
pdf.Options.DisplayHeader = true;
pdf.Header.DisplayOnFirstPage = true;
pdf.Header.DisplayOnOddPages = true;
pdf.Header.DisplayOnEvenPages = true;
pdf.Header.Height = headerPartialView.Height;
pdf.Header.Add(new PdfHtmlSection(header, null));
if (footerPartialView.HasValue)
{
pdf.Options.DisplayFooter = true;
pdf.Footer.Height = footerPartialView.Value.Height;
pdf.Footer.Add(new PdfHtmlSection(footer, null));
if (footerPartialView.Value.ShowPageNumbers)
{
const int fontSize = 8;
int pageNumX = (pdf.Options.WebPageWidth / 3) + footerPartialView.Value.HorizontalMarginOffset;
int pageNumY = footerPartialView.Value.VerticalMarginOffset;
PdfTextSection pageNumText = new PdfTextSection(pageNumX, pageNumY,
"Page {page_number} of {total_pages}",
new System.Drawing.Font("Arial", fontSize));
pdf.Footer.Add(pageNumText);
}
}
var doc = pdf.ConvertHtmlString(PDFView);
doc.Save(filePath);
doc.Close();
return filePath;
}
Disclosure: I work for SelectPdf.
You need to enable an alternate page break algorithm. To do that, use the following code:
pdf.Options.PageBreaksEnhancedAlgorithm = true;
Details about the property are here: https://selectpdf.com/docs/P_SelectPdf_HtmlToPdfOptions_PageBreaksEnhancedAlgorithm.htm