title: Itextsharp - check to see if adding an element will create a new page
body: <p>I'm using ITextSharp to convert an HTML document into PDF. I use HTMLWorker.ParseToList and loop through each item in turn. That works fine, however the first page is required to have different margin sizes to the subsequent pages. I can do this by calling MyDocument.NewPage() and calling MyDocument.SetMargins().<br /><br />
  My problem occurs when trying to detect the page transitions.<br /><br />
  I can use a loop to keep track of page transitions, then call NewPage() and reset the margins, however, this can only happen once I've actually added a paragraph that wraps onto a new page, leaving an entire page that's practically white-space.<br /><br />
  I need a way of pre-emptively detecting if the page will change if I add a certain Paragraph object.
  I've tried using ColumnText.Go(true) to simulate it (and if the result of that is ColumnText.NO_MORE_COLUMN, then make it a page break), unfortunately that appears to be flakey at best, and tends to detect page breaks in entirely the wrong places.<br /><br />
  Here's my current code:</p>
  
  <pre><code>            ColumnText simulation = new ColumnText(Writer.DirectContent);
              simulation.SetSimpleColumn(Writer.PageSize);   
              bool FirstPage = true;
              foreach (var item in ItemList)
              {
                  var para = new Paragraph("", Normal);
                  para.AddAll(item.Chunks);                    
                  para.SpacingAfter = 10;
                  foreach (Chunk c in item.Chunks)
                  {
                      simulation.AddText(c);
                  }
                  if(FirstPage) {
                      int simresult = simulation.Go(true);
                      if(simresult == (int)ColumnText.NO_MORE_COLUMN)
                      {
                          textDocument.SetMargins(100,100,100,100);
                          textDocument.NewPage();    
                          FirstPage = false;                 
                      }
                  }
  
                  textDocument.Add(para);
              }
  </code></pre>
  
  <p>This results in it not detecting a page break until the end of page 2. Which is no good.
  <br /><br />
  The only way I've found to get it working is to HALF the height going into simulation.SetSimpleColumn.
  <br /><br />
  It works, but I don't know why, and honestly, that's no good. If anyone can give me any insight that'd be great.
  <br /><br />
  <hr />
  <br />
  Thanks to Alexis I've worked it out. The ITextSharp follows the Java event model pretty thoroughly, which is annoying since I was looking for events in the Writer and Document directly.
  First I had to create a class that overrode PdfPageEventHelper:</p>
  
  <pre><code>internal class MainTextEventsHandler : PdfPageEventHelper
  {
      public override void OnStartPage(PdfWriter writer, Document document)
      {
          document.SetMargins(document.LeftMargin, document.LeftMargin, document.TopMargin, document.BottomMargin); //Mirror the horizontal margins
          document.NewPage(); //do this otherwise the margins won't take
      }
  }
  </code></pre>
  
  <p>Next, I set the PageEvent property of the Writer object, and I modified my loop to remove the simulation.</p>
  
  <pre><code>        Writer.PageEvent = new MainTextEventsHandler();
          foreach (var item in ItemList)
          {
              var para = new Paragraph("", Normal);
              para.AddAll(item.Chunks);
              /* per-paragraph stuff here */
              para.SpacingAfter = 10;                    
              textDocument.Add(para);
          }
  </code></pre>