Top Description Fields Constructors Methods
org.eclipse.swt.printing

public final Class Printer

extends Device
Class Inheritance
Imports
org.eclipse.swt.*, org.eclipse.swt.graphics.*, org.eclipse.swt.internal.*, org.eclipse.swt.internal.cairo.*, org.eclipse.swt.internal.gtk.*, org.eclipse.swt.internal.gtk3.*, org.eclipse.swt.internal.gtk4.*

Instances of this class are used to print to a printer. Applications create a GC on a printer using new GC(printer) and then draw on the printer GC using the usual graphics calls.

A Printer object may be constructed by providing a PrinterData object which identifies the printer. A PrintDialog presents a print dialog to the user and returns an initialized instance of PrinterData. Alternatively, calling new Printer() will construct a printer object for the user's default printer.

Application code must explicitly invoke the Printer.dispose() method to release the operating system resources managed by each instance when those instances are no longer required.

See Also
PrinterData, PrintDialog, Printing snippets, Sample code and further information

Field Summary

Modifier and TypeField and Description
pack-priv long
pack-priv PrinterData
pack-priv static boolean
pack-priv static int
pack-priv static PrinterData
pack-priv static long
pack-priv static final String
pack-priv static final String
pack-priv boolean
isGCCreated

whether or not a GC was created for this printer

pack-priv long
pack-priv long
pack-priv static PrinterData[]
pack-priv long
pack-priv long
pack-priv static byte[]
pack-priv static int
pack-priv long
Inherited from org.eclipse.swt.graphics.Device:
CHANGE_SCALEFACTORCurrentDeviceDEBUGDeviceFinderxDisplay

Constructor Summary

AccessConstructor and Description
public
Printer()

Constructs a new printer representing the default printer.

public
Printer(PrinterData
the printer data for the specified printer, or null to use the default printer
data
)

Constructs a new printer given a PrinterData object representing the desired printer.

Method Summary

Modifier and TypeMethod and Description
public void
cancelJob()

Cancels a print job in progress.

pack-priv static DeviceData
public Rectangle

Returns:

a rectangle, relative to the client area's coordinates, that is the client area expanded by the printer's trim (or minimum margins)
computeTrim
(int
the x coordinate of the client area
x
,
int
the y coordinate of the client area
y
,
int
the width of the client area
width
,
int
the height of the client area
height
)

Given a client area (as described by the arguments), returns a rectangle, relative to the client area's coordinates, that is the client area expanded by the printer's trim (or minimum margins).

protected void
create(DeviceData
the device data
deviceData
)

Overrides org.eclipse.swt.graphics.Device.create.

Creates the printer handle.
protected void
destroy()

Overrides org.eclipse.swt.graphics.Device.destroy.

Destroys the printer handle.
public void
endJob()

Ends the current print job.

public void
endPage()

Ends the current page.

public Rectangle

Returns:

the bounding rectangle
getBounds
()

Overrides org.eclipse.swt.graphics.Device.getBounds.

Returns a rectangle describing the receiver's size and location.
public Rectangle

Returns:

the client area
getClientArea
()

Overrides org.eclipse.swt.graphics.Device.getClientArea.

Returns a rectangle which describes the area of the receiver which is capable of displaying data.
public static PrinterData

Returns:

the default printer data or null
getDefaultPrinterData
()

Returns a PrinterData object representing the default printer or null if there is no default printer.

public Point

Returns:

the horizontal and vertical DPI
getDPI
()

Overrides org.eclipse.swt.graphics.Device.getDPI.

Returns a point whose x coordinate is the horizontal dots per inch of the printer, and whose y coordinate is the vertical dots per inch of the printer.
pack-priv Point
public PrinterData

Returns:

a PrinterData object describing the receiver
getPrinterData
()

Returns a PrinterData object representing the target printer for this print job.

public static PrinterData[]

Returns:

an array of PrinterData objects representing the available printers
getPrinterList
()

Returns an array of PrinterData objects representing all available printers.

