TOC PREV NEXT INDEX




Printing


Printing a PDF document with ICEpdf is a highly configurable task that allows users to print using a wide range of Java technologies. To aid developers in printing, the package com.icesoft.pdf.ri.common contains the PrintHelper class that implements Java 2 Printable and Pageable interfaces. The source code for this class is available for users who want to gain a greater understanding of the printing process or modify the printing behavior.

JDK 1.2 Printing

The SwingController found in the package com.icesoft.pdf.ri.common constructs the PrintHelper class which can be used to print PDF documents using Java 1.2. The PrinterHelper class can also be used without the SwingController class if desired, as shown in the following code example.

Document document = new Document();
 
document.setFile("/usr/home/testfile.pdf");
 
ViewModel viewModel = new ViewModel();
 
PrintHelper printHelper = new PrintHelper(viewModel, document.getPageTree());
 
printHelper.print(viewModel.getPrinterJob(),
 
    0, document.getNumberOfPages() - 1,
 
    1, // default number of copies.
 
    true, // shrink to printable area
 
    false, // show page setup
 
    false, // show print dialog
 
    null);
 
document.dispose();
 

 
JDK 1.4 Printing

The PrinterHelper class can also be used with Java Printer Services available in JDK 1.4. The printing API introduced in JDK 1.4 allows for far greater flexibility than previous JDKs in detecting printers and their capabilities. The following code example demonstrates how a specific printer can be used with Java Printer Services with no user interaction:

// Create a new attribute set and add a key for the printer
 
// we want to print to.
 
AttributeSet aset = new HashAttributeSet();
 
aset.add(new PrinterName("HP LaserJet 4050 Series PCL", null));
 

 
// Do a look up to try and find our LaserJet printer
 
services = PrintServiceLookup.lookupPrintServices(
 
	DocFlavor.SERVICE_FORMATTED.PAGEABLE,
 
	aset);
 

 
// If the LaserJet could not be found then try and find
 
// a default printer which can use the Pageable interface
 
if (services.length == 0)
 
	services = PrintServiceLookup.lookupPrintServices(
 
		DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
 

 
// Finally we can print using the first printer in the
 
// services list which will either be our specified LaserJet
 
// or the default printer on the user's operating system.
 
if (services.length > 0){
 
	printerJob.setPrintService(services[0]);
 

 
	// print the document without any user interaction, no
 
	// dialogs.
 
	printHelper.print(viewModel.getPrinterJob(), 0,
 
		document.getNumberOfPages() - 1,
 
		1, // default number of copies.
 
		true, // shrink to printable area
 
		false, // show page setup
 
		false, // show print dialog
 
		pageFormat);
 
}
 
document.dispose();
 


Copyright 2005-2007. ICEsoft Technologies, Inc.
http://www.icesoft.com

TOC PREV NEXT INDEX