Screens.Print.java Source code

Java tutorial

Introduction

Here is the source code for Screens.Print.java

Source

/*
 * 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 Screens;

import ConnectionDB.ConnMySql;
import Object.DaxilOlan;
import Object.PrintText;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.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.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

/**
 *
 * @author Texno USTA
 */
public class Print extends javax.swing.JDialog {

    /**
     * Creates new form Print
     *
     * @param parent
     * @param modal
     */
    DaxilOlan DataPrint;
    private final ConnMySql conn;
    //    private final String footer;
    private final String PrintTel;

    public Print(java.awt.Frame parent, boolean modal, DaxilOlan DataPrint) {
        super(parent, modal);
        initComponents();
        this.DataPrint = DataPrint;
        conn = new ConnMySql();
        String Print = conn.GetPrintByID().Data;
        PrintTel = conn.GetPrintTelByID().Data;
        jTextField1.setText(PrintTel);

        //         footer = "Thvil Verdi:  " + DataPrint.Ad + " " + DataPrint.Soyad + " (" + DataPrint.idDaxilOlan + ")" + "  ___________________ \n\n"
        //                + "Thvil ald:     Elxan ?sgrli       ___________________\n";
        jEditorPane1.setText(Print);

    }

    /**
     * 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() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();
        jToolBar1 = new javax.swing.JToolBar();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jEditorPane1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jEditorPane1.setPreferredSize(new java.awt.Dimension(420, 595));
        jScrollPane1.setViewportView(jEditorPane1);

        jToolBar1.setRollover(true);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/printer-icon.png"))); // NOI18N
        jButton2.setToolTipText("ap et");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton2);

        jButton1.setIcon(
                new javax.swing.ImageIcon(getClass().getResource("/Icon/Actions-document-print-preview-icon.png"))); // NOI18N
        jButton1.setToolTipText("lkin bax");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Save-icon.png"))); // NOI18N
        jButton3.setToolTipText("Mtni yadda saxla");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton3);

        jTextField1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

        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(200, 200, 200)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 543,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup().addContainerGap()
                                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 548,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 127,
                                        Short.MAX_VALUE)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 248,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup().addContainerGap()
                                        .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                                .addGroup(layout.createSequentialGroup().addGap(30, 30, 30)
                                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 595,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap()));

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    public static final String RESULT = "hello.pdf";

    public void ConvertPDF() throws DocumentException, FileNotFoundException, UnsupportedEncodingException,
            IOException, ParserConfigurationException, SAXException {
        FontFactory.register("tahoma.ttf", "MY_FONT");
        Document document = new Document(PageSize.A5, 20, 20, 10, 10);
        Image image = Image.getInstance("123.jpg");
        PdfPTable table = new PdfPTable(2);
        table.getDefaultCell().setBorder(0);
        //        table.setBorderColor(BaseColor.WHITE);

        PdfPTable tablefooter = new PdfPTable(3);
        tablefooter.getDefaultCell().setBorder(0);
        try {

            PdfWriter.getInstance(document, new FileOutputStream(RESULT));
            document.open();
            image.setAbsolutePosition(350f, 520f);
            document.add(image);
            document.add(new Paragraph("\n\n        Telefon: " + PrintTel + "\n\n\n",
                    FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            table.addCell(
                    new Paragraph("Ad, Soyad, ID kod", FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            table.addCell(new Paragraph(DataPrint.Ad + " " + DataPrint.Soyad + " (#" + DataPrint.idDaxilOlan + ")",
                    FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            table.addCell(new Paragraph("Model, Marka", FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            table.addCell(new Paragraph(DataPrint.Model + " " + DataPrint.Marka,
                    FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            table.addCell(new Paragraph("Aksesuar", FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            table.addCell(
                    new Paragraph(DataPrint.Aksesuar, FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            table.addCell(new Paragraph("Problem", FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            table.addCell(
                    new Paragraph(DataPrint.Problem, FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            table.addCell(new Paragraph("Tarix", FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            table.addCell(
                    new Paragraph(DataPrint.DatePlan, FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            table.addCell(new Paragraph("Telefon", FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            table.addCell(
                    new Paragraph(DataPrint.Telefon, FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            document.add(table);
            document.add(
                    new Paragraph(jEditorPane1.getText(), FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            //            document.add(new Paragraph(footer, FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            tablefooter.addCell(
                    new Paragraph("Thvil Verdi:", FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            tablefooter.addCell(new Paragraph("" + DataPrint.Ad + " " + DataPrint.Soyad + "",
                    FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            tablefooter.addCell(
                    new Paragraph("_____________", FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 12)));
            tablefooter.addCell(new Paragraph("  ", FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 12)));
            tablefooter.addCell(new Paragraph("  ", FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 12)));
            tablefooter.addCell(new Paragraph("  ", FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 12)));
            tablefooter.addCell(
                    new Paragraph("Thvil ald:", FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            tablefooter.addCell(
                    new Paragraph("Elxan ?sgrli", FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 10)));
            tablefooter.addCell(
                    new Paragraph("_____________", FontFactory.getFont("MY_FONT", BaseFont.IDENTITY_H, 12)));
            document.add(tablefooter);
        } catch (FileNotFoundException | DocumentException e) {
        }
        document.close();
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {
            ConvertPDF();

        } catch (DocumentException | ParserConfigurationException | SAXException ex) {
            Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, ex);

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

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

        Desktop desktop = Desktop.getDesktop();
        try {
            desktop.print(new File(RESULT));
        } catch (IOException e) {
        }
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            ConvertPDF();

        } catch (DocumentException | ParserConfigurationException | SAXException ex) {
            Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, ex);

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

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

        Desktop desktop = Desktop.getDesktop();
        try {
            desktop.open(new File(RESULT));
        } catch (IOException e) {
        }
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        conn.ChangePrint(new PrintText(1, jEditorPane1.getText()));
        conn.ChangePrint(new PrintText(2, jTextField1.getText()));
    }//GEN-LAST:event_jButton3ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    public javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables
}