I'm trying to print a label on my Dymo LabelWriter 450, but am having a hard time.
The issue I am dealing with now is that I can't even seem to fill the entire label, there seems to be a left margin of about 0.6mm that I don't get when I print using the Dymo software.
Ideally I would use an SDK, but I am unable to find a Dymo Java SDK.
This is the result I'm looking for
I modified some code from this answer to get to this result.
public class PrinterTest {
public static void main(String[] args) {
PrinterJob printerJob = PrinterJob.getPrinterJob();
if (printerJob.printDialog()) {
PageFormat pageFormat = printerJob.defaultPage();
Paper paper = pageFormat.getPaper();
double width = fromCMToPPI(1.9);
double height = fromCMToPPI(4.5);
double horizontalMargin = fromCMToPPI(0.25);
double verticalMargin = fromCMToPPI(0.1);
paper.setSize(width, height);
paper.setImageableArea(
horizontalMargin,
verticalMargin,
width,
height);
pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE);
pageFormat.setPaper(paper);
printerJob.setPrintable(new MyPrintable(), pageFormat);
try {
printerJob.print();
} catch (PrinterException ex) {
ex.printStackTrace();
}
}
}
private static double fromCMToPPI(double cm) {
return toPPI(cm * 0.393700787);
}
private static double toPPI(double inch) {
return inch * 72d;
}
public static class MyPrintable implements Printable {
@Override
public int print(Graphics graphics, PageFormat pageFormat,
int pageIndex) {
System.out.println(pageIndex);
int result = NO_SUCH_PAGE;
if (pageIndex < 1) {
Graphics2D g2d = (Graphics2D) graphics;
double width = pageFormat.getImageableWidth();
double height = pageFormat.getImageableHeight();
double x = pageFormat.getImageableX();
double y = pageFormat.getImageableY();
System.out.println("x = " + x);
System.out.println("y = " + y);
g2d.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY());
g2d.draw(new Rectangle2D.Double(x, y, width - x, height - y));
FontMetrics fm = g2d.getFontMetrics();
g2d.drawString("AxB", Math.round(x), fm.getAscent());
result = PAGE_EXISTS;
}
return result;
}
}
}
If there is an SDK available that I missed, I would love to know where to find it. Otherwise, how can I get rid of this left margin so that I can actually fit everything on the label?
Thanks!