public class PdfPrinter extends Object implements Serializable, Printable
load the document
,select a printer
, andprint
command.import com.gnostice.pdfone.PdfPrinter; public class PdfPrinter_Example { public static void main(String[] args) { try { // Create a printer object PdfPrinter prn = new PdfPrinter(); // Load a document prn.loadDocument("sample_doc.pdf"); // Get a list of printers String sPrinters[] = prn.getAvailablePrinterNames(); // If there are any printers if (sPrinters.length > 0) { // Select the first printer prn.setSelectedPrinterName(sPrinters[0]); // Print a print range for just once prn.print("1,3-4", 1); } // Close document prn.closeDocument(); } catch (Exception e) { System.out.println("Sorry, an error occurred - " + e.getMessage()); } } }
IMPORTANT:
Avoid using measurement units from PdfMeasurement
.
Instead, use the measurement unit constants from this class - MU_INCHES
,
MU_MM
and MU_POINTS
.
This printer component contains a built-in print dialog box, which can be used to obtain print settings and print command from the end-user.
PdfPrintPageHandler
,
Serialized FormModifier and Type | Field and Description |
---|---|
static int |
MU_INCHES
Constant for page measurement unit in inches.
|
static int |
MU_MM
Constant for page measurement unit in millimetres.
|
static int |
MU_POINTS
Constant for page measurement unit in points.
|
static int |
Orientation_LANDSCAPE
Constant for landscape printer page orientation.
|
static int |
Orientation_PORTRAIT
Constant for portrait printer page orientation.
|
static int |
SCALE_FIT_TO_PRINTER_MARGINS
Constant for specifying that document pages need to be
resized to exactly fit within printer page margins.
|
static int |
SCALE_NONE
Constant for specifying that document pages need to be printed
as is, without any resizing.
|
static int |
SCALE_REDUCE_TO_PRINTER_MARGINS
Constant for specifying that document pages need to down-sized
to be accommodated within the margins of printer page margins
but only when the document page is bigger than the printer
page.
|
static int |
SUBSET_EVEN_ONLY
Constant for specifying only even pages as the print range
subset.
|
static int |
SUBSET_NONE
Constant for specifying all pages as the print range subset.
|
static int |
SUBSET_ODD_ONLY
Constant for specifying only odd pages as the print range
subset.
|
NO_SUCH_PAGE, PAGE_EXISTS
Constructor and Description |
---|
PdfPrinter() |
PdfPrinter(PdfDocument d)
Deprecated.
Instead use a load method or the
setDocument(PdfDocument) method.
Constructs a PdfPrinter object with
specified PdfDocument object. |
Modifier and Type | Method and Description |
---|---|
void |
autoRotateandCenter(boolean autoRotateandCenter)
Specifies whether rotated pages in the document need to be
rotated and centered when they are printed.
|
void |
closeDocument()
Closes the
loaded document . |
String[] |
getAvailablePrinterNames()
Returns names of all printers available for printing.
|
int |
getCopies()
Returns number of copies for the print job.
|
String |
getDefaultPrinterName()
Returns name of default printer to which this printer object to
will print.
|
PdfDocument |
getDocument()
Returns the
PdfDocument object that has been set
up with this printer object. |
Finishings |
getFinishings()
Returns finishing operations that are set to be performed for
the current print job.
|
int |
getOrientation()
Returns default printer page orientation.
|
double[] |
getPageDimension()
Returns dimensions of default page size of the printer.
|
double[] |
getPageDimension(int inMeasurementUnit)
Returns dimensions of default page size of the printer, in
specified measurement unit.
|
double[] |
getPageMargins()
Returns page margins in default measurement units.
|
double[] |
getPageMargins(int inMeasurementUnit)
Returns page margins in specified measurement unit.
|
String |
getPageRange()
Returns page range of the next print job.
|
int |
getPageScale()
Returns how pages will be resized for the next print job.
|
int |
getPageSize()
Returns size of the pages for the next print job.
|
PdfPrintHandler |
getPdfPrintHandler() |
Chromaticity |
getPrintChromaticity() |
String |
getPrintJobName()
Returns name specified for the next print job.
|
MediaTray |
getPrintMediaTray()
Returns current paper source or tray.
|
PdfPrintPageHandler |
getPrintPageHandler() |
SheetCollate |
getPrintSheetCollate()
Returns how print output is collated when multiple copies of
the document is printed.
|
Sides |
getPrintSides()
Returns how pages from a print job are imposed on the sides of
the output media (paper).
|
String |
getPrintToFileName()
Returns pathname of the file to which printer has been set to
print.
|
PdfRenderErrorHandler |
getRenderErrorHandler() |
String |
getSelectedPrinterName()
Returns name of the selected printer for the next print job.
|
int |
getSubset()
Returns print range subset of the next print job.
|
boolean |
isAutoRotateandCenter()
Returns whether rotated pages in the document aer set to be
rotated and centered when they are printed.
|
static boolean |
isFontProcessorLibraryLoaded()
Deprecated.
no replacement
|
boolean |
isPrintToFile()
Specifies whether printer is set to print to a file.
|
boolean |
isReverse()
Returns whether all pages are set for being printed in reverse
order.
|
void |
loadDocument(byte[] byteArray)
Loads document contained in a specified byte array for
printing.
|
void |
loadDocument(byte[] byteArray,
String password)
Loads encrypted document in specfied array with specified
password.
|
void |
loadDocument(File inFile)
Loads document specified by a File object.
|
void |
loadDocument(FileInputStream fileInputStream)
Loads document specified by a FileInputStream object.
|
void |
loadDocument(FileInputStream fileInputStream,
String password)
Loads document specified by a FileInputStream object for
printing.
|
void |
loadDocument(File inFile,
String password)
Loads document specified by a File object with specified
password.
|
void |
loadDocument(String inFilePath)
Loads document in specified path for printing.
|
void |
loadDocument(String inFilePath,
String password)
Loads encrypted document in specified path with specified
password.
|
static void |
loadFontProcessorLibrary(String libDirPath)
Deprecated.
no replacement
|
int |
print(Graphics graphics,
PageFormat pageFormat,
int pageIndex) |
void |
print(String pageRange,
int numOfCopies)
Prints pages in specified page range for specified times.
|
void |
print(String selPrinterName,
String pageRange,
int numOfCopies)
Prints pages in specified page range for specified times using
specified printer.
|
void |
printToFile(boolean printToFile)
Specifies whether printer needs to print to a file.
|
void |
setCopies(int copies)
Specifies number of copies that need to be made for the print
job.
|
void |
setCustomPageDimension(double width,
double height,
int measurementUnit)
Sets custom dimensions for the default page of the printer.
|
void |
setDocument(PdfDocument d)
Sets up a
PdfDocument object with the printer
object. |
void |
setFinishings(Finishings finishings)
Specifies finishing operations for the current print job.
|
void |
setOrientation(int orientation)
Sets default printer page orientation.
|
void |
setPageMargins(double[] pageMargins,
int inUnits)
Sets specified page margins in specified measurement units.
|
void |
setPageRange(String pageRange)
Specifies page range for the next print job.
|
void |
setPageScale(int pageScale)
Specifies how pages need to be resized for the next print job.
|
void |
setPageSize(int paperSize)
Specifies size of printer pages that need to be selected for
the next print job.
|
void |
setPaperSize(int paperSize)
Deprecated.
Instead, use
setPageSize() . |
void |
setPdfPrintHandler(PdfPrintHandler pdfPrintHandler) |
void |
setPrintChromaticity(Chromaticity chromaticity) |
void |
setPrintJobName(String printJobName)
Specifies name for the next print job.
|
void |
setPrintMediaTray(MediaTray mediaTray)
Set specified media tray as current paper source or tray.
|
void |
setPrintPageHandler(PdfPrintPageHandler pdfPrintPageHandler)
Specifies an instance of user class that implements
PdfPrintPageHandler . |
void |
setPrintSheetCollate(SheetCollate sheetCollate)
Specifies how print output needs to collated when multiple
copies of the document is printed.
|
void |
setPrintSides(Sides sides)
Specifies how pages from a print job needs to be imposed on the
sides of the output media (paper).
|
void |
setPrintToFileName(String printToFileName)
Specified pathname of the file to which the printer has been
set to print.
|
void |
setRenderErrorHandler(PdfRenderErrorHandler pdfRenderErrorHandler)
Specifies user class instance that implements the
PdfRenderErrorHandler.onRenderError(Object, Throwable)
event handler. |
void |
setReverse(boolean reverse)
Specifies whether document pages need to be printed in reverse
order.
|
void |
setSelectedPrinterName(String selectedPrinterName)
Specifies name of the selected printer for the next print job.
|
void |
setSubset(int subset)
Specifies print range subset for the next print job.
|
void |
showCrossPlatformPrintDialog()
Displays a cross-platform print dialog box.
|
void |
showNativePrintDialog()
Displays a print dialog box supplied by the native operating
system.
|
void |
showPrintDialog()
Displays this component's print dialog box.
|
void |
showPrintDialog(Dialog owner,
boolean modal)
Displays this component's print dialog box as a modal or
non-modal window and with specified dialog as its owner.
|
void |
showPrintDialog(Frame owner,
boolean modal)
Displays this component's print dialog box as a modal or
non-modal window and with specified frame as its owner.
|
public static final int SUBSET_NONE
public static final int SUBSET_EVEN_ONLY
public static final int SUBSET_ODD_ONLY
public static final int SCALE_NONE
public static final int SCALE_FIT_TO_PRINTER_MARGINS
public static final int SCALE_REDUCE_TO_PRINTER_MARGINS
public static final int Orientation_PORTRAIT
setOrientation(int)
,
getOrientation()
,
Constant Field Valuespublic static final int Orientation_LANDSCAPE
setOrientation(int)
,
getOrientation()
,
Constant Field Valuespublic static final int MU_MM
public static final int MU_INCHES
public static final int MU_POINTS
public PdfPrinter()
public PdfPrinter(PdfDocument d) throws PdfException
setDocument(PdfDocument)
method.
Constructs a PdfPrinter
object with
specified PdfDocument
object.PdfException
public void loadDocument(String inFilePath) throws IOException, PdfException
inFilePath
- pathname of the documentIOException
- if an I/O error occurs.PdfException
- if an illegal argument is supplied.public void loadDocument(String inFilePath, String password) throws IOException, PdfException
inFilePath
- pathname of the document that needs to be printedpassword
- password with which the document needs to be openedIOException
- if an I/O error occurs.PdfException
- if an illegal argument is supplied.public void loadDocument(File inFile) throws IOException, PdfException
inFile
- document that needs to be printedIOException
- if an I/O error occurs.PdfException
- if an illegal argument is supplied.public void loadDocument(File inFile, String password) throws IOException, PdfException
inFile
- the document that needs to be printedpassword
- password with which the document needs to be openedIOException
- if an I/O error occurs.PdfException
- if an illegal argument is supplied.public void loadDocument(FileInputStream fileInputStream) throws IOException, PdfException
fileInputStream
- document that needs to be printedIOException
- if an I/O error occurs.PdfException
- if an illegal argument is supplied.public void loadDocument(FileInputStream fileInputStream, String password) throws IOException, PdfException
fileInputStream
- document that needs to be printedpassword
- password with which the documents to be openedIOException
- if an I/O error occurs.PdfException
- if an illegal argument is supplied.public void loadDocument(byte[] byteArray) throws IOException, PdfException
byteArray
- document that needs to be printedIOException
- if an I/O error occurs.PdfException
- if an illegal argument is supplied.public void loadDocument(byte[] byteArray, String password) throws IOException, PdfException
byteArray
- document that needs to be printedpassword
- password with which the document needs to be openedIOException
- if an I/O error occurs.PdfException
- if an illegal argument is supplied.public void setDocument(PdfDocument d)
PdfDocument
object with the printer
object. When a document is loaded by PdfDocument
object, the printer object automatically becomes ready for
printing the loaded document. To get notified when
PdfDocument
object loads a PDF document, user
classes need to implement PdfDocumentChangeHandler
.d
- PdfDocument
object with which this
printer component needs to be set upgetDocument()
public PdfDocument getDocument()
PdfDocument
object that has been set
up with this printer object.PdfDocument
object that has been set
up with this printer objectsetDocument(PdfDocument)
public void print(String pageRange, int numOfCopies) throws IOException, PdfException, PrinterException
pageRange
- pages that need to be printednumOfCopies
- number of times the pages need to be printedIOException
- if an I/O error occurs.PdfException
- if an illegal argument is supplied.PrinterException
- if a printing error occurs.public void print(String selPrinterName, String pageRange, int numOfCopies) throws IOException, PdfException, PrinterException
selPrinterName
- printer with which the pages need to be printedpageRange
- pages that need to be printednumOfCopies
- number of times the pages need to be printedIOException
- if an I/O error occurs.PdfException
- if an illegal argument is supplied.PrinterException
- if a printing error occurs.public void showNativePrintDialog() throws PdfException, IOException, PrinterException
PdfException
- if an illegal argument is supplied.IOException
- if an I/O error occurs.PrinterException
- an exceptional condition has occurred in the print
system.public void showCrossPlatformPrintDialog() throws PdfException, IOException, PrinterException
PdfException
- if an illegal argument is supplied.IOException
- if an I/O error occurs.PrinterException
- an exceptional condition has occurred in the print
system.public void showPrintDialog() throws PdfException
PdfException
- if an illegal argument is supplied.public void showPrintDialog(Frame owner, boolean modal) throws PdfException
owner
- owner frame of the displayed dialog boxmodal
- whether the displayed dialog box needs to be a modal
windowPdfException
- if an illegal argument is supplied.public void showPrintDialog(Dialog owner, boolean modal) throws PdfException
owner
- owner dialog of the displayed dialog boxmodal
- whether the displayed dialog box needs to be a modal
windowPdfException
- if an illegal argument is supplied.public void setPrintJobName(String printJobName)
printJobName
- name for the next print jobgetPrintJobName()
public String getPrintJobName()
setPrintJobName(String)
public void setSelectedPrinterName(String selectedPrinterName) throws PdfException
selectedPrinterName
- name of the selected printer for the next print jobPdfException
- if print services are not available.getSelectedPrinterName()
public String getSelectedPrinterName()
setSelectedPrinterName(String)
public void setPaperSize(int paperSize)
setPageSize()
.paperSize
- size of the pages for the next print jobgetPageSize()
public void setPageSize(int paperSize)
paperSize
is set to
PdfPageSize.CUSTOM
, then set custom page size by
calling setCustomPageDimension(double, double, int)
.paperSize
- size of the pagegetPageSize()
public void setCustomPageDimension(double width, double height, int measurementUnit) throws PdfException
default page size has been set
to PdfPageSize.CUSTOM
. If this method is not called,
then default page size (width x height) will be set to 612 x 792
points .width
- width of the default page of the printerheight
- height of the default page of the printermeasurementUnit
- constant specifying the measurement unit with which
the width and height of the default page of the
printer needs to be interpretedPdfException
- if an illegal argument is supplied.public void setPageMargins(double[] pageMargins, int inUnits) throws PdfException
pageMargins
- array containing left, top, right and bottom marginsinUnits
- measurement units with which the margins need to be
interpretedPdfException
- if an illegal argument is supplied.getPageMargins()
,
getPageMargins(int)
public double[] getPageMargins()
getPageMargins(int)
,
setPageMargins(double[], int)
public double[] getPageMargins(int inMeasurementUnit) throws PdfException
inMeasurementUnit
- measurement unit in which the margins need to be
returnedPdfException
- if an illegal argument is supplied.PdfMeasurement
,
getPageMargins()
,
setPageMargins(double[], int)
public int getPageSize()
setPageSize(int)
public void setPageRange(String pageRange)
pageRange
- pages that need to be printedgetPageRange()
public String getPageRange()
setPageRange(String)
public String[] getAvailablePrinterNames()
public String getDefaultPrinterName()
public void setCopies(int copies)
copies
- number of copiesgetCopies()
public int getCopies()
setCopies(int)
public void setSubset(int subset)
subset
- constant specifying the print range subsetgetSubset()
public int getSubset()
setSubset(int)
public void setPageScale(int pageScale)
pageScale
- constant specififying page scalinggetPageScale()
public int getPageScale()
setPageScale(int)
public void autoRotateandCenter(boolean autoRotateandCenter)
autoRotateandCenter
- true
for auto-rotate and center;
false
for otherwise.isAutoRotateandCenter()
public boolean isAutoRotateandCenter()
true
if set for auto-rotate and center;
false
if otherwise.autoRotateandCenter(boolean)
public void setReverse(boolean reverse)
reverse
- true
if pages need to printed in
reverse order; false
if otherwise.isReverse()
public boolean isReverse()
true
if pages will be printed in
reverse; false
if otherwise.setReverse(boolean)
public void setOrientation(int orientation)
orientation
- constant specifying printer page orientationOrientation_LANDSCAPE
,
Orientation_PORTRAIT
,
getOrientation()
public int getOrientation()
Orientation_LANDSCAPE
,
Orientation_PORTRAIT
,
setOrientation(int)
public void setPrintToFileName(String printToFileName) throws PdfException
printToFileName
- pathname of the file to which the printer needs to
printPdfException
- if printToFile(boolean)
has been set to
false
public String getPrintToFileName() throws PdfException
PdfException
- if printToFile(boolean)
has been set to
false
public void printToFile(boolean printToFile)
printToFile
- whether the printer should print to a filesetPrintToFileName(String)
,
getPrintToFileName()
,
isPrintToFile()
public boolean isPrintToFile()
public double[] getPageDimension()
default measurement unit
getPageDimension(int)
public double[] getPageDimension(int inMeasurementUnit) throws PdfException
inMeasurementUnit
- measurement unit in which the dimensions need to be
returned ( MU_INCHES
, MU_MM
or
MU_POINTS
)PdfException
- if an illegal argument is supplied.public void setPrintMediaTray(MediaTray mediaTray)
mediaTray
- paper sourcr or tray which needs to be used for
subsequent print jobsgetPrintMediaTray()
public MediaTray getPrintMediaTray()
setPrintMediaTray(MediaTray)
public void setPrintChromaticity(Chromaticity chromaticity)
public Chromaticity getPrintChromaticity()
public void setPrintSides(Sides sides)
sides
- printing attribute that specifies how pages from a
print job needs to be imposed on the sides of the
output media (paper)getPrintSides()
public Sides getPrintSides()
setPrintSides(Sides)
public void setPrintSheetCollate(SheetCollate sheetCollate)
sheetCollate
- printing attribute that specifies how print output
needs to collated when multiple copies of the
document is printedgetPrintSheetCollate()
public SheetCollate getPrintSheetCollate()
setPrintSides(Sides)
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException
print
in interface Printable
PrinterException
public void setPrintPageHandler(PdfPrintPageHandler pdfPrintPageHandler)
PdfPrintPageHandler
. Set an new instance of the user
class so that PdfDocument objects in the user class can call
the PdfPrintPageHandler
events.pdfPrintPageHandler
- instance of user class that implements
PdfPrintPageHandler
public PdfPrintPageHandler getPrintPageHandler()
public void setPdfPrintHandler(PdfPrintHandler pdfPrintHandler)
public PdfPrintHandler getPdfPrintHandler()
public void closeDocument() throws IOException, PdfException
loaded document
.IOException
- if an I/O error occurs.PdfException
- if an illegal argument is supplied.public static boolean isFontProcessorLibraryLoaded()
public static void loadFontProcessorLibrary(String libDirPath) throws PdfException
PdfException
public void setRenderErrorHandler(PdfRenderErrorHandler pdfRenderErrorHandler)
PdfRenderErrorHandler.onRenderError(Object, Throwable)
event handler.pdfRenderErrorHandler
- user class event handlerpublic PdfRenderErrorHandler getRenderErrorHandler()
public void setFinishings(Finishings finishings)
finishings
- finishing operations that need to be performed on
the printed documentgetFinishings()
public Finishings getFinishings()
setFinishings(Finishings)