Of course, in many situations, you want to do more than print the appearance of a component. You often want to print the contents of some component, rather than the component itself. For example, you may want to print the text the user has typed into a text area, rather than the text area itself. Or you may want to print the contents of a spreadsheet, rather than the collection of components that compose the spreadsheet.
Java 1.1 lets you print arbitrary content, which may include multipage documents. You aren't restricted to printing your components' appearance. In many ways, the steps required to print arbitrary content are similar to those we outlined previously. However, a few tricks are involved:
PrintJob pjob = getToolkit().getPrintJob(this, "Job Title", (Properties)null);
Graphics pg = pjob.getGraphics();
pjob.getPageDimension();
Font times = new Font ("SansSerif", Font.PLAIN, 12); pg.setFont(times); FontMetrics tm = pg.getFontMetrics(times);
pg.dispose(); // This is like sending a form feed
pjob.end();
Remember to set a font for the PrintGraphics object explicitly! It doesn't have a default font.
An example that loads and prints a text file is available from this book's Web page.