确定打印机是物理还是虚拟的便携式方法 [英] Portable way to determining of printer is physical or virtual

查看:18
本文介绍了确定打印机是物理还是虚拟的便携式方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站需要直接到打印机的功能,能够区分物理打印机和虚拟打印机(文件).

I need direct-to-printer functionality for my website, with the ability to distinguish a physical printer from a virtual printer (file).

Coupons.com 通过必须由用户安装的本机二进制文件具有此功能.我宁愿避免这种情况.

Coupons.com has this functionality via a native binary which must be installed by the user. I'd prefer to avoid that.

SmartSource.com 通过 Java 小程序实现:

SmartSource.com does it via Java applet:

有人知道这是怎么做的吗?我仔细研究了 Java API,除了查看名称(这似乎容易被误认)外,没有看到任何可以让您确定物理与虚拟的东西.能够用 Java 来做这件事会很好,因为我已经知道如何编写 Java 小程序.否则,有没有办法在 Flash 或 Silverlight 中做到这一点?

Does anybody know how this is done? I dug through that Java APIs a bit, and don't see anything that would let you determine physical vs virtual, except looking at the name (that seems prone to misidentification). It would be nice to be able to do it in Java, because I already know how to write Java applets. Failing that, is there a way to do this in Flash or Silverlight?

提前致谢.

Jason Sperske 获得了当之无愧的赏金,他制定了一个优雅的解决方案.感谢分享想法的人,以及实际调查 SmartSource.com 解决方案的人(如 Adrian).

Well deserved bounty awarded to Jason Sperske who worked out an elegant solution. Thanks to those of you who shared ideas, as well as those who actually investigated SmartSource.com's solution (like Adrian).

推荐答案

好的,这是我目前发现的(无论如何这不是一个详尽的测试,但尝试解决它是一个有趣的问题).通过查看 PrinterJob 类中的 validatePage() 方法的工作方式,似乎可能会有一些帮助.似乎如果打印机作业是虚拟的,那么任何设置页面 ImageableArea 的尝试将始终返回一个与默认页面 ImageableArea 完全相同的值,而尝试将相同的值设置为真实打印机将返回稍小的值(以考虑打印机机械师持有的纸张边缘.解决此问题的方法是,如果您在调用 validate 之前询问打印机的默认特性,您会得到一个乐观的结果,如果您将其与经过验证的响应进行比较,您可以执行以下操作如果测试很简单.我为此编写了一些代码,这些代码似乎适用于我桌面上的图像打印机和真实打印机(同样,这不是详尽无遗,但可以作为起点)

OK here is what I've found so far (this is not an exhaustive test by any means, but it has been a fun problem to try and tackle). it seems that there might be some help by looking at how the validatePage() method in the PrinterJob class works. It seems that if a printer job is virtual than any attempt to set a page's ImageableArea will always return a value exactly equal to the default pages ImageableArea while an attempt to to the same to a real printer will return slightly smaller values (to account for the edges of the paper being held by the printer mechanics. What helps for this problem is that if you just ask a printer for it's default characteristics before calling validate you get an optimistic result, and if you compare this to a validated response you can do a simple if test. I've written some code for this that seems to work with the image printers and real printers I have on my desktop (again this isn't exhaustive, but it could work as a starting point)

import java.awt.print.*;

import javax.print.PrintService;
import javax.print.attribute.Attribute;

public class DetectFilePrinter {

  public static void main(String[] args) {
    PrinterJob job = PrinterJob.getPrinterJob();
    PrintService printer = job.getPrintService();
    System.out.println("Printer Name:"+printer.getName());

    System.out.println(printer.toString());
    PageFormat page = job.defaultPage();
    double default_width = page.getWidth();
    double default_height = page.getHeight();

    Paper paper = new Paper();
    paper.setImageableArea(0, 0, Double.MAX_VALUE, Double.MAX_VALUE);
    page.setPaper(paper);

    PageFormat fixed_page = job.validatePage(page);

    double fixed_width = fixed_page.getImageableWidth();
    double fixed_height = fixed_page.getImageableHeight();

    //So far all of my tested "image printers" return the same 
    //height and width after calling validatePage()
    if(default_height == fixed_height && default_width == fixed_width) {
      System.out.println("This looks like a "image printer"");
    } else {
      System.out.println("This looks like a "real printer"");
    }
  }
}

这篇关于确定打印机是物理还是虚拟的便携式方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