2009-08-17 87 views
3

什麼是Java打印爲byte[]ByteArrayInputStream鑑於紙張大小爲4×6英寸的GIF的最好辦法打印GIF紙

此:

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 
aset.add(new MediaSize(4, 6, Size2DSyntax.INCH)); 
aset.add(new Copies(1)); 
PrintService[] pservices = 
PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, aset); 

DocPrintJob printJob = pservices[0].createPrintJob(); 
Doc doc = new SimpleDoc(sap.getGraphicImageBytes(), DocFlavor.INPUT_STREAM.GIF, null); 
printJob.print(doc, aset); 

不起作用,因爲的MediaSize是不是PrintRequestAttribute。這應該是幾乎一樣Package javax.print Description

回答

1

我找到了一種方法來解決我的問題

PageFormat format = new PageFormat(); 
format.setOrientation(PageFormat.REVERSE_LANDSCAPE); 

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 
aset.add(OrientationRequested.REVERSE_LANDSCAPE); 
aset.add(MediaSizeName.JAPANESE_POSTCARD); 

PrinterJob printerJob = PrinterJob.getPrinterJob(); 
printerJob.setPrintable(new ImagePrintable(sap.getGraphicImage())); 
printerJob.defaultPage(format); 
printerJob.print(aset); 

訣竅是使用日本明信片作爲媒體大小。

+0

投票這一個。這讓我很快就解決了打印到CUPS(Linux,Mac)中的4x6熱敏標籤打印機的一些默認紙張尺寸問題,謝謝。 – tresf 2015-09-02 04:46:55