Print gif using java on a 4x6" paper

你说的曾经没有我的故事 提交于 2019-12-23 21:26:49

问题


What is the best way in Java to print a gif given as byte[] or ByteArrayInputStream on a paper with a size of 4x6 inches?

This:

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);

does not work because the MediaSize is not a PrintRequestAttribute. This should be almost the same as in Package javax.print Description


回答1:


I found a way to solve my problem

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);

The trick was using japanese postcard as media size.



来源:https://stackoverflow.com/questions/1290937/print-gif-using-java-on-a-4x6-paper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!