Printing in Java
Printing is often a desirable feature for an application, but not something you want to devote a lot of time implementing. You just want to print a component. Unfortunately, the standard Java SE library from Sun only offers only very low-level access to the printing API.
This does allow flexible and powerful control, but (as I found out) it also means that carrying out simple printing tasks can be a cumbersome, lengthy process. Hopefully, the generic printing package I have developed can help other Java developers out there to quickly get into printing, or at least to give suggestions of how to develop your own. So here it is.
Features:
- Print an awt / swing component, such as a JPanel or JTextPane.
- Scale the item to print to whatever size you need, e.g. scale to fill a page.
- Print a single awt / swing component split over multiple pages
- Print multiple pages, e.g. a book
Download
Source code (.zip)
Netbeans project (.zip)
Documentation
Quick-start
To use the printing library, download the jar file, and add it to your classpath.
Printing a component, using it’s preferred size, on a single page
- Create a PrintManager object.
PrintManager manager = new PrintManager();
- (Optional) Show the print setup dialog to allow the user to choose print settings.
manager.showPageSetupDialog();
- Create a page, passing the component you wish to print to the constructor.
Page page = new Page(yourComponent);
- Pass the page to the PrintManager’s printSingleSwingComponent method.
manager.printSingleSwingComponent(page);
Printing a component to fill the whole page
- Create a PrintManager object.
PrintManager manager = new PrintManager();
- (Optional) Show the print setup dialog to allow the user to choose print settings.
manager.showPageSetupDialog();
- Create a page, passing a PrintScale object to the constructor, along with the component you want to print.
Page page = new Page(yourComponent, new PrintScale(1, PrintScale.SCALE_TO_PAGE));
Use the double argument of the PrintScale object to specify what proportion of the page to print. For example, 0.5 will print to fill half the page:
Page page = new Page(yourComponent, new PrintScale(0.5, PrintScale.SCALE_TO_PAGE));
- Pass the page to the PrintManager’s printSingleSwingComponent method.
manager.printSingleSwingComponent(page);
Printing a component at half it’s actual size
The example above scales the component according to the page size (e.g. the whole page, or half a page). If you want to apply the scaling to the component itself (e.g. half the component’s actual size, or double the component’s actual size), use the PrintScale.SCALE_TO_COMPONENT argument in the PrintScale constructor.
- Create a PrintManager object.
PrintManager manager = new PrintManager();
- (Optional) Show the print setup dialog to allow the user to choose print settings.
manager.showPageSetupDialog();
- Create a page, passing a PrintScale object to the constructor, along with the component you want to print.
Page page = new Page(yourComponent, new PrintScale(0.5, PrintScale.SCALE_TO_COMPONENT));
Use the double argument of the PrintScale object to specify how much to scale the component. For example, 0.75 will print the component at three-quarters of it’s size.
Page page = new Page(yourComponent, new PrintScale(0.75, PrintScale.SCALE_TO_COMPONENT));
- Pass the page to the PrintManager’s printSingleSwingComponent method.
manager.printSingleSwingComponent(page);
