Skip navigation links
Java™ Platform
Standard Ed. 8

Package javax.print

提供Java TM Print Service API的主要类和接口。

See: 描述

Package javax.print Description

提供Java TM Print Service API的主要类和接口。 Java Print Service API使客户端和服务器应用程序能够:

打印服务发现

应用程序调用抽象类PrintServiceLookup的静态方法来定位具有满足应用程序打印请求功能的打印服务。 例如,要打印双面文档,应用程序首先需要查找具有双面打印功能的打印机。

JDK包括可以定位标准平台打印机的PrintServiceLookup实现。 要找到其他类型的打印机,如IPP打印机或JINI打印机,打印服务提供商可以编写PrintServiceLookup的PrintServiceLookup 打印服务提供商可以使用SPI JAR file specification动态安装这些PrintServiceLookup 实现

属性定义

javax.print.attributejavax.print.attribute.standard软件包定义了打印属性,它们描述打印服务的功能,指定打印作业的要求,并跟踪打印作业的进度。

javax.print.attribute包描述了属性的类型以及如何将它们收集到集合中。 javax.print.attribute.standard包枚举了API支持的所有标准属性,其中大多数是2000年9月的IETF规范RFC 2911 Internet Printing Protocol, 1.1: Model and Semantics中指定的属性的实现。javax.print.attribute.standard中javax.print.attribute.standard包括常见功能,例如:分辨率,副本,媒体大小,作业优先级和页面范围。

文件类型规格

DocFlavor类表示打印数据格式,如JPEG或PostScript。 DocFlavor对象由描述格式的MIME类型和指示文档如何传递到打印机或输出流的文档表示类名称组成。 应用程序使用DocFlavor和属性集来查找可以打印由DocFlavor的文档类型并具有属性集指定的功能的打印机。

使用API

使用Java Print Service API的典型应用程序会执行以下步骤来处理打印请求:
  1. 选择一个DocFlavor
  2. 创建一组属性。
  3. 找到可以处理DocFlavor和属性集指定的打印请求的打印服务。
  4. 创建封装 DocFlavor和实际打印数据的Doc对象,可以采取多种形式,包括:Postscript文件,JPEG图像,URL或纯文本。
  5. 从打印服务获取由DocPrintJob表示的打印作业。
  6. 调用打印作业的打印方式。
以下代码示例演示了Java Print Service API的典型用法:定位可以在A4纸上打印Postscript文档的五个双面副本的打印机,从返回的打印服务之一创建打印作业并调用打印。
FileInputStream psStream;
try {
   psStream = new FileInputStream("file.ps");
} catch (FileNotFoundException ffne) {
}
if (psStream == null) {
    return;
}

DocFlavor psInFormat = DocFlavor.INPUT_STREAM.POSTSCRIPT;
Doc myDoc = new SimpleDoc(psStream, psInFormat, null);  
PrintRequestAttributeSet aset = 
        new HashPrintRequestAttributeSet();
aset.add(new Copies(5));
aset.add(MediaSize.A4);
aset.add(Sides.DUPLEX);
PrintService[] services = 
  PrintServiceLookup.lookupPrintServices(psInFormat, aset);
if (services.length > 0) {
   DocPrintJob job = services[0].createPrintJob();
   try {
        job.print(myDoc, aset);
   } catch (PrintException pe) {}
}

请注意:在javax.print API中,方法的空参考参数是不正确的,除非在方法中明确记录为具有有意义的解释。 使用相反的是错误的编码,可能会立即或稍后导致运行时异常。 IllegalArgumentException和NullPointerException是这种情况的典型和可接受的运行时间异常的示例。

从以下版本开始:
1.4
Skip navigation links
Java™ Platform
Standard Ed. 8

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.

本帮助文档是使用 《谷歌翻译》翻译,请与英文版配合使用