title: iText 5.5.x - Java - page breaks
body: <p>How can i add page breaks to a document using Java iText 5.5.x using the y position on the page? I have overridden the Div class to use a custom HTML tag, which denotes what content to keep together (because the conformance list [<a href="http://demo.itextsupport.com/xmlworker/itextdoc/CSS-conformance-list.htm]" rel="nofollow">http://demo.itextsupport.com/xmlworker/itextdoc/CSS-conformance-list.htm]</a> indicates that page breaks in HTML must be 'always'). I can't seem to establish how to break when the content will be greater than the size I want on the page. The PDF generation is <em>not</em> using the "standard" way of creating a PDF, but getting content from parsing a HTML file.</p>
  
  <p>Below is what I have for the Div class:</p>
  
  <pre><code>class PageBreak extends Div {
  private Document document;
  private PdfWriter writer;
  private float breakPoint;
  private float signatureArea = 80;
  
  public PageBreak(Document document, PdfWriter writer) {
      this.document = document;
      this.writer = writer;
      this.breakPoint = (document.top() - signatureArea - 40 /*padding*/);
  }
  
  @Override
  public List&lt;com.itextpdf.text.Element&gt; end(WorkerContext ctx, Tag tag, List&lt;com.itextpdf.text.Element&gt; currentContent) {
      System.out.println("END POS: " + writer.getVerticalPosition(true));
      System.out.println("writer.getVerticalPosition(true) " + writer.getVerticalPosition(true));
      System.out.println("breakPoint: " + breakPoint);
  
      boolean newPage = writer.getVerticalPosition(true) &gt; breakPoint;
  
      if(newPage) {
          System.out.println("breaking");
          document.newPage();
      }
  
      return currentContent;
  }
  
  @Override
  public List&lt;com.itextpdf.text.Element&gt; start(WorkerContext ctx, Tag tag) {
      System.out.println("START POS: " + writer.getVerticalPosition(true));
      System.out.println("writer.getVerticalPosition(true) " + writer.getVerticalPosition(true));
      System.out.println("breakPoint: " + breakPoint);
      return new ArrayList&lt;com.itextpdf.text.Element&gt;(0);
    }}
  </code></pre>
  
  <p>An example of the HTML:</p>
  
  <pre><code> &lt;pagebreak&gt;
   &lt;table&gt;&lt;tr&gt;&lt;td class="section-h3"&gt;A table&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
   &lt;table&gt;
   &lt;tr&gt;
   &lt;td class="withback-70"&gt;Reviewed By&lt;/td&gt;
   &lt;td class="withoutback-70"&gt;Jim&lt;/td&gt;
   &lt;/tr&gt;
   &lt;/table&gt;
   &lt;/pagebreak&gt;
  </code></pre>
  
  <p>This is the code to convert / parse from HTML to PDF:</p>
  
  <pre><code>    Document document = new Document();
      PdfWriter writer = PdfWriter.getInstance(document, outputStream);
      writer.setPageEvent(new HeaderFooter(mappedMasterIncident));
      document.setPageSize(PageSize.LETTER);
      document.setMargins(18, 18, 18, 95);
      document.open();
  
      HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
      //not working
      //TagProcessorFactory factory = Tags.getHtmlTagProcessorFactory();
      //factory.addProcessor(new PageBreak(document, writer), "pagebreak");
      //htmlContext.setTagFactory(factory);
      htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
      htmlContext.setImageProvider(new Base64ImageProvider());
  
      CSSResolver cssResolver = new StyleAttrCSSResolver();
      cssResolver.addCssFile("file:///C|/Users/jim.willmore/Desktop/pdf.css", true);
      Pipeline&lt;?&gt; pipeline = new CssResolverPipeline(cssResolver,new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));
  
      XMLWorker worker = new XMLWorker(pipeline, true);
      XMLParser p = new XMLParser( worker);
      p.parse(templateReader);
  
      document.close();
  </code></pre>