java不显示打印_调用Java打印接口打印图片该如何隐藏打印对话框?

这篇博客介绍了如何在Java中调用打印接口打印图片时,避免显示打印对话框。通过设置特定的参数和服务,可以实现直接打印而不会弹出对话框。
摘要由CSDN通过智能技术生成

public static PrintService printDialog(GraphicsConfiguration gc,

int x, int y,

PrintService[] services,

PrintService defaultService,

DocFlavor flavor,

PrintRequestAttributeSet attributes)

throws HeadlessException

{

int defaultIndex = -1;

if (GraphicsEnvironment.isHeadless()) {

throw new HeadlessException();

} else if ((services == null) || (services.length == 0)) {

throw new IllegalArgumentException("services must be non-null " +

"and non-empty");

} else if (attributes == null) {

throw new IllegalArgumentException("attributes must be non-null");

}

if (defaultService != null) {

for (int i = 0; i < services.length; i++) {

if (services[i].equals(defaultService)) {

defaultIndex = i;

break;

}

}

if (defaultIndex < 0) {

throw new IllegalArgumentException("services must contain " +

"defaultService");

}

} else {

defaultIndex = 0;

}

// For now we set owner to null. In the future, it may be passed

// as an argument.

Window owner = null;

Rectangle gcBounds = (gc == null) ? GraphicsEnvironment.

getLocalGraphicsEnvironment().getDefaultScreenDevice().

getDefaultConfiguration().getBounds() : gc.getBounds();

ServiceDialog dialog;

if (owner instanceof Frame) {

dialog = new ServiceDialog(gc,

x + gcBounds.x,

y + gcBounds.y,

services, defaultIndex,

flavor, attributes,

(Frame)owner);

} else {

dialog = new ServiceDialog(gc,

x + gcBounds.x,

y + gcBounds.y,

services, defaultIndex,

flavor, attributes,

(Dialog)owner);

}

Rectangle dlgBounds = dialog.getBounds();

// get union of all GC bounds

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

GraphicsDevice[] gs = ge.getScreenDevices();

for (int j=0; j

gcBounds =

gcBounds.union(gs[j].getDefaultConfiguration().getBounds());

}

// if portion of dialog is not within the gc boundary

if (!gcBounds.contains(dlgBounds)) {

// put in the center relative to parent frame/dialog

dialog.setLocationRelativeTo(owner);

}

[color=red] dialog.show();[/color]

if (dialog.getStatus() == ServiceDialog.APPROVE) {

PrintRequestAttributeSet newas = dialog.getAttributes();

Class dstCategory = Destination.class;

Class amCategory = SunAlternateMedia.class;

Class fdCategory = Fidelity.class;

if (attributes.containsKey(dstCategory) &&

!newas.containsKey(dstCategory))

{

attributes.remove(dstCategory);

}

if (attributes.containsKey(amCategory) &&

!newas.containsKey(amCategory))

{

attributes.remove(amCategory);

}

attributes.addAll(newas);

Fidelity fd = (Fidelity)attributes.get(fdCategory);

if (fd != null) {

if (fd == Fidelity.FIDELITY_TRUE) {

removeUnsupportedAttributes(dialog.getPrintService(),

flavor, attributes);

}

}

}

return dialog.getPrintService();

}

ServiceUI.printDialog 就是显示打印对话框,当你确认之后返回dialog.getPrintService();

打印时必须

if (services.length > 0) {

*    PrintService service =  ServiceUI.printDialog(null, 50, 50,

*                                               services, services[0],

*                                               null,

*                                               attributes);

*    if (service != null) {

*     ... print ...

*    }

* }

即当

if (service != null) {

... print ...

}

可以加上else表示无法打印

你直接打印就不显示对话框了

PrintService ps = pss[0];

//PrintService ps = PrintServiceLookup.lookupDefaultPrintService() ;

DocPrintJob job = ps.createPrintJob();

DocAttributeSet das = new HashDocAttributeSet();

Doc doc = new SimpleDoc(fi, df, das);

if (ps != null) {

job.print(doc, pras);

fi.close();

}

2012年12月16日 15:26

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值