com.numdata.printer
Class PrintTools

java.lang.Object
  extended by com.numdata.printer.PrintTools

public class PrintTools
extends Object

This class provides functionality for printing.

Author:
Peter S. Heijnen

Field Summary
static double INCH_IN_MILLIMETERS
          Length of an inch expressed in millimeters.
static int LPD_PORT
          The default LPD port number.
static double POINT_IN_INCHES
          Size of a PostScript point (1/72 inch) expressed in inches.
static double POINT_IN_MILLIMETERS
          Size of a PostScript point (1/72 inch) expressed in millimeters.
 
Method Summary
static PrintService getPrintService(String printerName, DocFlavor docFlavor)
          Get PrintService for printer that has the given name and supports the given document flavor, if either is specified.
static Map<String,PrintService> getPrintServices(DocFlavor docFlavor)
          Get map of print services by (printer) name.
static PrintService printDialogForDoc(Doc doc)
          Show print dialog for the given document.
static void printDoc(Doc doc)
          Print a document.
static void printRaw(PrinterSettings settings, String documentName, byte[] documentData, boolean raw)
          Print a document by using the specified PrinterSettings.
static void printRawFileToJPS(PrintService printService, File file)
          Send raw file to print service.
static void printRawFileToJPS(String printerName, File file)
          Send raw file to print service.
static void printTable(JTable table, String header, String footer)
          Print a JTable.
static void printText(String text)
          Print a plain text document.
static BufferedImage printToImage(PageFormat pageFormat, double resolution, Printable document)
          Renders the given printable document to an image matching the given page format and resolution.
static void printToJPS(PrintService printService, Doc doc, boolean waitUntilDone)
          Print a document to the given print service.
static void printToJPS(String queueName, String documentName, Printable document, PageFormat pageFormat)
          Prints the given document to a local printer using the Java Print API.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INCH_IN_MILLIMETERS

public static final double INCH_IN_MILLIMETERS
Length of an inch expressed in millimeters.

See Also:
Constant Field Values

POINT_IN_INCHES

public static final double POINT_IN_INCHES
Size of a PostScript point (1/72 inch) expressed in inches.

See Also:
Constant Field Values

POINT_IN_MILLIMETERS

public static final double POINT_IN_MILLIMETERS
Size of a PostScript point (1/72 inch) expressed in millimeters.

See Also:
Constant Field Values

LPD_PORT

public static final int LPD_PORT
The default LPD port number.

See Also:
Constant Field Values
Method Detail

printDoc

public static void printDoc(Doc doc)
Print a document. A print dialog is displayed before the actual print job is executed.

Parameters:
doc - Document to print.

printTable

public static void printTable(JTable table,
                              String header,
                              String footer)
Print a JTable. The table is scaled to fit the page. A print dialog is displayed before the actual print job is executed.

Parameters:
table - Table to print.
header - Header text (null => none).
footer - Footer text (null => none).

printText

public static void printText(String text)
Print a plain text document. A print dialog is displayed before the actual print job is executed.

Parameters:
text - Content of text document to print.

printDialogForDoc

public static PrintService printDialogForDoc(Doc doc)
Show print dialog for the given document.

Parameters:
doc - Document to print.
Returns:
PrintService that was selected; null if dialog was cancelled.

printRaw

public static void printRaw(PrinterSettings settings,
                            String documentName,
                            byte[] documentData,
                            boolean raw)
                     throws PrinterException
Print a document by using the specified PrinterSettings.

Parameters:
settings - Printer settings to use.
documentName - Name of document to display in queue.
documentData - Document data to print.
raw - Document data is raw vs. cooked (from printer's perspective).
Throws:
PrinterException - if the document could not be printed.

printToImage

public static BufferedImage printToImage(PageFormat pageFormat,
                                         double resolution,
                                         Printable document)
                                  throws PrinterException
Renders the given printable document to an image matching the given page format and resolution. Only the imageable area of the given page format is included in the image.

Parameters:
pageFormat - Page format to be used.
resolution - Resolution of the image, in dots per inch.
document - Document to be rendered to an image.
Returns:
Image on which the document is rendered.
Throws:
PrinterException - if an error occurs while rendering the image.
IllegalArgumentException - if the document doesn't contain any pages to be rendered, or if the given resolution and/or page size are too small to render an image of the page.

printToJPS

public static void printToJPS(@Nullable
                              String queueName,
                              @Nullable
                              String documentName,
                              @NotNull
                              Printable document,
                              @Nullable
                              PageFormat pageFormat)
                       throws PrinterException
Prints the given document to a local printer using the Java Print API.

Parameters:
queueName - Name of the printer; null to use the default printer.
documentName - Name of the document.
document - Document to be printed.
pageFormat - Optional page format for document.
Throws:
PrinterException - if an error occurs while printing.

printToJPS

public static void printToJPS(PrintService printService,
                              Doc doc,
                              boolean waitUntilDone)
                       throws PrinterException
Print a document to the given print service.

Parameters:
printService - Print service to use.
doc - Document to print.
waitUntilDone - Wait until the print job is done.
Throws:
PrinterException - if the document could not be printed.

printRawFileToJPS

public static void printRawFileToJPS(@Nullable
                                     String printerName,
                                     @NotNull
                                     File file)
                              throws PrinterException
Send raw file to print service.

Parameters:
printerName - Name of printer (null to use default/any).
file - Raw file.
Throws:
PrinterException - if the document could not be printed.

printRawFileToJPS

public static void printRawFileToJPS(@NotNull
                                     PrintService printService,
                                     @NotNull
                                     File file)
                              throws PrinterException
Send raw file to print service.

Parameters:
printService - Print service to use.
file - Raw file.
Throws:
PrinterException - if the document could not be printed.

getPrintService

@NotNull
public static PrintService getPrintService(@Nullable
                                                   String printerName,
                                                   @Nullable
                                                   DocFlavor docFlavor)
                                    throws PrinterException
Get PrintService for printer that has the given name and supports the given document flavor, if either is specified.

Parameters:
printerName - Printer name (if null, use default).
docFlavor - Required document flavor (null = any).
Returns:
PrintService.
Throws:
PrinterException - if the printer service is not available.

getPrintServices

@NotNull
public static Map<String,PrintService> getPrintServices(@Nullable
                                                                DocFlavor docFlavor)
Get map of print services by (printer) name. If specified, this will only return services that support the given document flavor.

Parameters:
docFlavor - Document flavor to get service for (null = any).
Returns:
Map PrintService by (printer) name.


Copyright © 2017 Numdata BV. All rights reserved.