2012-07-09 70 views
2

我有一個表單,用戶可以用數據填寫字段。 此後,他/她必須能夠將數據導出爲我已經寫了,因爲它可以在下面看到一個pdf:用Java打印數據

public void onSubmit() { 
try { 
    ManagerModel manager = managerDao.getManager(person); 
    PictureModel picture = new PictureModel(); 
    if (person.getPhotoId() != null) { 
     picture = new PictureModel(pictureDao.findPictureById(person.getPhotoId())); 
    } 
    getRequestCycle().setRequestTarget(new FileRequestTarget(Exporter.exportFile(person, manager, picture), person.getPdfName())); 
} catch (Exception e) { 
    Log.warn(e); 
} 

現在這爲我提供了所有數據一起PDF導出。我也想創建一個按鈕,允許用戶打印輸入到這些字段中的數據。現在,這應該是表單上的打印按鈕,而不是要求用戶導出然後打印。

有人可以告訴我如何創建此打印按鈕?我應該只使用PDF導出的輸出然後發送到打印機?如果是這樣,我該如何寫在Java中?

回答

5

那麼你可以通過簡單的創建一個按鈕:

import javax.swing.*; 
.... 
JButton button = new JButton("Print"); 

然後添加ActionListener到按鈕:

import java.awt.*; 
.... 
button.addActionListener(new ActionListener() { 
      @override 
      public void actionPerformed(ActionEvent e) 
      { 
       //Execute when button is pressed 
       printPDF("path/to/file/.pdf"); 
      } 
     }); 

然後再打印PDF您可以使用此方法:

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import javax.print.*; 
import javax.print.attribute.HashPrintRequestAttributeSet; 
import javax.print.attribute.PrintRequestAttributeSet; 
.... 
public static void printPDF(String file) { 

    FileInputStream psStream = null; 
    try { 
     psStream = new FileInputStream(file); 
    } catch (FileNotFoundException ffne) { 
     ffne.printStackTrace(); 
    } 
    if (psStream == null) { 
     return; 
    } 
    DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE; 
    Doc myDoc = new SimpleDoc(psStream, psInFormat, null); 
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 
    PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset); 

    // this step is necessary because I have several printers configured 
    PrintService myPrinter = null; 
    for (int i = 0; i < services.length; i++) { 
     String svcName = services[i].toString(); 
     System.out.println("service found: " + svcName); 
     if (svcName.contains("printer closest to me")) { 
      myPrinter = services[i]; 
      System.out.println("my printer found: " + svcName); 
      break; 
     } 
    } 
    if (myPrinter != null) { 
     DocPrintJob job = myPrinter.createPrintJob(); 
     try { 
      job.print(myDoc, aset); 

     } catch (Exception pe) { 
      pe.printStackTrace(); 
     } 
    } else { 
     System.out.println("no printer services found"); 
    } 
} 

附錄:

  • 爲了使特定打印機這方面的工作,可能不會有「打印機最接近我」: 更改此代碼,包括您的打印機名稱,或用或equals()分別確切的打印機名稱:

    String printerName=""; 
    .... 
    if (svcName.contains(printerName)||svcName.equals(printerName)) { 
            myPrinter = services[i]; 
            System.out.println("my printer found: " + svcName); 
            break; 
    } 
    

參考文獻:

+0

唯一的問題是,您所定義的文件的路徑,但對我來說,產生的PDF導出按鈕被點擊時立即,所以如何將我定義哪裏pdf是或者是它的位置?有沒有一種方法可以說生成的輸出(pdf)應該打印出來? – Pita 2012-07-09 14:14:30

+0

你是什麼意思?爲什麼不創建並將PDF保存在臨時位置,打印(使用臨時位置)並在打印完成後刪除臨時文件? – 2012-07-09 14:16:59

+0

另外,我剛剛創建了PDF類,但它的下劃線svcName說svcName無法解析爲變量。你能解釋我應該如何解決這個問題嗎? – Pita 2012-07-09 14:17:48