title: How to handle the case in which an iText\iTextSharp table is split into two pages?
body: <p>I have the following problem using iTextSharp.</p>
  <p>I am putting some tables into my document. The problem is that if a table content not fit on a page and go into another page I have that it overwrite the second page header, so I have the following situation:</p>
  <p><img src="https://i.stack.imgur.com/bxEIN.png" alt="enter image description here" /></p>
  <p>As you can see I am inserting a table at the end of a page and this is split into two pages and the second page header is overwrite by the table content.</p>
  <p>I want to avoid this situation but I don't know how have I to do.</p>
  <p>I am thinking that maybe I can do one of the following things:</p>
  <ol>
  <li><p>Maybe I can check if an element completely enter into a page. If not create a new page and put it into this new page (but this can be a problem if a single table need more then one page, in the case if I have a very big table)</p>
  </li>
  <li><p>I allow that a table is split across 2 pages but in this case I left some margin spacing in the upper of the second page so the header is correctly shown.</p>
  </li>
  </ol>
  <p>Or what can I do to solve this situation?</p>
  <p>Tnx</p>
  <p>EDIT:</p>
  <p>I have add the header in the following way:</p>
  <ol>
  <li>I have implement create a class named <strong>PdfHeaderFooter</strong> that extends the <strong>PdfPageEventHelper</strong> class and that implements its methods.</li>
  </ol>
  <p>At this time its class contains the following methods:</p>
  <pre><code>    // write on start of each page
      public override void OnStartPage(PdfWriter writer, Document document)
      {
          base.OnStartPage(writer, document);
          PdfPTable tabHead = new PdfPTable(3);
          tabHead.SetWidths(new int[] { 165, 205, 125 });
          
          //tabHead.TotalWidth = 460F;
          tabHead.TotalWidth = document.Right - document.Left;        // TotalWidth = 495
          tabHead.WidthPercentage = 98;
  
  
          PdfPCell cell1 = new PdfPCell(iTextSharp.text.Image.GetInstance(folderImages + &quot;logoEarlyWarning.png&quot;), true) { Border = PdfPCell.BOTTOM_BORDER };
          tabHead.AddCell(cell1);
          //tabHead.AddCell(new PdfPCell(new Phrase(&quot;CELL 1:&quot;)) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15, });
          
          tabHead.AddCell(new PdfPCell(new Phrase(&quot;&quot;)) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15 });
  
          if(_sourceId == &quot;NVD&quot;)
          {
              iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(folderImages + &quot;nvdLogo.png&quot;);
              logo.ScalePercent(48f);
              //PdfPCell cell3 = new PdfPCell(iTextSharp.text.Image.GetInstance(folderImages + &quot;nvdLogo.png&quot;), true) { Border = PdfPCell.BOTTOM_BORDER, PaddingBottom = 25 };
              PdfPCell cell3 = new PdfPCell(logo) { Border = PdfPCell.BOTTOM_BORDER, PaddingLeft = 50 };
              tabHead.AddCell(cell3);
          }
          else if(_sourceId == &quot;DeepSight&quot;)
          {
              PdfPCell cell3 = new PdfPCell(iTextSharp.text.Image.GetInstance(folderImages + &quot;DSLogo.jpg&quot;), true) { Border = PdfPCell.BOTTOM_BORDER };
              tabHead.AddCell(cell3);
          }
          //tabHead.AddCell(new PdfPCell(new Phrase(&quot;CELL 3:&quot;)) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15 });
        
          
          tabHead.WriteSelectedRows(0, -1, document.Left, document.Top, writer.DirectContent);
      }
  
      // write on end of each page
      public override void OnEndPage(PdfWriter writer, Document document)
      {
          base.OnEndPage(writer, document);
  
          int pageN = writer.PageNumber;      // numero della pagina corrente OK
          String text = &quot;Page &quot; + pageN + &quot; of &quot;;
          float len = bf.GetWidthPoint(text, 8);
  
          Rectangle pageSize = document.PageSize;
          
          cb.SetRGBColorFill(100, 100, 100);
  
          cb.BeginText();
          cb.SetFontAndSize(bf, 8);
          cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30));
          cb.ShowText(text);
          cb.EndText();
  
          cb.AddTemplate(template, pageSize.GetLeft(40) + len, pageSize.GetBottom(30));
  
          cb.BeginText();
          cb.SetFontAndSize(bf, 8);
          cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,
              &quot;Printed On &quot; + PrintTime,
              pageSize.GetRight(40),
              pageSize.GetBottom(30), 0);
          cb.EndText();
      }
  </code></pre>
  <p>So ad you can see, the <strong>OnStartPage()</strong> method add the header at the beginning of each pages and the <strong>OnEndPage()</strong> add the footer at the end of each pages.</p>
  <p>So from what I understand from your response I have to do the followings steps:</p>
  <ol>
  <li>Move the header insertion from <strong>OnStartPage()</strong> to <strong>OnEndPage()</strong></li>
  <li>Use an absolute position for place it in the upper part of the pages.</li>
  <li>In the document creation use the header height to set the top margin.</li>
  </ol>
  <p>Is it right?</p>
  <p><strong>EDIT 2:</strong></p>
  <p>I tried to do as you say to me and now I have the following situations:</p>
  <ol>
  <li><p>Into my <strong>PdfHeaderFooter</strong> that extends <strong>PdfPageEventHelper</strong> I have deleted the <strong>OnStartPage()</strong> method</p>
  </li>
  <li><p>I have moved the header table creation into the <strong>OnEndPage()</strong> method that now it this one:</p>
  <pre><code> // write on end of each page
   public override void OnEndPage(PdfWriter writer, Document document)
   {
       base.OnEndPage(writer, document);
  
       // HEADER:
       PdfPTable tabHead = new PdfPTable(3);
       tabHead.SetWidths(new int[] { 165, 205, 125 });
  
       //tabHead.TotalWidth = 460F;
       tabHead.TotalWidth = document.Right - document.Left;        // TotalWidth = 495
       tabHead.WidthPercentage = 98;
  
       PdfPCell cell1 = new PdfPCell(iTextSharp.text.Image.GetInstance(folderImages + &quot;logoEarlyWarning.png&quot;), true) { Border = PdfPCell.BOTTOM_BORDER };
       tabHead.AddCell(cell1);
       //tabHead.AddCell(new PdfPCell(new Phrase(&quot;CELL 1:&quot;)) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15, });
  
       tabHead.AddCell(new PdfPCell(new Phrase(&quot;&quot;)) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15 });
  
       if (_sourceId == &quot;NVD&quot;)
       {
           iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(folderImages + &quot;nvdLogo.png&quot;);
           logo.ScalePercent(48f);
           //PdfPCell cell3 = new PdfPCell(iTextSharp.text.Image.GetInstance(folderImages + &quot;nvdLogo.png&quot;), true) { Border = PdfPCell.BOTTOM_BORDER, PaddingBottom = 25 };
           PdfPCell cell3 = new PdfPCell(logo) { Border = PdfPCell.BOTTOM_BORDER, PaddingLeft = 50 };
           tabHead.AddCell(cell3);
       }
       else if (_sourceId == &quot;DeepSight&quot;)
       {
           PdfPCell cell3 = new PdfPCell(iTextSharp.text.Image.GetInstance(folderImages + &quot;DSLogo.jpg&quot;), true) { Border = PdfPCell.BOTTOM_BORDER };
           tabHead.AddCell(cell3);
       }
       //tabHead.AddCell(new PdfPCell(new Phrase(&quot;CELL 3:&quot;)) { Border = PdfPCell.BOTTOM_BORDER, Padding = 5, MinimumHeight = 50, PaddingTop = 15 });
  
  
       tabHead.WriteSelectedRows(0, -1, document.Left, document.Top, writer.DirectContent);
  
       float headerHeight = tabHead.CalculateHeights();
  
  
  
       // FOOTER:
       int pageN = writer.PageNumber;      // numero della pagina corrente OK
       String text = &quot;Page &quot; + pageN + &quot; of &quot;;
       float len = bf.GetWidthPoint(text, 8);
  
       Rectangle pageSize = document.PageSize;
  
       cb.SetRGBColorFill(100, 100, 100);
  
       cb.BeginText();
       cb.SetFontAndSize(bf, 8);
       cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30));
       cb.ShowText(text);
       cb.EndText();
  
       cb.AddTemplate(template, pageSize.GetLeft(40) + len, pageSize.GetBottom(30));
  
       cb.BeginText();
       cb.SetFontAndSize(bf, 8);
       cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,
           &quot;Printed On &quot; + PrintTime,
           pageSize.GetRight(40),
           pageSize.GetBottom(30), 0);
       cb.EndText();
   }
  </code></pre>
  </li>
  </ol>
  <p>As you can see now the <strong>OnEndPage()</strong> method contains the header and footer creation.</p>
  <p>It seems to me that <strong>tabHead</strong> (my header table) use the absolute positioning because I have:</p>
  <pre><code>tabHead.WriteSelectedRows(0, -1, document.Left, document.Top, writer.DirectContent);
  </code></pre>
  <p>In the other class where I create the document I have this line:</p>
  <pre><code>document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 50, 50, 30, 65);
  </code></pre>
  <p>so the pages are A4 and have 30px as upper margin.</p>
  <p>But I still have the same problem.</p>
  <p>If I change the 30 value with 80 it simply moves down the header leaving a white top but does not solve the problem.</p>
  <p>What am I missing? What is wrong?</p>