Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Printing multiple multi-page components with one PrintJob

843805Feb 1 2007 — edited Feb 2 2007
here's my situation....

I have an array of JEditorPane components. I need to print all the components in the array as one print job. The lenght of each component is dynamic, but all components will be of equal size.

imagine printing the same contract for three different users. The only difference between each contract is the users name, which is populated at runtime.

sometimes the contracts are really long and span multiple "pages", other times pretty short. I already have the code that will print a single component.....

What I need is a way to print all the contracts using only one print job so the user only has to see the print dialog once, but will get N number of pages printed, where N is NUM_PAGES_IN_CONTRACT * NUM_CONTRACTS.

any advice?

PS: it is not required to allow the user to specify a particular page to print. If they say yes to the dialog, they get all the pages....they say no, they get none
public class PrintUtilities implements Printable {
	private Component componentToBePrinted;
 
        public static void printComponent(Component c) {
		PrinterJob printJob = PrinterJob.getPrinterJob();
		printJob.setPrintable(new PrintUtilities(c));
		if (printJob.printDialog()){
			try {
				System.out.println("Calling PrintJob.print()");
				printJob.print();
				System.out.println("End PrintJob.print()");
			} catch (PrinterException pe) {
				System.out.println("Error printing: " + pe);
			}
		}
	}
 
        public PrintUtilities(Component componentToBePrinted) {
		this.componentToBePrinted = componentToBePrinted;
	}
 
public int print(Graphics g, PageFormat pf, int pageIndex) {
		int response = NO_SUCH_PAGE;
		Graphics2D g2 = (Graphics2D) g;
		// for faster printing, turn off double buffering
		disableDoubleBuffering(componentToBePrinted);
                // get size of document
		Dimension d = componentToBePrinted.getSize(); 
 
		double panelWidth = d.width; // width in pixels
		double panelHeight = d.height; // height in pixels
                // height of printer page
		double pageHeight = pf.getImageableHeight(); 
 
		double pageWidth = pf.getImageableWidth(); // width of printer page
		double scale = pageWidth / panelWidth;
		int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight);
		// make sure not print empty pages
		if (pageIndex >= totalNumPages) {
			response = NO_SUCH_PAGE;
		} else {
			// shift Graphic to line up with beginning 
                        // of print-imageable region
			g2.translate(pf.getImageableX(), pf.getImageableY());
			// shift Graphic to line up with beginning of next page to print
			g2.translate(0f, -pageIndex * pageHeight);
			// scale the page so the width fits...
			g2.scale(scale, scale);
                        // repaint the page for printing
			componentToBePrinted.paint(g2); 
			enableDoubleBuffering(componentToBePrinted);
			response = Printable.PAGE_EXISTS;
		}
		return response;
	}
 
	public static void disableDoubleBuffering(Component c) {
		RepaintManager currentManager =
                               RepaintManager.currentManager(c);
		currentManager.setDoubleBufferingEnabled(false);
	}
 
	public static void enableDoubleBuffering(Component c) {
		RepaintManager currentManager =
                               RepaintManager.currentManager(c);
		currentManager.setDoubleBufferingEnabled(true);
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 2 2007
Added on Feb 1 2007
2 comments
164 views