title: Print Receipt in Java with some Font Size
body: <p>I tried to print POS receipt with code that I got from <a href="https://stackoverflow.com/questions/12336607/thermal-printer-java">here</a>. It's works fine but I want to have some different font sizes in the receipt. After searching in the forum, I just found out that a JTextArea just only can have 1 font. So, How can it be done? I'm new in Java so I'm prefer a simple solution for this, Thanks.</p>
  
  <pre><code>public class NewClass {
  
  public void a() {
      PageFormat format = new PageFormat();
      Paper paper = new Paper();
  
      double paperWidth = 3;//3.25
      double paperHeight = 3.69;//11.69
      double leftMargin = 0.12;
      double rightMargin = 0.10;
      double topMargin = 0;
      double bottomMargin = 0.01;
  
      paper.setSize(paperWidth * 200, paperHeight * 200);
      paper.setImageableArea(leftMargin * 200, topMargin * 200,
              (paperWidth - leftMargin - rightMargin) * 200,
              (paperHeight - topMargin - bottomMargin) * 200);
  
      format.setPaper(paper);
  
      PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
      aset.add(OrientationRequested.PORTRAIT);
  
  
      PrinterJob printerJob = PrinterJob.getPrinterJob();
      Printable printable = new ReceiptPrint();
  
      format = printerJob.validatePage(format);
      boolean don = printerJob.printDialog();
      printerJob.setPrintable(printable, format);
      try {
          printerJob.print(aset);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  }
  
  class ReceiptPrint implements Printable {
  
  SimpleDateFormat df = new SimpleDateFormat();
  String receiptDetailLine;
  public static final String pspace = "               ";//15-spaces
  
  public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
          throws PrinterException {
  
      df.applyPattern("dd/MM/yyyy HH:mm:ss");
      String strText = null;
      final String LF = "\n";// text string to output
      int lineStart;           // start index of line in textarea
      int lineEnd;             // end index of line in textarea
      int lineNumber;
      int lineCount;
      final String SPACE = "          ";//10 spaces
      final String SPACES = "         ";//9
      final String uline = "________________________________________";
      final String dline = "----------------------------------------";
      String greetings = "THANKS FOR YOUR VISIT";
      receiptDetailLine = "asdasdasda";
  
      Graphics2D g2d = (Graphics2D) graphics;
      Font font = new Font("MONOSPACED", Font.BOLD, 9);
  
      if (pageIndex &lt; 0 || pageIndex &gt;= 1) {
          return Printable.NO_SUCH_PAGE;
      }
      JTextArea textarea = new JTextArea(10, 40);
  
  
      textarea.append(SPACES + "sadasdsad" + "\n");
  
      textarea.append(" " + SPACES + "sadasdsad" + "\n");
  
      textarea.append(SPACES + "sadasdsad" + "\n");
  
      textarea.append("" + SPACES + "sadasdsad" + "\n");
  
      textarea.append(SPACES + "sadasdsad" + "\n");
  
      textarea.append(uline + "\n");
      textarea.append("Order Ref:" + "   " + receiptDetailLine + "\n");
      textarea.append(dline + "\n");
      textarea.append(" Qty     Description" + SPACES + "  Price" + LF);
      textarea.append(dline + "\n");
  
      System.out.println(2);
  
      String printedLine = "       Service Charge Complimentary";
      textarea.append(printedLine + LF);
  
      textarea.append(LF + SPACES + "   Your Reciept\n" + SPACE + greetings + LF);
      textarea.append(df.format(new Date()) + LF);
      textarea.setEditable(false);
  
      g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
  
      g2d.setFont(font);
      lineNumber = 0;
      lineCount = textarea.getLineCount();
      strText = textarea.getText();
      /*MediaTracker mt = new MediaTracker(textarea);
       URL imageURL = null;
       try {
  
       imageURL = new URL(mainDirectory+"quindell.png");
       } catch (MalformedURLException me) {
       me.printStackTrace();
       }
  
       //--- Load the image and wait for it to load
       Image image = Toolkit.getDefaultToolkit().getImage(imageURL);
       mt.addImage(image, 0);
       */
  
      while (lineCount != 0) {
          try {
  
              lineStart = textarea.getLineStartOffset(lineNumber);
              lineEnd = textarea.getLineEndOffset(lineNumber);
              strText = textarea.getText(lineStart, lineEnd - lineStart);
          } catch (Exception exception) {
              System.out.println("Printing error:" + exception);                  // have to catch BadLocationException
          }
  
          g2d.drawString(strText, 1, (lineNumber + 1) * 18);
          //spacing    between lines
          lineNumber = lineNumber + 1;
          lineCount--;
      }
      return Printable.PAGE_EXISTS;
  }
  }
  </code></pre>