Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package gerarpdf; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfWriter; import java.awt.Desktop; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; /** * * @author Everton */ public class GerarPdf extends javax.swing.JFrame { /** * Creates new form GerarPdf */ public GerarPdf() { 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">//GEN-BEGIN:initComponents private void initComponents() { jScrollPane2 = new javax.swing.JScrollPane(); jtxt_area = new javax.swing.JTextArea(); btnGera = new javax.swing.JButton(); btn_sair = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Gerar PDF"); setResizable(false); jtxt_area.setColumns(20); jtxt_area.setRows(5); jtxt_area.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Insira o texto", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 0, 11), new java.awt.Color(255, 0, 51))); // NOI18N jScrollPane2.setViewportView(jtxt_area); btnGera.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gerarpdf/pdf.png"))); // NOI18N btnGera.setToolTipText("Gerar arquivo pdf"); btnGera.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnGeraActionPerformed(evt); } }); btn_sair.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N btn_sair.setText("Sair"); btn_sair.setToolTipText("sair"); btn_sair.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_sairActionPerformed(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().addContainerGap(773, Short.MAX_VALUE) .addComponent(btnGera, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(27, 27, 27) .addComponent(btn_sair, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(50, 50, 50)) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane2) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(27, 27, 27) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 422, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnGera, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btn_sair, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(625, 625, 625))); setSize(new java.awt.Dimension(1012, 583)); setLocationRelativeTo(null); }// </editor-fold>//GEN-END:initComponents private void btnGeraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGeraActionPerformed // Instanciando o document Document documento = new Document(); try { PdfWriter.getInstance(documento, new FileOutputStream("documento.pdf")); if (jtxt_area == null || jtxt_area.getText().toString().isEmpty()) { } documento.open(); //documento.add(new Paragraph("aLO ALOA AMOANOANIANINO")); documento.add(new Paragraph(jtxt_area.getText())); } catch (DocumentException | FileNotFoundException ex) { System.err.println("Erro ao carregar arquivo"); } finally { documento.close(); } try { Desktop.getDesktop().open(new File("documento.pdf")); } catch (IOException ex) { Logger.getLogger(GerarPdf.class.getName()).log(Level.SEVERE, null, ex); System.err.println("Erro ao carregar arquivo " + ex); } }//GEN-LAST:event_btnGeraActionPerformed private void btn_sairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_sairActionPerformed int opc = JOptionPane.showConfirmDialog(null, "Deseja realmente sair?", "Ateno", JOptionPane.YES_NO_OPTION); if (opc == JOptionPane.YES_OPTION) { System.exit(0); } }//GEN-LAST:event_btn_sairActionPerformed /** * @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(GerarPdf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(GerarPdf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(GerarPdf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(GerarPdf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new GerarPdf().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnGera; private javax.swing.JButton btn_sair; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTextArea jtxt_area; // End of variables declaration//GEN-END:variables }