2014-03-18 18 views
0

我試圖打印我的Jpanel。我大部分工作。現在我正在嘗試定製一些諸如邊距之類的東西。將Paper設置爲PageFormatter不會更新PageFormatter

這裏是我的代碼

PrinterJob pjob = PrinterJob.getPrinterJob(); 

//* 
PageFormat pf0 = pjob.defaultPage(); 
Paper p = pf0.getPaper(); 

//hardcode the page imageable area for testing. The sizes are valid ones i pulled from calling printerJob.getDialog(attr)...) 
p.setImageableArea(28, 28, 556, 734); 
pf0.setPaper(p); 

//set the attributes for the formatter 
PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet(); 
attr_set.add(Fidelity.FIDELITY_TRUE); 
attr_set.add(PrintQuality.HIGH); 
attr_set.add(PrintQuality.HIGH); 
attr_set.add(OrientationRequested.LANDSCAPE); 

//set the printable area 
int width = Math.round(MediaSize.NA.LETTER.getX(MediaSize.MM)); 
int height = Math.round(MediaSize.NA.LETTER.getY(MediaSize.MM)); 
attr_set.add(new MediaPrintableArea(10, 10, width - 20, height - 20, MediaPrintableArea.MM)); 

//update the page formatter 
pf0 = pjob.getPageFormat(attr_set); 

即使我打電話pf0.setPaper(p)紙對象pf0(頁面格式)不會更新。這裏是我的調試器顯示的值沒有改變的屏幕。

enter image description here

什麼給?

+0

有幾件事情,首先,您要設置兩次可成像。首先通過'Paper',然後通過'MediaPrintableArea'。 'PrintJob'將根據許多因素對「PageFormat」進行更改,包括打印機的功能...... – MadProgrammer

回答

2

好的,我已經玩過這個了。所以用你的例子我得到...

// Paper.setImageableArea 
28 pixels = 0.99 cm (x) 
28 pixels = 0.99 cm (y) 
556 pixels = 19.653 cm (width) 
734 pixels = 25.945 cm (height) 

// PrintRequestAttributeSet 
29.528 pixels = 1 cm (x) 
29.528 pixels = 1 cm (y) 
578.74 pixels = 19.6 cm (width) 
764.764 pixels = 25.9 cm (height) 

// Page format AFTER getting it from the PrintJob 
72 pixels = 2.545 cm (x) 
72 pixels = 2.545 cm (y) 
697.918 pixels = 24.669 cm (width) 
451.332 pixels = 15.953 cm (height) 

這顯然不是你的期望。

接下來,我添加attr_set.add(MediaSizeName.NA_LETTER);PrintRequestAttributeSet

// Page format AFTER getting it from the PrintJob 
22.082 pixels = 0.781 cm (x) 
72 pixels = 2.545 cm (y) 
697.918 pixels = 24.669 cm (width) 
451.332 pixels = 15.953 cm (height) 

仍然沒有你期待什麼?

現在,如果我用pf0 = pjob.pageDialog(attr_set);,而不是pf0 = pjob.getPageFormat(attr_set);

// Page format AFTER getting it from the PrintJob 
30 pixels = 1.06 cm (x) 
28 pixels = 0.99 cm (y) 
734 pixels = 25.945 cm (width) 
556 pixels = 19.653 cm (height) 

這幾乎是你設置的開始(注意,寬度和高度已被交換,因爲頁面我s在風景)...

所以...錯誤...並歡迎來到爲什麼??!?美妙的世界!

一些挖後,我發現MediaPrintableArea是被忽略,因爲service.isAttributeValueSupported(orientReq, null, attributes)調用其中servicePrintService的一些情況。這可能與默認打印機有關......?

相關問題