text: This section explains how to create a basic printing program that displays a print dialog and prints the text "Hello World" to the selected printer.
  Printing task usually consists of two parts:
  - Job control — Creating a print job, associating it with a printer, specifying the number of copies, and user print dialog interaction.
  - Page Imaging — Drawing content to a page, and managing content that spans pages (pagination).
  First create the printer job. The class representing a printer job and most other related classes is located in the
  java.awt.printpackage.import java.awt.print.*; PrinterJob job = PrinterJob.getPrinterJob();
  Next provide code that renders the content to the page by implementing the
  Printableinterfacevariable will be true if the user gave a command to go ahead and print. If the
  doPrintvariable is false, the user cancelled the print job. Since displaying the dialog at all is optional, the returned value is purely informational.
  If the
  doPrintvariable is true, then the application will request that the job be printed by calling the
  PrinterJob.printmethod.if (doPrint) { try { job.print(); } catch (PrinterException e) { /* The job did not successfully complete */ } }
  The
  PrinterExceptionwill be thrown if there is problem sending the job to the printer. However, since the
  PrinterJob.printmethod returns as soon as the job is sent to the printer, the user application cannot detect paper jams or paper out problems. This job control boilerplate is sufficient for basic printing uses.
  The
  Printableinterface has only one method:public int print(Graphics graphics, PageFormat pf, int page) throws PrinterException;
  The
  PageFormatclassparameter is the zero-based page number that will be rendered.
  The following code represents the full
  Printableimplementation:import java.awt.print.*; import java.awt.*; public class HelloWorldPrinter implements Printable { */ g.drawString("Hello world!", 100, 100); /* tell the caller that this page is part of the printed document */ return PAGE_EXISTS; } }The complete code for this example is in
  HelloWorldPrinter.java.
  Sending a
  Graphicsinstance to the printer is essentially the same as rendering it to the screen. In both cases you need to perform the following steps:
  - To draw a test string is as easy as the other operations that were described for drawing to a
  Graphics2D.
  - Printer graphics have a higher resolution, which should be transparent to most code.
  - The
  Printable.print()method is called by the printing system, just as the
  Component.paint()method is called to paint a Component on the display. The printing system will call the
  Printable.print()method for page 0, 1,.. etc until the
  print()method returns
  NO_SUCH_PAGE.
  - The
  print()method may be called with the same page index multiple times until the document is completed. This feature is applied when the user specifies attributes such as multiple copies with collate option.
  - The PageFormat's imageable area determines the clip area. Imageable area is also important in calculating pagination, or how to span content across printed pages, since page breaks are determined by how much can fit on each page.
  Note: A call to the
  print()method may be skipped for certain page indices if the user has specified a different page range that does not involve a particular page index.