|
You can find all available printers on your machine regardless of the current PrintRequestAttributeSet (including on the Macintosh) using the following method:
String[] printerNames = printer.getAllPrinterNames();
You can determine the default printer on the user's machine using the method:
String defaultPrinter = printer.getDefaultPrinterName();
Once you've determined the name of the printer you wish to use, J2Printer14 lets you specify this printer programmatically using the method:
printer.setPrinter(printerName);
The value of printerName should be one of those returned by the method getPrinterNames, i.e., it should be one of the available printers given the current PrintRequestAttributeSet. If name does not match one of the available printers, the default printer is used.
Specifying PrinterJob
As an alternative to using the J2Printer14 methods to set up your PrintRequestAttributeSet and printer choice, you can use the Java PrinterJob and PrintRequestAttributeSet classes directly to define a desired java.awt.print.PrinterJob instance yourself, then pass this to J2Printer14 using the following method:
printer.setPrinterJob(printerJob);
The setPrinterJob method is also supported by the J2Printer class but in that case only the PrinterJob attributes supported prior to JDK 1.4 are recognized, e.g., number of copies and print job name, and not the printer name or the extended print request attributes supported under JDK 1.4 or later.
Printing to a Postscript file
The J2Printer14 class provides methods that allow you to "print" your document to a Postscript file. A method available for this purpose is:
printToPS(java.lang.String file): Utility method for printing the document to a Postscript (.ps) file, suitable for dumping to a Postscript printer (for example, on Windows use the DOS command copy /b filename.ps lpt1: or a program like PrintFile), viewing or printing on any device with a program such as Ghostscript, or converting to PDF using programs such as Adobe Acrobat Distiller or the PS2PDF (part of Ghostscript), see below.
The Postscript printing capability was first introduced by Java under JDK 1.4, so the printToPS method requires JDK 1.4 or later. The file specification is relative to the working directory of the application. If subfolders are specified, all must already exist, they will not be created for you. If the file name is missing, the file name out.ps will be used. If the extension is missing, the file extension .ps is added.
J2Printer14 also has convenience printToPS methods available for writing the Postscript output to an in-memory byte array or to a provided OutputStream.
As of the J2PrinterWorks 4.2 release, the call to printToPS honors the setSeparatePrintThread(boolean) method, so that by default this method executes in a background thread and fires print progress events. Call setSeparatePrintThread(false) in advance of calling this method if you don't want this method to return until printing completes.
Printing to a PDF file
Beginning with J2PrinterWorks 4.0, you can now call the J2Printer method printToPDF(java.lang.String file) to save your document to a PDF file. This feature does not require JDK 1.4 or J2Printer14, but it does require the presence of the free, third-party component iText.jar (see "printToPDF" in the J2Printer documentation).
As of the J2PrinterWorks 4.2 release, the call to printToPDF honors the setSeparatePrintThread(boolean) method, so that by default this method executes in a background thread and fires print progress events. Call setSeparatePrintThread(false) in advance of calling this method if you don't want this method to return until printing completes.
Alternatively, it is possible use the J2Printer14 method printToPS above to create a Postscript file and then invoke third-party programs such as Adobe Acrobat Distiller or PS2PDF (part of Ghostscript) to convert this file to a PDF file. The following code shows how to do this. It assumes you have downloaded and installed Ghostscript. The default install location on Windows is c:\gs\gs8.14 (the following code assumes this path has no embedded spaces):
static private void printToPDF(String file) {
String ghostscriptDir = "c:\\gs\\gs8.14"; // change this if necessary
printer.printToPS("J2PWoutTemp.ps"); // printer = your J2Printer14 instance
String libDir = ghostscriptDir + "\\lib";
String binDir = ghostscriptDir + "\\bin";
String fullPathPS = "\"" + new File("J2PWoutTemp.ps").getAbsolutePath() + "\"";
String fullPathPDF = "\"" + new File(file).getAbsolutePath() + "\"";
String cmd = libDir + "\\ps2pdf.bat " + fullPathPS + " " + fullPathPDF;
String envp = "path=.;" + libDir + ";" + binDir;
try {
Runtime.getRuntime().exec(cmd, new String[] {envp}, new File(libDir)).waitFor();
} catch (Exception e) { e.printStackTrace(); }
File f = new File("J2PWoutTemp.ps");
if (f.exists()) f.delete();
}
As a convenience on Windows systems, J2PrinterWorks also supports methods for printing a .pdf file to the default printer and displaying a .pdf file, see description under J2Printer "printToPDF".
«
|