打印机忽略的 javax.print.attribute.PrintRequestAttributeSet 选项 [英] javax.print.attribute.PrintRequestAttributeSet options ignored by Printer

查看:74
本文介绍了打印机忽略的 javax.print.attribute.PrintRequestAttributeSet 选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用 javax.print api 打印文件(纯文本).我能够查找打印机并提交打印作业.但我只能打印一份文件副本.下面是我一直在使用的代码.

I have to print a file (plain text) using javax.print api's. I am able to lookup the printer and submit a print job. But i am able to only print one copy of the file. Below is the code i have been using.

我使用 PrintRequestAttributeSet 指定的任何选项/属性均未被打印机识别.虽然我指定打印 2 份,但打印机只打印一份.我在这里做错了什么吗?

None of the options/attributes i specified using the PrintRequestAttributeSet is recognized by the printer. Though i specify 2 copies to print, the printer only prints one copy. Am i doing anything wrong here?

所用打印机:佳能 iR5050 PCL6

Printer used: Canon iR5050 PCL6

包 com.print;

package com.print;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.Finishings;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.NumberUp;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.attribute.standard.Sides;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;

public class TestPrint {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  String fileName = "D:/test.log";
  // Open the file
  InputStream in = null;
  try {
   in = new FileInputStream(fileName);
  } catch (FileNotFoundException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }

  // Figure out what type of file we're printing
  DocFlavor myFormat = getFlavorFromFilename(fileName);
  // Create a Doc
  Doc myDoc = new SimpleDoc(in, myFormat, null);
  // Build a set of attributes
  PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
  aset.add(OrientationRequested.LANDSCAPE);
  aset.add(new Copies(2));
  aset.add(Sides.DUPLEX);
  aset.add(MediaSizeName.NA_LETTER);
  aset.add(new NumberUp(2));
  aset.add(Finishings.STAPLE);
  // discover the printers that can print the format according to the
  // instructions in the attribute set
  PrintService[] services = PrintServiceLookup.lookupPrintServices(
    myFormat, aset);

  // Create a print job from one of the print services
  if (services.length > 0) {
   System.out.println("The print sent to>>>" + services[0].getName());
   DocPrintJob job = services[0].createPrintJob();

   // Monitor the print job with a listener
   job.addPrintJobListener(new PrintJobAdapter() {
    public void printDataTransferCompleted(PrintJobEvent e) {
     System.out.println("Data transfer completed!");
    }

    public void printJobNoMoreEvents(PrintJobEvent e) {
     System.out.println("No more events!");
    }

    public void printJobRequiresAttention(PrintJobEvent e) {
     System.out.println("Requires Attention!");
    }

    public void printJobFailed(PrintJobEvent e) {
     System.out.println("Print Job Failed!");
    }

    public void printJobCompleted(PrintJobEvent e) {
     System.out.println("Print Job Completed!");
    }

    public void printJobCanceled(PrintJobEvent e) {
     System.out.println("Print Job Cancelled!");
    }
   });

   try {
    job.print(myDoc, aset);
   } catch (PrintException pe) {
    pe.printStackTrace();
   }
   System.out.println("The print job ........");
  }
 }

 // A utility method to return a DocFlavor object matching the
 // extension of the filename.
 public static DocFlavor getFlavorFromFilename(String filename) {
  String extension = filename.substring(filename.lastIndexOf('.') + 1);
  extension = extension.toLowerCase();
  if (extension.equals("gif"))
   return DocFlavor.INPUT_STREAM.GIF;
  else if (extension.equals("jpeg"))
   return DocFlavor.INPUT_STREAM.JPEG;
  else if (extension.equals("jpg"))
   return DocFlavor.INPUT_STREAM.JPEG;
  else if (extension.equals("png"))
   return DocFlavor.INPUT_STREAM.PNG;
  else if (extension.equals("ps"))
   return DocFlavor.INPUT_STREAM.POSTSCRIPT;
  else if (extension.equals("txt"))
   return DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST;
  // Fallback: try to determine flavor from file content
  else
   return DocFlavor.INPUT_STREAM.AUTOSENSE;
 }

}

<小时>

推荐答案

经过长时间的研究和无数小时的测试,发现以下推论,

After long research and numerous hours of testing, found the following inferences,

1) DocFlavor.INPUT_STREAM.AUTOSENSE 不支持打印属性○ http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4876267

1) DocFlavor.INPUT_STREAM.AUTOSENSE would not support print attributes ○ http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4876267

2) DocFlavor.INPUT_STREAM.POSTSCRIPT 也不支持 javax.print 属性○ 参考 Bug ID 4722601(不能发布多个链接)

2) DocFlavor.INPUT_STREAM.POSTSCRIPT would not support javax.print attributes either ○ Refer Bug ID 4722601 (Not able to post more than one link)

3) 没有windows API for JDK 来判断打印是否支持PCL○ 参考 Bug ID 4529660(不能发布多个链接)

3) No windows API for JDK to find whether the print supports PCL ○ Refer Bug ID 4529660 (Not able to post more than one link)

因此,以上都不是 sun 支持论坛中指定的缺陷.所以我找到的唯一选择是将所有 POSTSCRIPT 命令嵌入到 Postscript 文件中,并使用 Javax.print api 打印此文档.确保将风味设置为 AUTOSENSE.

So none of the above are defects as specified in the sun support forum. So the only option i found is to snap in all the POSTSCRIPT commands in to the Postscript file and use Javax.print api to print this document. Make sure to set the flavor as AUTOSENSE.

还要确保您的打印机支持 POSTSCRIPT 格式.您也可以尝试使用 PJL.希望这些信息有帮助!

Also make sure your printer supports POSTSCRIPT format. You can also try using PJL. Hope this information helps!

问候,悟空

这篇关于打印机忽略的 javax.print.attribute.PrintRequestAttributeSet 选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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