pack-priv static void
pack-priv static long
pack-priv static long
GtkPrinterFunc_Default(long printer, long user_data)

pack-priv static long
GtkPrinterFunc_FindNamedPrinter(long printer, long user_data)

pack-priv static long
GtkPrinterFunc_List(long printer, long user_data)

protected void
init()

Overrides org.eclipse.swt.graphics.Device.init.

Initializes any internal resources needed by the device.
public void
internal_dispose_GC(long
the platform specific GC handle
hDC
,
GCData
the platform specific GC data
data
)

Implements abstract org.eclipse.swt.graphics.Device.internal_dispose_GC.

Implements org.eclipse.swt.graphics.Drawable.internal_dispose_GC.

Invokes platform specific functionality to dispose a GC handle.
public long

Returns:

the platform specific GC handle
internal_new_GC
(GCData
the platform specific GC data
data
)

Implements abstract org.eclipse.swt.graphics.Device.internal_new_GC.

Implements org.eclipse.swt.graphics.Drawable.internal_new_GC.

Invokes platform specific functionality to allocate a new GC handle.
public boolean
isAutoScalable()

Overrides default org.eclipse.swt.graphics.Drawable.isAutoScalable.

Returns true iff coordinates can be auto-scaled on this drawable and false if not.
pack-priv static PrinterData
pack-priv static void
restore(byte[] data, long settings, long page_setup)

pack-priv static boolean
pack-priv static byte[]
restoreBytes(String key, boolean nullTerminate)

pack-priv static double
pack-priv static int
public boolean

Returns:

true if the job started successfully and false otherwise.
startJob
(String
the name of the print job to start
jobName
)

Starts a print job and returns true if the job started successfully and false otherwise.

public boolean

Returns:

true if the page started successfully and false otherwise.
startPage
()

Starts a page and returns true if the page started successfully and false otherwise.

pack-priv static byte[]
Inherited from org.eclipse.swt.graphics.Device:
checkDevicedisposegetDepthgetDeviceDatagetDeviceZoomgetFontListgetSystemColorgetSystemFontgetWarningsisDisposedisTrackingloadFontreleasesetTrackingsetWarnings

Field Detail

cairoback to summary
pack-priv long cairo
databack to summary
pack-priv PrinterData data
disablePrintingback to summary
pack-priv static boolean disablePrinting
endback to summary
pack-priv static int end
findDataback to summary
pack-priv static PrinterData findData
findPrinterback to summary
pack-priv static long findPrinter
GTK_FILE_BACKENDback to summary
pack-priv static final String GTK_FILE_BACKEND
GTK_LPR_BACKENDback to summary
pack-priv static final String GTK_LPR_BACKEND
isGCCreatedback to summary
pack-priv boolean isGCCreated

whether or not a GC was created for this printer

pageSetupback to summary
pack-priv long pageSetup
printerback to summary
pack-priv long printer
printerListback to summary
pack-priv static PrinterData[] printerList
printJobback to summary
pack-priv long printJob
settingsback to summary
pack-priv long settings
settingsDataback to summary
pack-priv static byte[] settingsData
startback to summary
pack-priv static int start
surfaceback to summary
pack-priv long surface

Constructor Detail

Printerback to summary
public Printer()

Constructs a new printer representing the default printer.

Note

You must dispose the printer when it is no longer required.

Exceptions
SWTError:
  • ERROR_NO_HANDLES - if there is no valid default printer
See Also
Device#dispose
Printerback to summary
public Printer(PrinterData data)

Constructs a new printer given a PrinterData object representing the desired printer. If the argument is null, then the default printer will be used.

Note

You must dispose the printer when it is no longer required.

Parameters
data:PrinterData

the printer data for the specified printer, or null to use the default printer

Exceptions
IllegalArgumentException:
  • ERROR_INVALID_ARGUMENT - if the specified printer data does not represent a valid printer
SWTError:
  • ERROR_NO_HANDLES - if there are no valid printers
See Also
Device#dispose

Method Detail

