2015-07-10 144 views
-1

我在netbeans中創建了一個程序,點擊按鈕在主目錄中創建一個pdf文件,但是我必須添加什麼代碼才能在點擊按鈕後打開它。如何在按鈕點擊netbeans後從java程序創建的文件

package demopdf; 

import com.itextpdf.text.Document; 
import com.itextpdf.text.DocumentException; 
import com.itextpdf.text.Paragraph; 
import com.itextpdf.text.pdf.PdfWriter; 


import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 

import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* 
* @author sunil 
*/ 

public class DemoPdfCreate extends javax.swing.JFrame { 
public static final String RESULT="Report.pdf"; 



/** 
* Creates new form DemoPdfCreate 
*/ 
public DemoPdfCreate() { 
    initComponents(); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated 
Code">       

private void initComponents() { 

    jButton_pdf = new javax.swing.JButton(); 
    jTextField_txt = new javax.swing.JTextField(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    jButton_pdf.setText("Save To Pdf"); 
    jButton_pdf.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton_pdfActionPerformed(evt); 
     } 
    }); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout 
(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup 
(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGroup(layout.createParallelGroup 
(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(53, 53, 53) 
        .addComponent(jTextField_txt, 
javax.swing.GroupLayout.PREFERRED_SIZE, 266, 
javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(113, 113, 113) 
        .addComponent(jButton_pdf, 
javax.swing.GroupLayout.PREFERRED_SIZE, 144, 
javax.swing.GroupLayout.PREFERRED_SIZE))) 
      .addContainerGap(81, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup 
(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, 
layout.createSequentialGroup() 
      .addContainerGap() 
      .addComponent(jTextField_txt, 
javax.swing.GroupLayout.PREFERRED_SIZE, 159, 
javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addPreferredGap 
(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE) 
      .addComponent(jButton_pdf, 
javax.swing.GroupLayout.PREFERRED_SIZE, 37, 
javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addGap(64, 64, 64)) 
    ); 

    pack(); 
}// </editor-fold>       

private void jButton_pdfActionPerformed(java.awt.event.ActionEvent evt) 
{            
    // TODO add your handling code here: 
    Document doc =new Document(); 
try { 
    try { 
    PdfWriter.getInstance(doc, new FileOutputStream(RESULT)); 
} catch (FileNotFoundException ex) { 
    Logger.getLogger(DemoPdfCreate.class.getName()).log(Level.SEVERE, null, 
ex); 
} 
    doc.open(); 

    doc.add(new Paragraph(jTextField_txt.getText())); 

    doc.close(); 

} catch (DocumentException ex) { 
    Logger.getLogger(DemoPdfCreate.class.getName()).log(Level.SEVERE, null, 
ex); 
} 



}           

/** 
* @param args the command line arguments 
*/ 

public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting 
code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the 
default look and feel. 
    * For details see 
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : 
javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(DemoPdfCreate.class.getName 
()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(DemoPdfCreate.class.getName 
()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(DemoPdfCreate.class.getName 
()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(DemoPdfCreate.class.getName 
()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(() -> { 
     new DemoPdfCreate().setVisible(true); 
    }); 
} 

// Variables declaration - do not modify      
private javax.swing.JButton jButton_pdf; 
private javax.swing.JTextField jTextField_txt; 
// End of variables declaration     


} 

我上面的程序,並從創建,使用itextpdf.jar我可以能夠創建PDF文件在我frograms目錄時,它在相同的按鈕創建點擊打開這iwant

+0

我不想使用openfiledialog,就像Process.Start和文件路徑在vb中使用一樣。應該在java中使用的代碼打開文件 –

回答

3

我會請嘗試Desktop.open(File),其中:

啓動關聯的應用程序以打開文件。

if (Desktop.isDesktopSupported()) { 
    try { 
     File myFile = new File("/path/to/file.pdf"); 
     Desktop.getDesktop().open(myFile); 
    } catch (IOException ex) { 
     // no application registered for PDFs 
    } 
} 
+0

是的。這是實現它的最簡單的方法。 –

+0

我在我的問題中添加了代碼,請指導。 –

+0

這對我有很大的幫助。謝謝 –

-1

你可以在actionperfomrmed方法中使用類似的東西:File yourSavedFile = new File("path to your saved file")。之後,您可以從該文件讀取並顯示它。

+0

OP詢問基於安裝在OS中的應用程序將文件打開爲常規文件,而不是以文本文件的閱讀模式。 –

+0

這是非常不清楚他的真正含義。如果我誤解了它,我很抱歉。 – grahan

+0

我應該如何添加我的程序代碼問題,它只顯示添加的代碼,我無法編輯和添加我的程序的代碼 –

相關問題