IronPdf how to allow body start X-margintop when a new page created due to element overflow?

18 Views Asked by At

so. Im trying to generate a pdf file, with a permanent header and footer ( though the footer is dynamic, means xxxx page 1 , xxxx page 2 and so on ).

I have the following code:

  static string defaultPageCss =
       // $"body {{ direction: rtl; margin-top: 50px;}} \r\n" +
        $"body {{ direction: rtl;}} \r\n" +
        $"div {{ font-size: 100%; }} \r\n" +
        $"#specialDiv {{ font-size: 100%; color: red; }} \r\n" +
        $"table {{ border-collapse: collapse; }} \r\n" +
        $"table, th, td {{ border: 0px solid; }} \r\n" +
        $"p {{ white-space : break-spaces; }} \r\n";

 public IronPdfUtil() : this(defaultPageCss)
 {

 }

 public IronPdfUtil(string pageCss = "")
 {
     // <head></head>
     XElement head = new XElement("head");
     if (pageCss != "")
     {
         // <head> <style>...</style> </head>
         head.Add(new XElement("style", pageCss));
     }

     // <html> <head></head> <body></body> </html>
     htmlPage = new XElement("html",
     head,
     new XElement("body", "")
     );
 }

public void AddPageElement(string name, string content, string id = "", string css = "")
{
     XElement element = new XElement(name, content);

     if (id != "")
     {
         element.SetAttributeValue("id", id);
     }

     if (css != "")
     {
        element.SetAttributeValue("style", css);
     }

     insertElementAtTheEnd(element);
     
}

  public void InsertElementAtTheEnd(XElement element)
  {
      XElement? searchResult = htmlPage.Descendants()
          .FirstOrDefault(el => el.Name == "body");
      if (searchResult != null)
      {
          // add element as the last child of the body element
          searchResult.Add(element);
      }
  }

here the method, and invokation:

  private void BuildTitle(IronPdfUtil pdfUtil, Font font)
  {
      string hesberimTitle = MaagarTviotApi.Properties.Resources.hesberim_title;

      // Creates a paragraph element for the title
      XElement titleP = new XElement("p", hesberimTitle);

      // Inserts the title div into the PDF document
      pdfUtil.InsertElementAtTheEnd(titleP);
  }

and here lets imagine a main method that will loop 20times that method to create element:
BuildTitle(pdfUtil, fontUnderLine);

Now, lets say I have a method using "AddPageElementAtTheEnd" and I invoke it 20times. Eventually, it will cause my page to have too many elements to keep up at the same page, and will create a new page.

Now, why did I mention the header? Because, I have a problem that these added elements dont really recognize the header( starting from the 2nd page ). So, if page a new page is automatically created. I want the next element added to be added below it the header.. Which I cant seem to make it work.

0

There are 0 best solutions below