2011-07-13 38 views
2

我正在使用Apache FOP API打印一段時間運行良好的文檔,但現在它試圖在紙盒1上的合法尺寸紙上打印。我想知道如果我可以請將其更改爲Letter尺寸,以便用戶不必手動按打印機上的按鈕即可完成此操作。修改介質名稱的打印屬性Java Apache FOP API

public void printDocument() { 
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; 
    PrintRequestAttributeSet aset = 
      new HashPrintRequestAttributeSet(); 
    PrintService prnSvc = null; 

    /* locate a print service that can handle it */ 
    PrintService[] pservices = 
      PrintServiceLookup.lookupPrintServices(null, null); 
    if (pservices.length > 0) { 
     int ii = 0; 
     while (ii < pservices.length) { 
      System.out.println("Named Printer found: " + pservices[ii].getName()); 
      if (pservices[ii].getName().endsWith("xyz")) { 
       prnSvc = pservices[ii]; 
       System.out.println("Named Printer selected: " + pservices[ii].getName() + "*"); 
       break; 
      } 
      ii++; 
     } 

     /* create a print job for the chosen service */ 
     DocPrintJob pj = prnSvc.createPrintJob(); 
     try { 
      File file = new File("test.pcl"); 
      FileInputStream fis = new FileInputStream(file); //Doc encapsulating the print data 
      Doc doc = new SimpleDoc(fis, flavor, null); 
      /* print the doc as specified */ 
      pj.print(doc, aset); 
     } catch (IOException ie) { 
      System.err.println(ie); 
     } catch (PrintException e) { 
      e.printStackTrace(); 
      System.err.println(e); 
     } 
    } 
} 

如果任何人都可以提供任何相同的建議,將非常感激。

回答

2

你需要將它添加到aset指定紙張尺寸:

aset.add(javax.print.attribute.standard.MediaSizeName.<desired paper size>); 

(Javadoc文檔MediaSizeName)。對於字母大小,請使用

aset.add(javax.print.attribute.standard.MediaSizeName.NA_LETTER); 
+0

嘗試過,但使用AUTOSENSE docflavor時,printRequestAttributeSet上的屬性看起來不起作用。 –

+0

@John C:你如何構建文檔?紙張大小可能嵌入在文件中。 –