title: Printing Multiple pages in JAVA
body: <p>So I was tinkering with this code that I found <b><a href="http://docs.oracle.com/javase/tutorial/2d/printing/examples/PaginationExample.java" rel="nofollow">http://docs.oracle.com/javase/tutorial/2d/printing/examples/PaginationExample.java</a></b>
  I altered the page size to 300, 400 when I tried printing to XPS the first page is always default and the following is 300,400, I want to is set all printed pages to 300,400</p>
  
  <p>below is the code</p>
  
  <pre><code>package app;
  
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import java.awt.print.*;
  
  public class PaginationExample implements Printable, ActionListener {
  
  int[] pageBreaks;  // array of page break line positions.
  
  /* Synthesise some sample lines of text */
  String[] textLines;
  
  private void initTextLines() {
      if (textLines == null) {
          int numLines = 200;
          textLines = new String[numLines];
          for (int i = 0; i &lt; numLines; i++) {
              textLines[i] = "This is line number " + i;
          }
      }
  }
  
  public int print(Graphics g, PageFormat pf, int pageIndex)
          throws PrinterException {
      pf.setPaper(paper);
  
  
      Font font = new Font("Serif", Font.PLAIN, 10);
      FontMetrics metrics = g.getFontMetrics(font);
      int lineHeight = metrics.getHeight();
  
      if (pageBreaks == null) {
          initTextLines();
          int linesPerPage = (int) (pf.getImageableHeight() / lineHeight);
          int numBreaks = (textLines.length - 1) / linesPerPage;
          pageBreaks = new int[numBreaks];
  
          for (int b = 0; b &lt; numBreaks; b++) {
              pageBreaks[b] = (b + 1) * linesPerPage;
          }
      }
  
      if (pageIndex &gt; pageBreaks.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 = 0;
      int start = (pageIndex == 0) ? 0 : pageBreaks[pageIndex - 1];
      int end = (pageIndex == pageBreaks.length)
              ? textLines.length : pageBreaks[pageIndex];
      for (int line = start; line &lt; end; line++) {
          y += lineHeight;
          g.drawString(textLines[line], 0, y);
      }
  
      /* tell the caller that this page is part of the printed document */
      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 */
          }
      }
  }
  private static Paper paper;
  public static void main(String args[]) {
  
      paper = new Paper();
      paper.setSize(300, 400);
  
  
      try {
          String cn = UIManager.getSystemLookAndFeelClassName();
          UIManager.setLookAndFeel(cn); // Use the native L&amp;F
      } catch (Exception cnf) {
      }
      JFrame f = new JFrame("Printing Pagination Example");
      f.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
              System.exit(0);
          }
      });
      JButton printButton = new JButton("Print Pages");
      printButton.addActionListener(new PaginationExample());
      f.add("Center", printButton);
      f.pack();
      f.setVisible(true);
  }
  }          
  </code></pre>