title: Java: Unable to set page size while printing a PDF with SimpleDoc
body: <p>I am currently using this code to print a document (which is passed as a byte array). It is important to note that the document already has the correct size (which I set with iText):</p>
  
  <pre><code>private void printReceipt(ByteArrayOutputStream baos) throws PrinterException {
  
      //Retrieve byte array (which is in fact the pdf file)
      ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
      Doc pdfDoc = new SimpleDoc(bais, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
  
      //Select the right printer
      String printerNameDesired = "Star TSP143 (STR_T-001)";
      PrintService[] service = PrinterJob.lookupPrintServices(); // list of printers
      DocPrintJob docPrintJob = null;
  
      for (int i = 0; i &lt; service.length; i++) {
          if (service[i].getName().equalsIgnoreCase(printerNameDesired )) {
              docPrintJob = service[i].createPrintJob();
              break;
          }
      }
  
      //Print the byte array
      try {
          final HashPrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
          attrs.add(PrintQuality.HIGH);
          attrs.add(MediaSizeName.ISO_A8);
          docPrintJob.print(pdfDoc, attrs);
      } catch (PrintException ex) {
          ex.printStackTrace();
      }
  
      //Close the bytearray inputstream to prevent memory issues
      try {
          bais.close();
      } catch (IOException ex) {
          ex.printStackTrace();
      }
  
      //Delete the pdfDoc to prevent memory issues
      pdfDoc =null;
  
  }
  </code></pre>
  
  <p>This code works for me, the only problem is that the height of the document is not correct. I need to set a custom page size. However, I have to choose a <code>MediaSizeName</code>, and I can not set a custom page size myself. I would like to set the page formatting like this:</p>
  
  <pre><code>PageFormat pf = new PageFormat();
  pf.setOrientation(LANDSCAPE);
  Paper paper = pf.getPaper();
  paper.setSize(1.102 * 72, 3.504 * 72);
  paper.setImageableArea(0, 10, 1.0 * 72, 3.4 * 72);
  pf.setPaper(paper);
  </code></pre>
  
  <p>I don't know how to add this PageFormat to my SimpleDoc. I can not convert my SimpleDoc to a Printable like this:</p>
  
  <pre><code>PrinterJob job = PrinterJob.getPrinterJob();
  job.setPrintService(docPrintJob.getPrintService());
  
  Book book = new Book();//java.awt.print.Book
  book.append(SimpleDocConvertedToPrintable, pf);
  job.setPageable(book);
  
  job.print();
  </code></pre>
  
  <p>Unfortunately, I can not get this to work. I also thought of setting the custom MediaSize from within the <code>HashPrintRequestAttributeSet</code>, but I could not find it anywhere.</p>
  
  <p>So my question is: how can I create a custom MediaSize, or is there maybe a better and quicker way to print a PDF without saving it to a file?</p>
  
  <p>Any help is greatly appreciated!</p>