Printing gif using java on 4x6 paper

What is the best way in Java to print a gif specified as byte[]or ByteArrayInputStreamon 4x6 inch paper?

It:

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 MediaSize is not a PrintRequestAttribute attribute. It should be almost the same as in the javax.print package. Description

+3
source share
1 answer

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 used a Japanese postcard as the size of the media.

+1
source

Source: https://habr.com/ru/post/1715377/


All Articles