2014-09-19 116 views
0

我嘗試使用DocPrintJob打印PDF,但它不適合頁面,所以我必須手動打印頁面。 我的pdf已經是Letter格式。Java - 設置打印頁面格式

這是我到目前爲止已經完成:

  PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 

      DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; 
      PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras); 
      PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); 
      PrintService service = ServiceUI.printDialog(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(), 200, 200, 
            printService, defaultService, flavor, pras); 

      if (service != null) { 
       pras.add(MediaSizeName.NA_LETTER); 
       PageFormat page = new PageFormat(); 
/*    MediaSizeName media = MediaSize.findMedia(
         (float) (page.getWidth()/72.0), 
         (float) (page.getHeight()/72.0), 
         Size2DSyntax.INCH); 
    */   
     //  pras.add(media); 
       DocPrintJob job = service.createPrintJob(); 
       FileInputStream fis=null; 
       File file = new File("test.pdf"); 
       try { 
        fis = new FileInputStream("test.pdf"); 

       } catch (FileNotFoundException e2) { 
        // TODO Auto-generated catch block 
        e2.printStackTrace(); 
       } 
       DocAttributeSet das = new HashDocAttributeSet(); 

       Doc document = null; 
       document = new SimpleDoc(fis, flavor, null); 


       try { 
        job.print(document, pras); 
       } catch (PrintException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } 


      } 

正如你所看到的,我試圖用MediaSizeName.NA_LETTER,但它只是不會改變任何東西。 有沒有辦法讓用戶選擇介質尺寸,在打印對話框中沒有選項? 在此先感謝!

回答

-1

似乎打印機正在根據您設置的偏好設置來使用默認紙張,所以如果您將首選項設置爲使用Letter尺寸,它會要求打印Letter尺寸頁面,默認情況下它將打印在A4尺寸的紙張上。

+0

我試圖在Letter尺寸上打印:「pras.add(MediaSizeName.NA_LETTER);」,但它不起作用,所以不應使用默認的A4尺寸的紙張? – Synor 2014-09-19 15:06:42

+0

顯示此示例代碼:http://www.programcreek.com/java-api-examples/index.php?example_code_path=druid-org.dlib.gui.print-GraphicPrinter.java – stacky 2014-09-19 15:15:00