title: java.awt.print How to set default page size to A6
body: <p>I have a java class with a static method that prints a JPanel which implements a custom interface (called MyPrintablePanel).</p>
  <p>The problem is, when the print dialog appears (Page setup dialog box), the default paper size is &quot;Letter&quot;. I want the default paper size to be A6 so that I don't have to manually choose A6 each time I print.</p>
  <p>Here is the code:</p>
  <pre><code>package designer.office.utils;
  
  import designer.office.printables.MyPrintablePanel;
  import java.awt.Component;
  import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.print.PageFormat;
  import java.awt.print.Paper;
  import java.awt.print.Printable;
  import java.awt.print.PrinterException;
  import java.awt.print.PrinterJob;
  import javax.print.attribute.Size2DSyntax;
  import javax.print.attribute.standard.MediaSize;
  import javax.print.attribute.standard.MediaSizeName;
  import javax.swing.JPanel;
  
  /**
   * A class to print a JPanel
   */
  public class MyPrinter implements Printable {
      
      final MyPrintablePanel comp;
  
      public MyPrinter(MyPrintablePanel comp){
          this.comp = comp;
      }
      
      public static void printJPanel(MyPrintablePanel jPanel){
  
          PrinterJob pjob = PrinterJob.getPrinterJob();
          MediaSize isoA6Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A6);
          float[] size = isoA6Size.getSize(Size2DSyntax.INCH);
          Paper paper = new Paper();
          paper.setSize(size[0] * 72.0, size[1] * 72.0);
          paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
          PageFormat preformat = new PageFormat();
          preformat.setPaper(paper);
          
          PageFormat postformat = pjob.pageDialog(preformat);
          //If user does not hit cancel then print.
          if (preformat != postformat) {
              //Set print component
              pjob.setPrintable(new MyPrinter(jPanel), postformat);
              if (pjob.printDialog()) {
                  try {
                      pjob.print();
                  } catch (PrinterException ex) {
                      System.out.println(&quot;Failed to print: &quot; + ex.getMessage());
                  }
              }
          }
      }
  
      @Override
      public int print(Graphics g, PageFormat format, int page_index) 
              throws PrinterException {
          if (page_index &gt; this.comp.getNumOfPages() - 1) {
              return Printable.NO_SUCH_PAGE;
          }
  
          // get the bounds of the component
          Dimension dim = comp.getSize();
          double cHeight = dim.getHeight()/this.comp.getNumOfPages();
          double cWidth = dim.getWidth();
  
          // get the bounds of the printable area
          double pHeight = format.getImageableHeight();
          double pWidth = format.getImageableWidth();
  
          double pXStart = format.getImageableX();
          double pYStart = format.getImageableY();
  
          double xRatio = pWidth / cWidth;
          double yRatio = pHeight / cHeight;
  
  
          Graphics2D g2 = (Graphics2D) g;
          g2.translate(pXStart, pYStart - page_index * pHeight);
          g2.scale(xRatio, yRatio);
          comp.paint(g2);
  
          return Printable.PAGE_EXISTS;
      }
  }
  </code></pre>
  <p>I'm printing (or rather my client is printing) from a <code>HP Laser Jet Professional P1102</code> printer with A6 paper in its tray.</p>