Java 打印
public static void printJPG() throws FileNotFoundException, PrintException, InterruptedException {
//image file
String filename = "abc.jpg";
// PrintRequestAttributeSet實例。
// 這用來彈出顯示的對話框,并在對話框消失之前返回用戶所作的任何更改。
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);// 用戶可選用的PrintService實例數組。
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); // 默認的PrintService
/*
* 為用戶提供一個選擇 PrintService(打印機)的對話框。
* gc - 用于選擇屏幕。null 意味著主屏幕或默認屏幕。
* x - 對話框在屏幕坐標中的位置,包括邊框
* y - 對話框在屏幕坐標中的位置,包括邊框
* services - 可瀏覽的服務,必須不為 null。
* defaultService - 要顯示的初始 PrintService。
* flavor - 要打印的 flavor,或者為 null。
* attributes - 輸入時為應用程序最初提供的首選項。這不能為 null,但可以為空。輸出時為反映用戶所作的更改的屬性。
*/
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
if (service != null) {
DocPrintJob job = service.createPrintJob(); // 創建打印任務
FileInputStream fis = new FileInputStream(filename);
DocAttributeSet das = new HashDocAttributeSet();
/*
* 定義要打印的文檔,SimpleDoc(,,)里有三個參數:
* ·Object 代表要打印的內容
* ·DocFlavor的一個實例描述數據類型
* ·可選的DocAttributeSet 包含打印時的屬性
*/
Doc doc = new SimpleDoc(fis, flavor, das);
/*
* 啟動打印 job.print( , )
* doc - 要打印的文檔。如果必須是一個 flavor,則此 PrintJob 必須支持它。
* attributes - 應用到此 PrintJob 的作業屬性。如果此參數為 null,則使用默認屬性。
*/
job.print(doc, pras);
Thread.sleep(10000);
}
}
//image file
String filename = "abc.jpg";
// PrintRequestAttributeSet實例。
// 這用來彈出顯示的對話框,并在對話框消失之前返回用戶所作的任何更改。
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);// 用戶可選用的PrintService實例數組。
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); // 默認的PrintService
/*
* 為用戶提供一個選擇 PrintService(打印機)的對話框。
* gc - 用于選擇屏幕。null 意味著主屏幕或默認屏幕。
* x - 對話框在屏幕坐標中的位置,包括邊框
* y - 對話框在屏幕坐標中的位置,包括邊框
* services - 可瀏覽的服務,必須不為 null。
* defaultService - 要顯示的初始 PrintService。
* flavor - 要打印的 flavor,或者為 null。
* attributes - 輸入時為應用程序最初提供的首選項。這不能為 null,但可以為空。輸出時為反映用戶所作的更改的屬性。
*/
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
if (service != null) {
DocPrintJob job = service.createPrintJob(); // 創建打印任務
FileInputStream fis = new FileInputStream(filename);
DocAttributeSet das = new HashDocAttributeSet();
/*
* 定義要打印的文檔,SimpleDoc(,,)里有三個參數:
* ·Object 代表要打印的內容
* ·DocFlavor的一個實例描述數據類型
* ·可選的DocAttributeSet 包含打印時的屬性
*/
Doc doc = new SimpleDoc(fis, flavor, das);
/*
* 啟動打印 job.print( , )
* doc - 要打印的文檔。如果必須是一個 flavor,則此 PrintJob 必須支持它。
* attributes - 應用到此 PrintJob 的作業屬性。如果此參數為 null,則使用默認屬性。
*/
job.print(doc, pras);
Thread.sleep(10000);
}
}
public static void printPDF() throws Exception{
//pdf file
String filename = "abc.pdf";
// PrintRequestAttributeSet實例。
// 這用來彈出顯示的對話框,并在對話框消失之前返回用戶所作的任何更改。
PrintService printService[] = PrinterJob.lookupPrintServices();// 用戶可選用的PrintService實例數組。
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); // 默認的PrintService
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
/*
* 為用戶提供一個選擇 PrintService(打印機)的對話框。
* gc - 用于選擇屏幕。null 意味著主屏幕或默認屏幕。
* x - 對話框在屏幕坐標中的位置,包括邊框
* y - 對話框在屏幕坐標中的位置,包括邊框
* services - 可瀏覽的服務,必須不為 null。
* defaultService - 要顯示的初始 PrintService。
* flavor - 要打印的 flavor,或者為 null。
* attributes - 輸入時為應用程序最初提供的首選項。這不能為 null,但可以為空。輸出時為反映用戶所作的更改的屬性。
*/
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, null, pras);
if (service != null) {
//創建PDFDocument
PDDocument document = PDDocument.load( filename );
//創建打印Job
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setJobName(new File(filename).getName());
printJob.setPrintService(service);
//開始打印
document.silentPrint( printJob );
Thread.sleep(10000);
}
}
//pdf file
String filename = "abc.pdf";
// PrintRequestAttributeSet實例。
// 這用來彈出顯示的對話框,并在對話框消失之前返回用戶所作的任何更改。
PrintService printService[] = PrinterJob.lookupPrintServices();// 用戶可選用的PrintService實例數組。
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); // 默認的PrintService
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
/*
* 為用戶提供一個選擇 PrintService(打印機)的對話框。
* gc - 用于選擇屏幕。null 意味著主屏幕或默認屏幕。
* x - 對話框在屏幕坐標中的位置,包括邊框
* y - 對話框在屏幕坐標中的位置,包括邊框
* services - 可瀏覽的服務,必須不為 null。
* defaultService - 要顯示的初始 PrintService。
* flavor - 要打印的 flavor,或者為 null。
* attributes - 輸入時為應用程序最初提供的首選項。這不能為 null,但可以為空。輸出時為反映用戶所作的更改的屬性。
*/
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, null, pras);
if (service != null) {
//創建PDFDocument
PDDocument document = PDDocument.load( filename );
//創建打印Job
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setJobName(new File(filename).getName());
printJob.setPrintService(service);
//開始打印
document.silentPrint( printJob );
Thread.sleep(10000);
}
}
public static void printPDF() throws Exception{
// pdf file
String filename = "abc.pdf";
// PrintRequestAttributeSet實例。
// 這用來彈出顯示的對話框,并在對話框消失之前返回用戶所作的任何更改。
PrintService printService[] = PrinterJob.lookupPrintServices();// 用戶可選用的PrintService實例數組。
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); // 默認的PrintService
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
/*
* 為用戶提供一個選擇 PrintService(打印機)的對話框。 gc - 用于選擇屏幕。null 意味著主屏幕或默認屏幕。 x -
* 對話框在屏幕坐標中的位置,包括邊框 y - 對話框在屏幕坐標中的位置,包括邊框 services - 可瀏覽的服務,必須不為 null。
* defaultService - 要顯示的初始 PrintService。 flavor - 要打印的 flavor,或者為 null。
* attributes - 輸入時為應用程序最初提供的首選項。這不能為 null,但可以為空。輸出時為反映用戶所作的更改的屬性。
*/
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, null, pras);
if (service != null) {
PdfDecoder pdfDecoder = new PdfDecoder(true); // Set to true as I don't want to render it to screen
try {
pdfDecoder.openPdfFile(filename);
pdfDecoder.setPageParameters(1, 1); //values scaling (1=100%). page number
//setup print job and objects
PrinterJob printerJob = PrinterJob.getPrinterJob();
PageFormat pf = printerJob.defaultPage();
printerJob.setPrintService(service);
/*
* default page size
*/
Paper paper = new Paper();
paper.setSize(595, 842); // in pixels. For A4 paper (21 cm x 29.7 cm) at 72 dpi (java default)
paper.setImageableArea(0, 0, 595, 842);
pf.setPaper(paper);
printerJob.setPageable(pdfDecoder);
//setup default values to padd into JPS
PageRanges fullPageRange = new PageRanges(1, pdfDecoder.getPageCount());
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(fullPageRange);
pdfDecoder.setPageFormat(pf);
pdfDecoder.setPagePrintRange(fullPageRange);
printerJob.setCopies(1);
printerJob.print();
} finally {
// cleanup temporary files (%TMP%/jpedal)
pdfDecoder.closePdfFile();
}
}
}
// pdf file
String filename = "abc.pdf";
// PrintRequestAttributeSet實例。
// 這用來彈出顯示的對話框,并在對話框消失之前返回用戶所作的任何更改。
PrintService printService[] = PrinterJob.lookupPrintServices();// 用戶可選用的PrintService實例數組。
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); // 默認的PrintService
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
/*
* 為用戶提供一個選擇 PrintService(打印機)的對話框。 gc - 用于選擇屏幕。null 意味著主屏幕或默認屏幕。 x -
* 對話框在屏幕坐標中的位置,包括邊框 y - 對話框在屏幕坐標中的位置,包括邊框 services - 可瀏覽的服務,必須不為 null。
* defaultService - 要顯示的初始 PrintService。 flavor - 要打印的 flavor,或者為 null。
* attributes - 輸入時為應用程序最初提供的首選項。這不能為 null,但可以為空。輸出時為反映用戶所作的更改的屬性。
*/
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, null, pras);
if (service != null) {
PdfDecoder pdfDecoder = new PdfDecoder(true); // Set to true as I don't want to render it to screen
try {
pdfDecoder.openPdfFile(filename);
pdfDecoder.setPageParameters(1, 1); //values scaling (1=100%). page number
//setup print job and objects
PrinterJob printerJob = PrinterJob.getPrinterJob();
PageFormat pf = printerJob.defaultPage();
printerJob.setPrintService(service);
/*
* default page size
*/
Paper paper = new Paper();
paper.setSize(595, 842); // in pixels. For A4 paper (21 cm x 29.7 cm) at 72 dpi (java default)
paper.setImageableArea(0, 0, 595, 842);
pf.setPaper(paper);
printerJob.setPageable(pdfDecoder);
//setup default values to padd into JPS
PageRanges fullPageRange = new PageRanges(1, pdfDecoder.getPageCount());
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(fullPageRange);
pdfDecoder.setPageFormat(pf);
pdfDecoder.setPagePrintRange(fullPageRange);
printerJob.setCopies(1);
printerJob.print();
} finally {
// cleanup temporary files (%TMP%/jpedal)
pdfDecoder.closePdfFile();
}
}
}
posted on 2011-12-13 14:50 MingIsMe 閱讀(163) 評論(0) 編輯 收藏 所屬分類: 06 J2EE