public class DocumentPrinter extends Object implements Serializable, Printable, PropertyChangeListener
print()
methods to programmatically print a document. Use the
showPrintDialog()
to enable the end-user to print the document
using an interactive dialog.
import java.io.IOException; import com.gnostice.core.XDocException; import com.gnostice.documents.controls.swing.printer.DocumentPrinter; public class DocumentPrintingDemo { static { XDocSetup.activate(); } public static void main(String[] args) { try { DocumentPrinter prn = new DocumentPrinter(); if (prn.getAvailablePrinterNames().length > 0) { // Load a document prn.loadDocument("unicode.docx",""); // Print to default printer prn.print(); // Unload IO resources used for the document prn.closeDocument(); } else { System.out.println("No printer found."); } } catch (XDocException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
import java.io.IOException; import com.gnostice.core.XDocException; import com.gnostice.documents.controls.swing.printer.DocumentPrinter; public class DocumentPrintingDemo { static { XDocSetup.activate(); } public static void main(String[] args) { try { DocumentPrinter prn = new DocumentPrinter(); if (prn.getAvailablePrinterNames().length > 0) { // Load a document prn.loadDocument("unicode.docx",""); // Show print dialog to the end-user prn.showPrintDialog(); // Unload IO resources used for the document prn.closeDocument(); } else { System.out.println("No printer found."); } } catch (XDocException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
NO_SUCH_PAGE, PAGE_EXISTS
Constructor and Description |
---|
DocumentPrinter()
Creates a new instance of this class.
|
Modifier and Type | Method and Description |
---|---|
void |
addPrintListener(DocumentPrintListener printListener)
Adds specified user-class instance to the list of listeners that needs to
be notified of printing events.
|
void |
closeDocument()
Closes the current document.
|
String[] |
getAvailablePrinterNames()
Returns names of printers.
|
String |
getDefaultPrinterName()
Returns name of the default printer.
|
PrinterPreferences |
getPreferences()
Returns settings of the current printer.
|
DocumentPrintListener[] |
getPrintListeners()
Returns user-class instances that need to be notified of printing events.
|
DocumentPrintSettings |
getPrintSettings()
Returns print settings.
|
boolean |
isDocumentLoaded()
Returns whether a document has been loaded for printing.
|
void |
loadDocument(InputStream inputStream,
String password)
Loads a document from specified stream after decrypting it with specified password.
|
void |
loadDocument(String fileName,
String password)
Loads a document from the specified file after decrypting it with specified
password.
|
void |
print()
Prints the current document to the current printer.
|
int |
print(Graphics g,
PageFormat pageFormat,
int printJobIndex) |
void |
print(PageRangeSettings pageRangeSettings,
int numOfCopies)
Prints the document with specified page range settings for a specified
number of times.
|
void |
print(String printerName,
PageRangeSettings pageRangeSettings,
int numOfCopies)
Prints the document specified number of times to specified printer with
specified page range settings.
|
void |
propertyChange(PropertyChangeEvent arg0) |
void |
removePrintListener(DocumentPrintListener printListener)
Removes specified user-class instance from the list of listeners that need
to be notified of printing events.
|
void |
showCrossPlatformPrintDialog()
Displays the cross-platform printing dialog box.
|
void |
showNativePrintDialog()
Displays the printing dialog native to the JVM host.
|
void |
showPrintDialog()
Displays the print dialog.
|
void |
showPrintDialog(Dialog owner,
boolean modal)
Displays the printing dialog box with the specified dialog as the parent
container.
|
void |
showPrintDialog(Frame owner,
boolean modal)
Displays the printing dialog box with the specified frame as the parent
container.
|
public DocumentPrinter() throws XDocException
XDocException
- if an error occurs with the instancepublic void loadDocument(String fileName, String password) throws FormatNotSupportedException, IncorrectPasswordException, IOException, XDocException
fileName
- pathname of the filepassword
- password with which the document needs to be decryptedFormatNotSupportedException
- if the document format is not supportedIncorrectPasswordException
- if the password is not correctIOException
- if an I/O error occursXDocException
- if an error occurs in processing the documentpublic void loadDocument(InputStream inputStream, String password) throws FormatNotSupportedException, IncorrectPasswordException, XDocException, IOException
inputStream
- stream from which a document needs to be loadedpassword
- password with which the document needs to be decryptedFormatNotSupportedException
- if the document format is not supportedIncorrectPasswordException
- if the password is not correctXDocException
- if an error occurs in processing the documentIOException
- if an I/O error occurspublic boolean isDocumentLoaded()
public void print() throws XDocException, IOException, PrinterException
XDocException
- if an error occurs in processing the documentIOException
- if an I/O error occursPrinterException
- if an error occurs with the printerpublic int print(Graphics g, PageFormat pageFormat, int printJobIndex) throws PrinterException
print
in interface Printable
PrinterException
public void print(PageRangeSettings pageRangeSettings, int numOfCopies) throws XDocException, PrinterException, IOException
pageRangeSettings
- page range settingsnumOfCopies
- number of copiesXDocException
- if an error occurs in processing the documentPrinterException
- if an error occurs with the printerIOException
- if an I/O error occurspublic void print(String printerName, PageRangeSettings pageRangeSettings, int numOfCopies) throws XDocException, PrinterException, IOException
printerName
- name of the printerpageRangeSettings
- page range settingsnumOfCopies
- number of copiesXDocException
- if an error occurs in processing the documentPrinterException
- if an error occurs with the printerIOException
- if an I/O error occurspublic void closeDocument()
public String[] getAvailablePrinterNames()
public String getDefaultPrinterName()
public DocumentPrintSettings getPrintSettings()
public void addPrintListener(DocumentPrintListener printListener)
printListener
- user-class instance that needs to be addedpublic void removePrintListener(DocumentPrintListener printListener)
printListener
- user-class instance that needs to be removedpublic DocumentPrintListener[] getPrintListeners()
public void showCrossPlatformPrintDialog() throws XDocException, IOException, PrinterException
XDocException
- if the component encounters an errorIOException
- if an I/O error occursPrinterException
- if an error occurs in printingpublic void showNativePrintDialog() throws XDocException, IOException, PrinterException
XDocException
- if the component encounters an errorIOException
- if an I/O error occursPrinterException
- if an error occurs in printingpublic void showPrintDialog() throws XDocException
XDocException
- if the component encounters an errorpublic void showPrintDialog(Frame owner, boolean modal) throws XDocException
owner
- the parent containermodal
- whether the dialog box is a model windowXDocException
- if the component encounters an errorpublic void showPrintDialog(Dialog owner, boolean modal) throws XDocException
owner
- parent containermodal
- whether the dialog box is a model windowXDocException
- if the component encounters an errorpublic PrinterPreferences getPreferences()
public void propertyChange(PropertyChangeEvent arg0)
propertyChange
in interface PropertyChangeListener