cancelJobback to summary
public void cancelJob()

Cancels a print job in progress.

Exceptions
SWTException:
  • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
checkNullback to summary
pack-priv static DeviceData checkNull(PrinterData data)
computeTrimback to summary
public Rectangle computeTrim(int x, int y, int width, int height)

Given a client area (as described by the arguments), returns a rectangle, relative to the client area's coordinates, that is the client area expanded by the printer's trim (or minimum margins).

Most printers have a minimum margin on each edge of the paper where the printer device is unable to print. This margin is known as the "trim." This method can be used to calculate the printer's minimum margins by passing in a client area of 0, 0, 0, 0 and then using the resulting x and y coordinates (which will be <= 0) to determine the minimum margins for the top and left edges of the paper, and the resulting width and height (offset by the resulting x and y) to determine the minimum margins for the bottom and right edges of the paper, as follows:

  • The left trim width is -x pixels
  • The top trim height is -y pixels
  • The right trim width is (x + width) pixels
  • The bottom trim height is (y + height) pixels
Parameters
x:int

the x coordinate of the client area

y:int

the y coordinate of the client area

width:int

the width of the client area

height:int

the height of the client area

Returns:Rectangle

a rectangle, relative to the client area's coordinates, that is the client area expanded by the printer's trim (or minimum margins)

Exceptions
SWTException:
  • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
See Also
getBounds, getClientArea
createback to summary
protected void create(DeviceData deviceData)

Overrides org.eclipse.swt.graphics.Device.create.

Creates the printer handle. This method is called internally by the instance creation mechanism of the Device class.

Parameters
deviceData:DeviceData

the device data

Annotations
@Override
destroyback to summary
protected void destroy()

Overrides org.eclipse.swt.graphics.Device.destroy.

Destroys the printer handle. This method is called internally by the dispose mechanism of the Device class.

Annotations
@Override
endJobback to summary
public void endJob()

Ends the current print job.

Exceptions
SWTException:
  • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
See Also
startJob, startPage, endPage
endPageback to summary
public void endPage()

Ends the current page.

Exceptions
SWTException:
  • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
See Also
startPage, startJob, endJob
getBoundsback to summary
public Rectangle getBounds()

Overrides org.eclipse.swt.graphics.Device.getBounds.

Returns a rectangle describing the receiver's size and location.

For a printer, this is the size of the physical page, in pixels.

Returns:Rectangle

the bounding rectangle

Annotations
@Override
Exceptions
SWTException:
  • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
See Also
getClientArea, computeTrim
getClientAreaback to summary
public Rectangle getClientArea()

Overrides org.eclipse.swt.graphics.Device.getClientArea.

Returns a rectangle which describes the area of the receiver which is capable of displaying data.

For a printer, this is the size of the printable area of the page, in pixels.

Returns:Rectangle

the client area

Annotations
@Override
Exceptions
SWTException:
  • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
See Also
getBounds, computeTrim
getDefaultPrinterDataback to summary
public static PrinterData getDefaultPrinterData()

Returns a PrinterData object representing the default printer or null if there is no default printer.

Returns:PrinterData

the default printer data or null

Since
2.1
getDPIback to summary
public Point getDPI()

Overrides org.eclipse.swt.graphics.Device.getDPI.

Returns a point whose x coordinate is the horizontal dots per inch of the printer, and whose y coordinate is the vertical dots per inch of the printer.

Returns:Point

the horizontal and vertical DPI

Annotations
@Override
Exceptions
SWTException:
  • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
getIndependentDPIback to summary
pack-priv Point getIndependentDPI()
getPrinterDataback to summary
public PrinterData getPrinterData()

Returns a PrinterData object representing the target printer for this print job.

Returns:PrinterData

a PrinterData object describing the receiver

getPrinterListback to summary
public static PrinterData[] getPrinterList()

Returns an array of PrinterData objects representing all available printers. If there are no printers, the array will be empty.

Returns:PrinterData[]

an array of PrinterData objects representing the available printers

