Header/footer overlapping with page content

Hi,

I’m trying to convert html to pdf document. Once I have created Document object I want to set header and footer, but if header content is higher then page margin then the header will override the page content (footer is cut of by the end of the page). I have been reading some other tickets (e.g. ticket) and understood this is not supported, could you please confirm this? Are there any plans to implement the functionality to move content of the page down in this case to avoid overlapping? On other hand there is this ticket where ticket PDFNEWNET-38635 is created to address, I think this issue, but I’m not sure about this?

Also, I have understood this should work for adding header/footer while creating a new page (ticket), but I didn’t have luck with this either. Here is one of the examples I have tried:

Document document = new Document();
for (int i = 0; i < 3; i++)
{
   Page page = document.Pages.Add();
   HeaderFooter header = new HeaderFooter();
   page.Header = header;
   header.Margin = new MarginInfo(0, 0, 0, 20);
   header.Paragraphs.Add(new TextFragment(
   "Sample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\n" +
   "Sample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\n" +
   "Sample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\n" +
   "Sample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\n" +
   "Sample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\n" +
   "Sample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\n" +
   "Sample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\nSample header Text\n"));

   Table table = new Table();
   table.ColumnWidths = "20% 20% 20% 20% 20%";
   table.BackgroundColor = Color.White;

   for (int k = 0; k < 200; k++)
   {
      var row = table.Rows.Add();
      for (int j = 0; j < 5; j++)
      {
         var cell = row.Cells.Add();
         TextFragment text = new TextFragment("Engine");
         cell.Paragraphs.Add(text);
      }
   }
   page.Paragraphs.Add(table);

   HeaderFooter footer = new HeaderFooter();
   footer.Paragraphs.Add(new TextFragment(
      "Sample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\n" +
      "Sample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\n" +
      "Sample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\n" +
      "Sample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\n" +
      "Sample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\n" +
      "Sample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\nSample footer Text\n"));
   page.Footer = footer;
}

document.Save(@"C:\resultant.pdf");

Here is one not-working example using OnBeforePageGenerate:

public static void Run()
{
   Document doc = new Document();
   Page page = doc.Pages.Add();
   page.PageInfo.Height = PageSize.A4.Height;
   page.PageInfo.Width = PageSize.A4.Width;
   page.PageInfo.Margin = new MarginInfo(60, 60, 60, 60);

   page.OnBeforePageGenerate += Page_OnBeforePageGenerate;

   Table table = new Table();
   table.ColumnWidths = "20% 20% 20% 20% 20%";

   for (int i = 0; i < 200; i++)
   {
      var row = table.Rows.Add();
      for (int j = 0; j < 5; j++)
      {
         var cell = row.Cells.Add();
         TextFragment text = new TextFragment("Engine");
         cell.Paragraphs.Add(text);
      }
   }

   page.Paragraphs.Add(table);

   doc.Save(@"C:\testheaderfooter.pdf");
}
private static void Page_OnBeforePageGenerate(Page page)
{
   HeaderFooter footer = new HeaderFooter();
   footer.Paragraphs.Add(new HtmlFragment("footer last page "));
   footer.Paragraphs.Add(new HtmlFragment("footer last page "));
   footer.Paragraphs.Add(new HtmlFragment("footer last page "));
   footer.Paragraphs.Add(new HtmlFragment("footer last page "));
   footer.Paragraphs.Add(new HtmlFragment("footer last page "));
   footer.Paragraphs.Add(new HtmlFragment("footer last page "));
   footer.Paragraphs.Add(new HtmlFragment("footer last page "));
   footer.Paragraphs.Add(new HtmlFragment("footer last page "));
   footer.Paragraphs.Add(new HtmlFragment("footer last page "));
   footer.Paragraphs.Add(new HtmlFragment("footer last page "));
   page.Footer = footer;

   HeaderFooter header = new HeaderFooter();
   header.Paragraphs.Add(new HtmlFragment("header page first line"));
   header.Paragraphs.Add(new HtmlFragment("header page second line"));
   header.Paragraphs.Add(new HtmlFragment("header page second line"));
   header.Paragraphs.Add(new HtmlFragment("header page second line"));
   header.Paragraphs.Add(new HtmlFragment("header page second line"));
   header.Paragraphs.Add(new HtmlFragment("header page second line"));
   header.Paragraphs.Add(new HtmlFragment("header page second line"));
   header.Paragraphs.Add(new HtmlFragment("header page second line"));
   header.Paragraphs.Add(new HtmlFragment("header page second line"));
   page.Header = header;
}

