We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Check for Page Break from HTML Document

I am trying to check when a page breaks from an HTML document. I am current using the following line of code in my HTML documents to page break…

<div style="page-break-after: always"></div>

Below is my complete code for Apose in C#

  // Initialize original document
            var document = new Document();

            // Add page
            var page = document.Pages.Add();

            // Set paper attributes
            page.SetPageSize(1024, 768);
            page.PageInfo.Margin.Left = 24;
            page.PageInfo.Margin.Right = 24;
            page.PageInfo.Margin.Top = 80;
            page.PageInfo.Margin.Bottom = 32;

            // Add content to page
            var htmlFragment = new HtmlFragment(html);
            page.Paragraphs.Add(htmlFragment);

            // Save original document
            var exportFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            var outputPath = Path.Combine(exportFolder+"/_Reports", "Original.pdf");
            document.Save(outputPath);

            // Load original document, add headers and footers
            var pdfDocument = new Document(outputPath);

            // Add page numbers
            var pageNumberStamp = new PageNumberStamp();
            pageNumberStamp.Background = false;
            pageNumberStamp.Format = "Page # of " + pdfDocument.Pages.Count;
            pageNumberStamp.BottomMargin = 16;
            pageNumberStamp.HorizontalAlignment = HorizontalAlignment.Center;
            pageNumberStamp.StartingNumber = 1;
            pageNumberStamp.TextState.Font = FontRepository.FindFont("Arial");
            pageNumberStamp.TextState.FontStyle = FontStyles.Bold;
            pageNumberStamp.TextState.FontSize = 9;
            var color = Color.FromRgb(System.Drawing.Color.FromArgb(132, 136, 141));
            pageNumberStamp.TextState.ForegroundColor = color;


            // Add to every page
            foreach (Page originalPage in pdfDocument.Pages)
            {
                if (originalPage.Number == 1 ||  ) 
                {
                    // Header
                    var headerFirst = new HeaderFooter();
                    var headerHtmlFragment = new HtmlFragment(htmlHeader);
                    headerFirst.Margin.Left = 0;
                    headerFirst.Margin.Right = 0;
                    headerFirst.Paragraphs.Add(headerHtmlFragment);
                    originalPage.Header = headerFirst;

                    // Footer
                    var footerFirst = new HeaderFooter();
                    var footerHtmlFragment = new HtmlFragment(htmlFooter);
                    footerFirst.Margin.Left = 24;
                    footerFirst.Margin.Right = 24;
                    originalPage.AddStamp(pageNumberStamp);
                    footerFirst.Paragraphs.Add(footerHtmlFragment);
                    originalPage.Footer = footerFirst;
                }
                else
                {
                    // Header
                    var headerRunning = new HeaderFooter();
                    var headerRunningHtmlFragment = new HtmlFragment(htmlHeaderRunning);
                    headerRunning.Paragraphs.Add(headerRunningHtmlFragment);
                    headerRunning.Margin.Left = 24;
                    headerRunning.Margin.Right = 24;
                    headerRunning.Margin.Top = 32;
                    originalPage.Header = headerRunning;

                    // Footer
                    var footerRunning = new HeaderFooter();
                    var footerRunningHtmlFragment = new HtmlFragment(htmlFooterRunning);
                    footerRunning.Paragraphs.Add(footerRunningHtmlFragment);
                    footerRunning.Margin.Left = 24;
                    footerRunning.Margin.Right = 24;
                    originalPage.AddStamp(pageNumberStamp);
                    originalPage.Footer = footerRunning;
                }
            }
            // Save updated document
            var finalOutputPath = Path.Combine(exportFolder+"/_Reports", "Updated.pdf");
            pdfDocument.Save(finalOutputPath);

Is there a way for Aspose.PDF to know when a page has been broken intentionally by html? What I really want to do is put a header on the page but only on the condition that the page was broken by HTML.

@TravisWoodward

Thanks for contacting support.

We regret that there is no such way or method which can determine the occurrence of “page-break-after” style in HTML. As HTML to PDF conversion is performed through HTML to PDF conversion Engine which is internally implemented, we need to investigate the feasibility of this feature. Currently, API breaks the page where it finds mentioned CSS property but API does this silently.

In order to further investigate the feasibility of this feature, we may need a sample HTML file and expected PDF document from you side. We will definitely test the scenario in our environment and address it accordingly.