gtk_initback to summary
pack-priv static void gtk_init()
gtkPrinterFromPrinterDataback to summary
pack-priv static long gtkPrinterFromPrinterData(PrinterData data)
GtkPrinterFunc_Defaultback to summary
pack-priv static long GtkPrinterFunc_Default(long printer, long user_data)
GtkPrinterFunc_FindNamedPrinterback to summary
pack-priv static long GtkPrinterFunc_FindNamedPrinter(long printer, long user_data)
GtkPrinterFunc_Listback to summary
pack-priv static long GtkPrinterFunc_List(long printer, long user_data)
initback to summary
protected void init()

Overrides org.eclipse.swt.graphics.Device.init.

Initializes any internal resources needed by the device.

This method is called after create.

If subclasses reimplement this method, they must call the super implementation.

Annotations
@Override
See Also
create
internal_dispose_GCback to summary
public void internal_dispose_GC(long hDC, GCData data)

Implements abstract org.eclipse.swt.graphics.Device.internal_dispose_GC.

Implements org.eclipse.swt.graphics.Drawable.internal_dispose_GC.

Invokes platform specific functionality to dispose a GC handle.

Important Note

This method is not part of the public API for Printer. It is marked public only so that it can be shared within the packages provided by SWT. It is not available on all platforms, and should never be called from application code.

Parameters
hDC:long

the platform specific GC handle

data:GCData

the platform specific GC data

Annotations
@Override
internal_new_GCback to summary
public long internal_new_GC(GCData data)

Implements abstract org.eclipse.swt.graphics.Device.internal_new_GC.

Implements org.eclipse.swt.graphics.Drawable.internal_new_GC.

Invokes platform specific functionality to allocate a new GC handle.

Important Note

This method is not part of the public API for Printer. It is marked public only so that it can be shared within the packages provided by SWT. It is not available on all platforms, and should never be called from application code.

Parameters
data:GCData

the platform specific GC data

Returns:long

the platform specific GC handle

Annotations
@Override
isAutoScalableback to summary
public boolean isAutoScalable()

Overrides default org.eclipse.swt.graphics.Drawable.isAutoScalable.

Doc from org.eclipse.swt.graphics.Drawable.isAutoScalable.

Returns true iff coordinates can be auto-scaled on this drawable and false if not. E.g. a GC method should not auto-scale the bounds of a figure drawn on a Printer device, but it may have to auto-scale when drawing on a high-DPI Display monitor.

Returns:boolean

true if auto-scaling is enabled for this drawable

Annotations
@Override
printerDataFromGtkPrinterback to summary
pack-priv static PrinterData printerDataFromGtkPrinter(long printer)
restoreback to summary
pack-priv static void restore(byte[] data, long settings, long page_setup)
restoreBooleanback to summary
pack-priv static boolean restoreBoolean(String key)
restoreBytesback to summary
pack-priv static byte[] restoreBytes(String key, boolean nullTerminate)
restoreDoubleback to summary
pack-priv static double restoreDouble(String key)
restoreIntback to summary
pack-priv static int restoreInt(String key)
startJobback to summary
public boolean startJob(String jobName)

Starts a print job and returns true if the job started successfully and false otherwise.

This must be the first method called to initiate a print job, followed by any number of startPage/endPage calls, followed by endJob. Calling startPage, endPage, or endJob before startJob will result in undefined behavior.

Parameters
jobName:String

the name of the print job to start

Returns:boolean

true if the job started successfully and false otherwise.

Exceptions
SWTException:
  • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
See Also
startPage, endPage, endJob
startPageback to summary
public boolean startPage()

Starts a page and returns true if the page started successfully and false otherwise.

After calling startJob, this method may be called any number of times along with a matching endPage.

Returns:boolean

true if the page started successfully and false otherwise.

Exceptions
SWTException:
  • ERROR_DEVICE_DISPOSED - if the receiver has been disposed
See Also
endPage, startJob, endJob
uriFromFilenameback to summary
pack-priv static byte[] uriFromFilename(String filename)