2012-07-09 71 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