我有麻烦打印到标签打印机,代码在一个打印4“标签”,任何帮助(附加标签的图片)

Code Bellow打印给兄弟QL-500标签打印机.它打印到3.5“by 1.1”标签
如果有人可以帮助我更好地了解代码,这也是很棒的

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import javax.print.PrintService;



public class DYMOLabelPrintConnector implements Printable {

public static final String PRINTERNAME = "DYMO LabelWriter 400";


public static final boolean PRINTMENU = false;

public static void main(String[] args) {
PrinterJob printerJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = printerJob.defaultPage();
Paper paper = new Paper();

final double widthPaper = (1.2 * 72);
final double heightPaper = (1.5 * 72);

paper.setSize(widthPaper,heightPaper);
paper.setimageableArea(0,widthPaper,heightPaper);

pageFormat.setPaper(paper);

pageFormat.setorientation(PageFormat.LANDSCAPE);

if (PRINTMENU) {
  if (printerJob.printDialog()) {
    printerJob.setPrintable(new DYMOLabelPrintConnector(),pageFormat); 

    try {
      printerJob.print();
    } catch (PrinterException e) {
      e.printstacktrace();
    }
  }
} else {
  PrintService[] printService = PrinterJob.lookupprintServices();

  for (int i = 0; i < printService.length; i++) {
    System.out.println(printService[i].getName());

    if (printService[i].getName().compareto(PRINTERNAME) == 0) {
      try {
        printerJob.setPrintService(printService[i]);
        printerJob.setPrintable(new DYMOLabelPrintConnector(),pageFormat); 
        printerJob.print();
      } catch (PrinterException e) {
        e.printstacktrace();
      }
    }
  }
}

System.exit(0);
 }

  public String getValue(final int elementOnLabel,final int labelCounter) {
  String value = "";

switch (elementOnLabel) {
case 0:
  // what ever you want to have in this line
  value = "SetupX";

  break;

case 1:
    // what ever you want to have in this line
  value = "fiehnlab.ucd";

  break;

case 2:
    // what ever you want to have in this line
  value = "id: " + labelCounter;

  break;

case 3:
    // what ever you want to have in this line
  // Todo - add DB connection
  value = "label:" + elementOnLabel;

  break;

case 4:
    // what ever you want to have in this line
  value = DateFormat.getDateInstance(DateFormat.SHORT,Locale.US).format(new Date());

  break;

default:
  break;
}

return value;
}


public int print(Graphics graphics,PageFormat pageFormat,int pageIndex)
throws PrinterException {
System.out.println("printing page: " + pageIndex);

   if (pageIndex < getPageNumbers()) {
    Graphics2D g = (Graphics2D) graphics;

  // g.translate(pageFormat.getimageableX(),pageFormat.getimageableY());
  g.translate(20,10);

  String value = "";
  pageIndex = pageIndex + 1;

  // specific for four circular labels per page
  for (int x = 0; x < 80; x = x + 50) {
    for (int y = 0; y < 80; y = y + 36) {
      int posOnPage = 4; // Bottomright,TopRight,BottomLeft,TopLeft 

      if (x > 0) {
        posOnPage = posOnPage - 2;
      }

      if (y > 0) {
        posOnPage = posOnPage - 1;
      }

      // current counter for the label.
      int id = (posOnPage - 1) + ((pageIndex - 1) * 4);

      // setupx
      g.setFont(new Font(g.getFont().getFontName(),g.getFont().getStyle(),3));
      value = this.getValue(0,id);
      g.drawString("      " + value,x,y);

      // fiehnlab
      g.setFont(new Font(g.getFont().getFontName(),3));
      value = this.getValue(1,id);
      g.drawString("    " + value,y + 4);

      // ID
      g.setFont(new Font(g.getFont().getFontName(),Font.BOLD,7));
      value = this.getValue(2,id);
      g.drawString("" + value,y + 10);

      // label
      g.setFont(new Font(g.getFont().getFontName(),5));
      value = this.getValue(3,id);
      g.drawString(" " + value,y + 16);

      // date
      g.setFont(new Font(g.getFont().getFontName(),Font.PLAIN,3));
      value = this.getValue(4,y + 20);
    }
  }

  return PAGE_EXISTS;
} else {
  return NO_SUCH_PAGE;
}
}

public int getPageNumbers() {
return 5;
 }
 }

