generate PDF with Jlabel BLOB image that comes from mysql database in itext

Asked

Viewed 94 times

2

Good morning, I am with a java desktop program and my problem is in generating PDF with itext, with the BLOB image coming from mysql database, the same is being shown in Jlabel inside a Jpanel with a select.

package classes_conexao;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.text.MaskFormatter;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.BaseColor;

import classes_conexao.pdf.exemplo3_tableComDadosdoBanco;
import javax.swing.JTextPane;
import java.awt.FlowLayout;

public class Certificados extends JFrame {

    private static final int BOLD = 0;
    private JPanel contentPane;
    private JTextField txt_prPresidente;
    private JTextField txt_prDirigente;
    private JTextField txt_titCertificado;
    private JTextField txt_Certificado;
    private JTextField txt_data;
    private JComboBox cb_TpCertificado;
    private JButton btn_imprimir;
    private JTextPane txt_Mensagem;
    private JComboBox cb_assinatura; 
    private JPanel pn_assinatura;
    private JLabel lbl_rubrica;
    private JPanel pn_dirigente;
    private JLabel lbl_Dirigente;
    private JComboBox cb_prDirigente;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Certificados frame = new Certificados();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void geraPdf(String tipodecertificado, String data, String imagedir, String pastorpresidente, String assinatura, String assinaturadir, String pastordirigente, String titulocertificados, String mensagem) throws SQLException {
        Document documento = new Document(PageSize.A4.rotate());

        try {

            OutputStream outputStream = new FileOutputStream("Certificado.pdf");
                PdfWriter.getInstance(documento,outputStream);
                documento.open();//abre o arquivo
                //adicionando texto para o pdf

                // conexão com o banco de dados

                ....cóigo....



                ImageIcon img = (ImageIcon)lbl_Dirigente.getIcon();
                documento.add(img);

                ...Código...

                documento.close();
                JOptionPane.showMessageDialog(null, "Certificado gerado com sucesso");

                Process p = Runtime.getRuntime().exec("cmd.exe /C Certificado.pdf");            



        } catch (FileNotFoundException e1) {
            JOptionPane.showMessageDialog(null, e1);
            e1.printStackTrace();
        }

        catch (MalformedURLException e11) {
            // TODO Auto-generated catch block
            e11.printStackTrace();
        } 
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (DocumentException e111) {
            Logger.getLogger(exemplo3_tableComDadosdoBanco.class.getName()).log(null, null, e111);
            JOptionPane.showMessageDialog(null, e111);
            e111.printStackTrace();
        }catch (Exception e1111)
        {
            System.err.println("Got an exception! ");
            System.err.println(e1111.getMessage());
          }}




    /**
     * Create the frame.
     */
    public Certificados() {
        setTitle("Selecione o Membro que emitir\u00E1 Carta");
        setIconImage(Toolkit.getDefaultToolkit().getImage(Certificados.class.getResource("/icon/icon_igrejas.png")));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 465, 440);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

                            ...Código...


        btn_imprimir = new JButton("");
        btn_imprimir.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {


                                    ...Código...

                try {
                    geraPdf(txt_data.getText(), txt_Certificado.getText(), lbl_Dirigente.getText(), txt_prPresidente.getText(), txt_Mensagem.getText(), cb_assinatura.getSelectedItem().toString(), cb_prDirigente.getSelectedItem().toString(), txt_prDirigente.getText(), txt_titCertificado.getText());

                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        btn_imprimir.setIcon(new ImageIcon(Certificados.class.getResource("/icon/printer icon40x40.png")));
        btn_imprimir.setBounds(362, 326, 61, 50);
        contentPane.add(btn_imprimir);

                                    ...Código...


                                ...Código...

        cb_assinatura.setBounds(31, 136, 176, 20);
        contentPane.add(cb_assinatura);

        cb_prDirigente = new JComboBox();
        cb_prDirigente.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    String sql = "select img_arc_dir FROM certificados";
                    Connection con = Conexao.conectar();
                    PreparedStatement prep = con.prepareStatement(sql);
                    ResultSet rs = prep.executeQuery();

                    int usertypes = (int) cb_prDirigente.getSelectedIndex();

                    if(rs != null) {
                        while(rs.next()) {

                        switch (usertypes) {
                        case 0:

                            lbl_Dirigente.setIcon(null); 
                            this.dispose();
                            break;
                        case 1:

                             byte[] imagedir= rs.getBytes("img_arc_dir");  
                                if(imagedir != null) {
                               ImageIcon formatdir = new ImageIcon(imagedir); 
                               formatdir.setImage(formatdir.getImage().getScaledInstance(pn_dirigente.getWidth() -5, pn_dirigente.getHeight() -5, 100));
                               lbl_Dirigente.setIcon(formatdir); 

                            this.dispose();
                            break;
                    }

                }
            }           
        }

            prep.close();
        }catch (SQLException e) {
            e.printStackTrace();
        }

            }

            private void dispose() {
                // TODO Auto-generated method stub

            }
        });
        cb_prDirigente.setModel(new DefaultComboBoxModel(new String[] {"Selecione uma Rubrica", "Pastor Dirigente"}));
        cb_prDirigente.setBounds(238, 136, 176, 20);
        contentPane.add(cb_prDirigente);

        pn_dirigente = new JPanel();
        pn_dirigente.setBorder(new LineBorder(new Color(169, 169, 169)));
        pn_dirigente.setBounds(231, 168, 192, 47);
        contentPane.add(pn_dirigente);
        pn_dirigente.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

        lbl_Dirigente = new JLabel("");
        pn_dirigente.add(lbl_Dirigente);

    }
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.