Is there any way to solve this issue? Maybe embedding header/footer in html document.
I have already tried this, but header element is not processed as header but as page content.

Is there I way to get header height once it’s set so page margins could be adjusted?

The version of the library I’m using is 17.12; visual studio 2015 on win 10.

Sorry on so many questions and big post. I would appreciate any help with this issue. Thank you!

Kind regards,
Drazen

@drazen.pupovac

Thank you for explaining your requirements with all the details.

I would like to share with you that the feature to avoid content overlapping is not supported yet. And the ticket ID PDFNET-38635, formerly PDFNEWNET-38635, was logged for the same feature request. However, I have logged another ticket with ID PDFNET-43941 with your findings for the support of this feature. The issue ID has been linked with this thread so that you will receive notification as soon as the issue is resolved.

Can you share the HTML document with header element for our reference please.

As Aspose.Pdf mimics the behavior of Adobe Acrobat and same feature is not available in that so I am afraid this may not be possible with Aspose.Pdf either.

We are sorry for the inconvenience.

Hi Raza,

Thank you on fast response.

Do you maybe know if we can expect this feature implemented soon? Is there any possible workaround except maybe html2word2pdf?

Here is an example, but it’s only simple html document with header and footer:

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
	border: 1px solid black;
}
th {
	background-color:red;
}
</style>
</head>
<body>
<header>
	<h1>Most important heading here</h1>
	<h3>Less important heading here</h3>
	<p>Some additional information here</p>
	<p>Some additional information here</p>
	<p>Some additional information here</p>
	<p>Some additional information here</p>
	<p>Some additional information here</p>
	<p>Some additional information here</p>
	<p>Some additional information here</p>
	<p>Some additional information here</p>
	<p>Some additional information here</p>
	<p>Some additional information here</p>
	<p>Some additional information here</p>
  </header>
