从Java打印到Dymo标签打印机

时间:2018-08-09 08:32:16

标签: java printing dymo

我试图在Dymo LabelWriter 450上打印标签,但是很麻烦。

我现在要解决的问题是,我什至似乎无法填满整个标签,当使用Dymo软件进行打印时,似乎没有获得约0.6mm的剩余边距。 / p>

理想情况下,我会使用SDK,但找不到Dymo Java SDK。

这是我正在寻找的结果 Result I'm looking for

这是我到目前为止所得到的 current label

我修改了this answer中的一些代码以得到此结果。

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

如果我错过了一个可用的SDK,我很想知道在哪里可以找到它。否则,我该如何消除这个左边距,以便实际上可以贴上标签上的所有内容?

谢谢!

0 个答案:

没有答案