title: Exception thrown upon assigning of custom page event to PdfCopy
body: <p>I am currently trying to iterate over an existing PDF and stamp each page with some footer text using the <code>OnPageEnd</code> event as detailed in the iText documentation, <a href="http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-5#225-moviecountries1.java" rel="nofollow noreferrer">Chapter 5: Table, cell, and page events</a>.</p>
  
  <p>When I assign my new custom event class to the PdfCopy instance, I receive this exception:</p>
  
  <blockquote>
    <p>"Operation is not valid due to the current state of the object" at
    iTextSharp.text.pdf.PdfCopy.set_PageEvent(IPdfPageEvent value)</p>
  </blockquote>
  
  <p>Below is the code I have written to preform the operation:</p>
  
  <pre><code>PdfReader pdf = new PdfReader(file.Value);
  int pages = pdf.NumberOfPages;
  
  pdf.SelectPages(string.Format("0 - {0}", pages));
  using (MemoryStream stream = new MemoryStream())
  {
      Document doc = new Document();
      PdfCopy copy = new PdfCopy(doc, stream) { PageEvent = new PdfFooterStamp() };
  
      doc.Open();
      for (int x = 0, y = pages; x &lt; y; x++)
      {
          copy.AddPage(copy.GetImportedPage(pdf, x + 1));
      }
  
      doc.Close();
      copy.Flush();
      copy.Close();
  
      collection[file.Key] = stream.ToArray();
  }
  </code></pre>
  
  <p>And this is my custom event class definition:</p>
  
  <pre><code>public class PdfFooterStamp : PdfPageEventHelper
  {
      public override void OnEndPage(PdfWriter writer, Document document)
      {
          Rectangle rect = writer.PageSize;
          ColumnText.ShowTextAligned(writer.DirectContent,
              Element.ALIGN_CENTER, new Phrase("PERSONALISED DOCUMENT"),
                  (rect.Left + rect.Right) / 2, rect.Bottom - 18, 0);
          base.OnEndPage(writer, document);
      }
  }
  </code></pre>
  
  <p>Is there anyone that might have an idea as to what might be going wrong?</p>