gpt4 book ai didi

java - 如何获取默认打印机属性设置

转载 作者:行者123 更新时间:2023-12-02 05:36:38 25 4
gpt4 key购买 nike

我想知道如何使用 PrintServiceAttributeSet 获取默认打印机属性设置。例如,在从打印机打印之前,我需要知道当前的打印机属性设置,例如色度。根据该返回值,我需要将打印机设置为新的属性设置。谁能帮我吗。是否可以获取一些示例代码来获取和设置此属性。

最佳答案

引用How do I get print service attribute set? :

This example demonstrates how to get print service’s attribute set using the javax.print API. First we find the default printer for the current machine using the PrintServiceLookup class. This will give us a PrintService object, this object might be null if no print service found.

The final step is to get the print service attribute set by calling getAttributes() method of the PrintService. We can convert the returned AttributeSet into an array using the toArray() method and iterate it.

package org.kodejava.example.print;

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

public class PrinterAttribute {
public static void main(String[] args) {
//
// Locates the default print service for this environment.
//
PrintService printer =
PrintServiceLookup.lookupDefaultPrintService();

if (printer != null) {
//
// Getting print service's attribute set.
//
AttributeSet attributes = printer.getAttributes();
for (Attribute a : attributes.toArray()) {
String name = a.getName();
String value = attributes.get(a.getClass()).toString();
System.out.println(name + " : " + value);
}
}
}
}

关于java - 如何获取默认打印机属性设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24905203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com