<table>
  <colgroup>
	<col style="width:50px">
	<col style="width:50px">
	<col style="width:50px">
  </colgroup>
  <tr>
	<th>ISBN</th>
	<th>Title</th>
	<th>Price</th>
  </tr>
  <tr>
	<td>3476896</td>
	<td>My first HTML</td>
	<td>$53</td>
  </tr>
  <tr>
	<td>5869207</td>
	<td>My first CSS</td>
	<td>$49</td>
  </tr>
  <tr>
	<td>3476896</td>
	<td>My first HTML</td>
	<td>$53</td>
  </tr>
  <tr>
	<td>5869207</td>
	<td>My first CSS</td>
	<td>$49</td>
  </tr>
  <tr>
	<td>3476896</td>
	<td>My first HTML</td>
	<td>$53</td>
  </tr>
  <tr>
	<td>5869207</td>
	<td>My first CSS</td>
	<td>$49</td>
  </tr>
  <tr>
	<td>3476896</td>
	<td>My first HTML</td>
	<td>$53</td>
  </tr>
  <tr>
	<td>5869207</td>
	<td>My first CSS</td>
	<td>$49</td>
  </tr>
  <tr>
	<td>3476896</td>
	<td>My first HTML</td>
	<td>$53</td>
  </tr>
  <tr>
	<td>5869207</td>
	<td>My first CSS</td>
	<td>$49</td>
  </tr>
  <tr>
	<td>3476896</td>
	<td>My first HTML</td>
	<td>$53</td>
  </tr>
  <tr>
	<td>5869207</td>
	<td>My first CSS</td>
	<td>$49</td>
  </tr>
  <tr>
	<td>3476896</td>
	<td>My first HTML</td>
	<td>$53</td>
  </tr>
  <tr>
	<td>5869207</td>
	<td>My first CSS</td>
	<td>$49</td>
  </tr>
  <tr>
	<td>3476896</td>
	<td>My first HTML</td>
	<td>$53</td>
  </tr>
  <tr>
	<td>5869207</td>
	<td>My first CSS</td>
	<td>$49</td>
  </tr>
  <tr>
	<td>3476896</td>
	<td>My first HTML</td>
	<td>$53</td>
  </tr>
  <tr>
	<td>5869207</td>
	<td>My first CSS</td>
	<td>$49</td>
  </tr>
  <tr>
	<td>3476896</td>
	<td>My first HTML</td>
	<td>$53</td>
  </tr>
  <tr>
	<td>5869207</td>
	<td>My first CSS</td>
	<td>$49</td>
  </tr>
  <tr>
	<td>3476896</td>
	<td>My first HTML</td>
	<td>$53</td>
  </tr>
  <tr>
	<td>5869207</td>
	<td>My first CSS</td>
	<td>$49</td>
  </tr>
  <tr>
	<td>3476896</td>
	<td>My first HTML</td>
	<td>$53</td>
  </tr>
  <tr>
	<td>5869207</td>
	<td>My first CSS</td>
	<td>$49</td>
  </tr>
  <tr>
	<td>3476896</td>
	<td>My first HTML</td>
	<td>$53</td>
  </tr>
  <tr>
	<td>5869207</td>
	<td>My first CSS</td>
	<td>$49</td>
  </tr>
  <tr>
	<td>3476896</td>
	<td>My first HTML</td>
	<td>$53</td>
  </tr>
  <tr>
	<td>5869207</td>
	<td>My first CSS</td>
	<td>$49</td>
  </tr>
  <tr>
	<td>3476896</td>
	<td>My first HTML</td>
	<td>$53</td>
  </tr>
  <tr>
	<td>5869207</td>
	<td>My first CSS</td>
	<td>$49</td>
  </tr>
  <tr>
	<td>3476896</td>
	<td>My first HTML</td>
	<td>$53</td>
  </tr>
  <tr>
	<td>5869207</td>
	<td>My first CSS</td>
	<td>$49</td>
  </tr>
</table>
<footer>
  <p>Footer example</p>
  <p>Footer example</p>
  <p>Footer example</p>
  <p>Footer example</p>
  <p>Footer example</p>
  <p>Footer example</p> 
</footer>
</body>
</html>

Kind regards,
Drazen

@drazen.pupovac

I would like to share with you that implementation of this feature may take some time. However, Aspose offers an advanced HTML manipulation API. Aspose.Html for .NET enables you to perform a wide range of HTML manipulation tasks.

Please try using below code snippet with Aspose.Html for .NET 17.12 in your environment, and you will notice the generated PDF document is rendered fine, exactly the same as you see it with HTML in a browser. I have attached the generated PDF file for your kind reference. Aspose_Html_17.12.pdf

        // Create PdfRendering Options object
        Aspose.Html.Rendering.Pdf.PdfRenderingOptions pdf_options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions();
        
        // Instantiate PdfDevice object while passing PdfRenderingOptions and resultant file path as arguments
        using (Aspose.Html.Rendering.Pdf.PdfDevice pdf_device = new Aspose.Html.Rendering.Pdf.PdfDevice(pdf_options, @"D:\Aspose_Html_17.12.pdf"))
        // Create HtmlRenderer object
        using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
        // Create HtmlDocument instance while passing path of already created HTML file
        using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(@"D:\header.html"))
        {
            // Render the output using HtmlRenderer
            renderer.Render(pdf_device, html_document);
        } 

If the generated PDF still does not satisfy your requirements then please share a PDF document as expected output so that we may proceed further to help you out.