打印机忽略javax.print.attribute.PrintRequestAttributeSet选项

时间:2010-10-27 14:43:20

标签: java printing

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

打印机无法识别我使用PrintRequestAttributeSet指定的选项/属性。虽然我指定要打印2份,但打印机只打印一份。我在这做错什么吗?

使用的打印机:佳能iR5050 PCL6


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;
 }

}

1 个答案:

答案 0 :(得分:7)

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

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

2)DocFlavor.INPUT_STREAM.POSTSCRIPT不支持javax.print属性     ○参考错误ID 4722601(无法发布多个链接)

3)没有用于JDK的Windows API来查找打印是否支持PCL     ○参考错误ID 4529660(无法发布多个链接)

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

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

此致 戈库尔