title: Wrong iteration while printing a multiple page(Possibly printer or PDF) document using Java Printing API
body: <p>I am using Java Printing API and have got the code of printing multiple page document of billing invoice from this oracle page.</p>
  <p><a href="https://docs.oracle.com/javase/tutorial/2d/printing/set.html" rel="nofollow noreferrer">Printing a Multiple Page Document</a></p>
  <p>And Example code from this page</p>
  <p><a href="https://docs.oracle.com/javase/tutorial/2d/printing/examples/PaginationExample.java" rel="nofollow noreferrer">PaginationExample</a></p>
  <p>Now as you know most invoice have billing details of items start from center-top of the page which is dynamic values sometimes details get pushed to next page.</p>
  <p>I fiddle and modify with above code and below is my working code.</p>
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import java.awt.print.*;
  
  public class PaginationExample implements Printable, ActionListener {
  
      int[] pageBreaks1, pageBreaks2;  // array of page break line positions.
  
      /* Synthesize some sample lines of text */
      String[][] textLines;
      private void initTextLines() {
          if (textLines == null) {
              int numLines=300;
              textLines = new String[numLines][3];
              textLines = new String[numLines][4];
              for (int i=0;i&lt;numLines;i++) {
                  textLines[i][0]= &quot;1. This is line number &quot; + i;
                  textLines[i][1]= &quot;2. This is line number &quot; + i;
                  textLines[i][2]= &quot;3. This is line number &quot; + i;
              }
          }
      }
  
      public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
  
          Font font = new Font(&quot;Serif&quot;, Font.PLAIN, 10);
          FontMetrics metrics = g.getFontMetrics(font);
          int lineHeight = metrics.getHeight();
          
          int linesPerPage1 = (int)(550/lineHeight);
          int linesPerPage2 = (int)(750/lineHeight);
          
          initTextLines();
          
          if (pageBreaks1 == null) {            
              int numBreaks1 = (textLines.length-1)/linesPerPage1;
              pageBreaks1 = new int[numBreaks1];
              for (int b1=0; b1&lt;numBreaks1; b1++) {
                  pageBreaks1[b1] = (b1+1)*linesPerPage1;
              }
          }
          
          if (pageBreaks2 == null) {            
              int numBreaks2 = (textLines.length-1)/linesPerPage2;
              pageBreaks2 = new int[numBreaks2];
              for (int b2=0; b2&lt;numBreaks2; b2++) {
                  pageBreaks2[b2] = (b2+1)*linesPerPage2; 
              }
          }
  
          if (pageIndex &gt; pageBreaks1.length) {
              return NO_SUCH_PAGE;
          }
          if (pageIndex &gt; pageBreaks2.length) {
              return NO_SUCH_PAGE;
          }
  
          /* User (0,0) is typically outside the imageable area, so we must
           * translate by the X and Y values in the PageFormat to avoid clipping
           * Since we are drawing text we
           */
          
          Graphics2D g2d = (Graphics2D)g;
          g2d.translate(pf.getImageableX(), pf.getImageableY());
  
          /* Draw each line that is on this page.
           * Increment 'y' position by lineHeight for each line.
           */
          
          int y = (pageIndex == 0) ? 200 : 30;
          
          int start = (pageIndex == 0) ? ((pageIndex == 0) ? 0 : pageBreaks1[pageIndex-1])
                                       : ((pageIndex == 0) ? 0 : pageBreaks2[pageIndex-1]);
          
          int end   = (pageIndex == 0) ? ((pageIndex == pageBreaks1.length) ? textLines.length : pageBreaks1[pageIndex]) 
                                       : ((pageIndex == pageBreaks2.length) ? textLines.length : pageBreaks2[pageIndex]);
          
          for (int line=start; line&lt;end; line++) {
              y += lineHeight;
              g.drawString(textLines[line][0], 10, y);
              g.drawString(textLines[line][1], 100, y);
              g.drawString(textLines[line][2], 200, y);
              g.drawString(textLines[line][3], 300, y);
              
          }
          return PAGE_EXISTS;
      }
  
      public void actionPerformed(ActionEvent e) {
           PrinterJob job = PrinterJob.getPrinterJob();
           job.setPrintable(this);
           boolean ok = job.printDialog();
           if (ok) {
               try {
                    job.print();
               } catch (PrinterException ex) {
                /* The job did not successfully complete */
               }
           }
      }
      
      public static void main(String args[]) {
          try {
              String cn = UIManager.getSystemLookAndFeelClassName();
              UIManager.setLookAndFeel(cn); // Use the native L&amp;F
          } catch (Exception cnf) {
          }
          JFrame f = new JFrame(&quot;Printing Pagination Example&quot;);
          
          f.addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent e) {System.exit(0);}
          });
          
          JButton printButton = new JButton(&quot;Print Pages&quot;);
          printButton.addActionListener(new PaginationExample());
          f.add(&quot;Center&quot;, printButton);
          f.pack();
          f.setVisible(true);
      }
  }
  
  
  </code></pre>
  <p>Now the issue I get is this in below picture.</p>
  <p><a href="https://i.stack.imgur.com/0F8PV.jpg" rel="nofollow noreferrer">pdf output image</a></p>
  <p>What is happening is code is taking the iteration wrong because of and jumping some elements in the array and this error is only happening in first page.</p>
  <p>I do believe my problem in the above posted code lies here.</p>
  <pre><code>int y = (pageIndex == 0) ? 200 : 30;
          
          int start = (pageIndex == 0) ? ((pageIndex == 0) ? 0 : pageBreaks1[pageIndex-1])
                                       : ((pageIndex == 0) ? 0 : pageBreaks2[pageIndex-1]);
          
          int end   = (pageIndex == 0) ? ((pageIndex == pageBreaks1.length) ? textLines.length : pageBreaks1[pageIndex]) 
                                       : ((pageIndex == pageBreaks2.length) ? textLines.length : pageBreaks2[pageIndex]);
          
          for (int line=start; line&lt;end; line++) {
              y += lineHeight;
              g.drawString(textLines[line][0], 10, y);
              g.drawString(textLines[line][1], 100, y);
              g.drawString(textLines[line][2], 200, y);
              g.drawString(textLines[line][3], 300, y);
              
          }
  </code></pre>
  <p>It tried fixing the iteration but it doesn't work. Please if possible guide me where I am going wrong here.</p>
  <p>Thank you :).</p>