enter code here

这是什么打印 –

解决方法

哇,我不能告诉你我喜欢用Java打印多少东西,当它的工作,这是伟大的…

.

public class PrinterTest {

    public static void main(String[] args) {

        PrinterJob pj = PrinterJob.getPrinterJob();
        if (pj.printDialog()) {
            PageFormat pf = pj.defaultPage();
            Paper paper = pf.getPaper();    
            double width = fromCMToPPI(3.5);
            double height = fromCMToPPI(8.8);    
            paper.setSize(width,height);
            paper.setimageableArea(
                            fromCMToPPI(0.25),fromCMToPPI(0.5),width - fromCMToPPI(0.35),height - fromCMToPPI(1));                
            System.out.println("Before- " + dump(paper));    
            pf.setorientation(PageFormat.PORTRAIT);
            pf.setPaper(paper);    
            System.out.println("After- " + dump(paper));
            System.out.println("After- " + dump(pf));                
            dump(pf);    
            PageFormat validatePage = pj.validatePage(pf);
            System.out.println("Valid- " + dump(validatePage));                
            //Book book = new Book();
            //book.append(new MyPrintable(),pf);
            //pj.setPageable(book);    
            pj.setPrintable(new MyPrintable(),pf);
            try {
                pj.print();
            } catch (PrinterException ex) {
                ex.printstacktrace();
            }    
        }    
    }

    protected static double fromCMToPPI(double cm) {            
        return toPPI(cm * 0.393700787);            
    }

    protected static double toPPI(double inch) {            
        return inch * 72d;            
    }

    protected static String dump(Paper paper) {            
        StringBuilder sb = new StringBuilder(64);
        sb.append(paper.getWidth()).append("x").append(paper.getHeight())
           .append("/").append(paper.getimageableX()).append("x").
           append(paper.getimageableY()).append(" - ").append(paper
       .getimageableWidth()).append("x").append(paper.getimageableHeight());            
        return sb.toString();            
    }

    protected static String dump(PageFormat pf) {    
        Paper paper = pf.getPaper();            
        return dump(paper);    
    }

    public static class MyPrintable implements Printable {

        @Override
        public int print(Graphics graphics,int pageIndex) throws PrinterException {    
            System.out.println(pageIndex);                
            int result = NO_SUCH_PAGE;    
            if (pageIndex < 2) {                    
                Graphics2D g2d = (Graphics2D) graphics;                    
                System.out.println("[Print] " + dump(pageFormat));                    
                double width = pageFormat.getimageableWidth();
                double height = pageFormat.getimageableHeight();    
                g2d.translate((int) pageFormat.getimageableX(),(int) pageFormat.getimageableY());                    
                g2d.draw(new Rectangle2D.Double(1,1,width - 1,height - 1));                    
                FontMetrics fm = g2d.getFontMetrics();
                g2d.drawString("0x0",fm.getAscent());    
                result = PAGE_EXISTS;    
            }    
            return result;    
        }
    }
}

如果您尝试并指定纸张尺寸和页边距,我知道PrintDialog有一些不一致的情况,而且老实说这是另一天的问题.

我发布的代码能够在Dymo LabelWriter 400 Turbo上相继打印两个标签,而不会出现问题

更新
还应该提到,我认为你基本上缺少PageFormat.setPaper

更新与Barbaque BarCode

从文件示例中打印…

Barcode b = BarcodeFactory.createCode128("Hello");
b.setResolution(72);

File f = new File("mybarcode.png");
// Let the barcode image handler do the hard work
BarcodeImageHandler.savePNG(b,f);

.
.
.

public static class PrintFromFile implements Printable {

    @Override
    public int print(Graphics graphics,int pageIndex) throws PrinterException {

        int result = NO_SUCH_PAGE;
        if (pageIndex == 0) {

            graphics.translate((int)pageFormat.getimageableX(),(int)pageFormat.getimageableY());

            result = PAGE_EXISTS;

            try {

                // You may want to rescale the image to better fit the label??
                BufferedImage read = ImageIO.read(new File("mybarcode.png"));
                graphics.drawImage(read,null);

            } catch (IOException ex) {

                ex.printstacktrace();

            }

        }

        return result;

    }

}

直接打印到图形上下文

Barcode b = BarcodeFactory.createCode128("Hello");
b.setResolution(72);

