So as I am developing an application using java I want the functionality to print an 950x250 png image with an Epson TM-C3500. The labels I use are from the 90x20 mm size.
Printing using another windows application fits perfectly on the label but if I do it with my application I will get very weird results with prints on multiple labels and so on.
I tried a lot of things I found on this site but didn't manage to find a solution. Does anyone have some experience with this or knows how to do this? I think I could probably scale my png but I have no clue how.
my current code:
PrintService[] factories = PrinterJob.lookupPrintServices();
AttributeSet aset = new HashAttributeSet();
aset.add(new PrinterName("EPSON TM-C3500 Ver2", null));
PrintService[] pservices =
PrintServiceLookup.lookupPrintServices(null, aset);
PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setPrintService(pservices[0]);
PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
attributeSet.add(OrientationRequested.LANDSCAPE);
attributeSet.add(new MediaPrintableArea(0,0,90,20, MediaPrintableArea.MM));
BufferedImage image = ImageIO.read(new File("C:\\Users\\Simon\\Downloads\\Distribib-master\\testprinter\\src\\com\\company\\label (1).png"));
printerJob.setPrintable(new Printable() {
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex !=0) {
return NO_SUCH_PAGE;
}
graphics.drawImage(image,0,0, image.getWidth(),image.getHeight(),null);
return PAGE_EXISTS;
}
});
try {
printerJob.print(attributeSet);
} catch (PrinterException e1) {
e1.printStackTrace();
}
I tried numerous things. Like translating the image to an appropriate size, selecting supported MediaSize by getting it through "getSupportedAttributeValues", trying to change pageFormat, ...
But these things were always just trying out because I found the documentation on printing png's very confusing.
As I was trying a lot of things like searching for different mediaSizes, changing driver settings, using docPrintJob instead, ... I was not able to find a proper working solution. I figured out to just use the command line tool from irfanView and a proper ini settings file to print it directly out as a proper label. I just called the following command in java which worked perfectly.
Runtime.getRuntime().exec("C:\Programme\IrfanView\i_view32.exe C:\FileToPrint.png /ini="C:\" /print");
http://www.etcwiki.org/index.php/IrfanView_Command_Line_Options