tztBackoffice.Print.java Source code

Java tutorial

Introduction

Here is the source code for tztBackoffice.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 tztBackoffice;

/**
 *
 * @author edward
 * dit is aangepaste code, gejat en gewijzigd!
 */
import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.io.FileOutputStream;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;

import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import java.io.File;
import javax.swing.JOptionPane;

public class Print extends JFrame {
    private JTable table;
    private File paths;

    // de tabel wordt opgehaald
    public Print(File path, JTable jtable) {
        // er wordt een layout manager aangezt voor de content pane
        getContentPane().setLayout(new BorderLayout());
        createTable(path, jtable);
    }

    private void createTable(File path, JTable jtable) {
        paths = path;

        table = jtable;
        // de tabel wordt aangemaakt
        JPanel tPanel = new JPanel(new BorderLayout());
        tPanel.add(table.getTableHeader(), BorderLayout.NORTH);
        tPanel.add(table, BorderLayout.CENTER);

        getContentPane().add(tPanel, BorderLayout.CENTER);

        print(path, table);

        // dit zorgt dat alles optimaal wordt geresized
        pack();
    }

    private void print(File path, JTable table) {
        // het document vormaat wort aangemaakt
        Document document = new Document(PageSize.A4.rotate());
        PdfWriter writer;
        try {
            // check eidigt op .pdf
            String pathd = "" + paths;
            if (!pathd.endsWith(".pdf")) {
                writer = PdfWriter.getInstance(document, new FileOutputStream(path + ".pdf"));
            } else {
                writer = PdfWriter.getInstance(document, new FileOutputStream(path));
            }
            // het document wordt geopend
            document.open();
            // de writer wordt aangemaakt
            PdfContentByte cb = writer.getDirectContent();
            // de staat wordt opgeslagen
            cb.saveState();
            // graphics worden aangemaakt
            Graphics2D g2 = cb.createGraphics(500, 500);
            // de clip wordt weggeschreven
            Shape oldClip = g2.getClip();
            g2.clipRect(0, 0, 500, 500);

            // de tabel wordt geprint in de graphics
            table.print(g2);
            g2.setClip(oldClip);

            // graphics word gesloten
            g2.dispose();
            cb.restoreState();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage() + "Er is een fout opgetreden");
        } finally {
            document.close();
        }
    }
}