.
.
.

public static class PrintToGraphics implements Printable {

    private Barcode b;

    private PrintToGraphics(Barcode b) {

        this.b = b;

    }

    @Override
    public int print(Graphics graphics,int pageIndex) throws PrinterException {

        int result = NO_SUCH_PAGE;
        if (pageIndex == 0) {

            result = PAGE_EXISTS;

            int x = (int)pageFormat.getimageableX();
            int y = (int)pageFormat.getimageableY();

            int width = (int)pageFormat.getimageableWidth();
            int height = (int)pageFormat.getimageableHeight();

            graphics.translate(x,y);
            try {
                b.draw((Graphics2D)graphics,0);
            } catch (OutputException ex) {

                ex.printstacktrace();

            }

        }

        return result;

    }

}

最后但并非最不重要的是,直接从“examples”目录下载

public class PrintingExample
{

    public static void main(String[] args)
    {
        try
        {
            Barcode b = BarcodeFactory.createCode128("Hello");
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPrintable(b);
            if (job.printDialog())
            {
                job.print();
            }
        }
        catch (Exception e)
        {
            e.printstacktrace();
        }

    }

}

以Java打印到标签打印机的更多相关文章

  1. 以Java打印到标签打印机

    我有麻烦打印到标签打印机,代码在一个打印4“标签”,任何帮助CodeBellow打印给兄弟QL-500标签打印机.它打印到3.5“by1.1”标签如果有人可以帮助我更好地了解代码,这也是很棒的这是什么打印–解决方法哇,我不能告诉你我喜欢用Java打印多少东西,当它的工作,这是伟大的…直接打印到图形上下文最后但并非最不重要的是,直接从“examples”目录下载

随机推荐

  1. 基于EJB技术的商务预订系统的开发

    用EJB结构开发的应用程序是可伸缩的、事务型的、多用户安全的。总的来说,EJB是一个组件事务监控的标准服务器端的组件模型。基于EJB技术的系统结构模型EJB结构是一个服务端组件结构,是一个层次性结构,其结构模型如图1所示。图2:商务预订系统的构架EntityBean是为了现实世界的对象建造的模型,这些对象通常是数据库的一些持久记录。

  2. Java利用POI实现导入导出Excel表格

    这篇文章主要为大家详细介绍了Java利用POI实现导入导出Excel表格,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  3. Mybatis分页插件PageHelper手写实现示例

    这篇文章主要为大家介绍了Mybatis分页插件PageHelper手写实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  4. (jsp/html)网页上嵌入播放器(常用播放器代码整理)

    网页上嵌入播放器,只要在HTML上添加以上代码就OK了,下面整理了一些常用的播放器代码,总有一款适合你,感兴趣的朋友可以参考下哈,希望对你有所帮助

  5. Java 阻塞队列BlockingQueue详解

    本文详细介绍了BlockingQueue家庭中的所有成员,包括他们各自的功能以及常见使用场景,通过实例代码介绍了Java 阻塞队列BlockingQueue的相关知识,需要的朋友可以参考下

  6. Java异常Exception详细讲解

    异常就是不正常,比如当我们身体出现了异常我们会根据身体情况选择喝开水、吃药、看病、等 异常处理方法。 java异常处理机制是我们java语言使用异常处理机制为程序提供了错误处理的能力,程序出现的错误,程序可以安全的退出,以保证程序正常的运行等

  7. Java Bean 作用域及它的几种类型介绍

    这篇文章主要介绍了Java Bean作用域及它的几种类型介绍,Spring框架作为一个管理Bean的IoC容器,那么Bean自然是Spring中的重要资源了,那Bean的作用域又是什么,接下来我们一起进入文章详细学习吧

  8. 面试突击之跨域问题的解决方案详解

    跨域问题本质是浏览器的一种保护机制,它的初衷是为了保证用户的安全,防止恶意网站窃取数据。那怎么解决这个问题呢?接下来我们一起来看

  9. Mybatis-Plus接口BaseMapper与Services使用详解

    这篇文章主要为大家介绍了Mybatis-Plus接口BaseMapper与Services使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  10. mybatis-plus雪花算法增强idworker的实现

    今天聊聊在mybatis-plus中引入分布式ID生成框架idworker,进一步增强实现生成分布式唯一ID,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部