title: iText 7 dynamic page header issue
body: <p>I am trying to create dynamic page header with itext7 using EventHandlers. I am using the below code for this:</p>
  
  <pre><code>    public static void main(String[] args) throws Exception {
      File file = new File("C:\\Test\\variable_header.pdf");
      file.getParentFile().mkdirs();
      new VariableHeader().manipulatePdf(DEST);
  }
  
  protected void manipulatePdf(String dest) throws Exception {
      PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
      Document doc = new Document(pdfDoc);
      VariableHeaderEventHandler handler = new VariableHeaderEventHandler();
      pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, handler);
      handler.setHeader("First Header");
      for (int i = 1; i &lt;= 50; i++) {
          doc.add(new Paragraph("Added First content"));
       }
      doc.add(new AreaBreak());
      handler.setHeader("Second Header");
      for (int i = 1; i &lt;= 15; i++) {
          doc.add(new Paragraph("Added Second content"));
      }
      doc.close();
  }
  
  protected class VariableHeaderEventHandler implements IEventHandler {
      protected String header;
  
      public void setHeader(String header) {
          this.header = header;
      }
  
      @Override
      public void handleEvent(Event event) {
          PdfDocumentEvent documentEvent = (PdfDocumentEvent) event;
          try {
              new PdfCanvas(documentEvent.getPage())
                      .beginText()
                      .setFontAndSize(PdfFontFactory.createFont(FontConstants.HELVETICA), 12)
                      .moveText(450, 806)
                      .showText(header)
                      .endText()
                      .stroke();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>
  
  <p>Here I created 'VariableHeaderEventHandler' that renders the dynamic page header for every page of the document. </p>
  
  <p>Initially I set the page header as "First Header" and added the text "Added First content" to the document for 50 times. Then I added the Area break which leaves the further part of the page empty after rendering the previous contents and any further contents will be rendered in a new page. Then I changed the page header as "Second Header" and added the text "Added Second content" to the document for 15 times.</p>
  
  <p>So as per the above code, the page header "Second Header" should be rendered ONLY from the page where the text "Added Second content" is being displayed. But the "Second Header" is rendered in the previous page where the text "Added First content" is displayed as shown below:</p>
  
  <p>iText7 Dynamic header issue:</p>
  
  <p><a href="https://i.stack.imgur.com/a3JVB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/a3JVB.png" alt="Screenshot"></a></p>
  
  <p>It looks like there is some issue with the Event Handling mechanism in iText 7.0.1. </p>
  
  <p>Is there some way to solve this issue? Any help would be appreciated. </p>