com.mycompany.mavenproject2.VirtualkeyController.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.mavenproject2.VirtualkeyController.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 com.mycompany.mavenproject2;

import java.io.FileOutputStream;
import java.util.Date;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

import com.itextpdf.text.pdf.PdfWriter;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.HostServices;

import java.io.File;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import com.mycompany.mavenproject2.FinalDemo;
import javafx.fxml.Initializable;

/**
 * FXML Controller class
 *
 * @author Third Ev
 */

public class VirtualkeyController implements Initializable {
    private static String FILE1 = "/home/drashti/MyDemo.pdf";
    private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
    private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED);
    private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
    private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
    public HostServices hostServices = null;
    public com.mycompany.mavenproject2.FinalDemo f = new com.mycompany.mavenproject2.FinalDemo();
    public FinalDemo f4 = new FinalDemo();

    @FXML
    public void handlePDFButtonAction(ActionEvent a) throws IOException, DocumentException {

        Document document = new Document();
        FileOutputStream f2 = new FileOutputStream(FILE1);
        PdfWriter.getInstance(document, f2);
        document.open();
        addMetaData(document);
        addTitlePage(document);
        //      addContent(document);
        // document.close();
        /* PdfReader reader = new PdfReader("C:/Users/Third Ev/Downloads/my.pdf");
        System.out.println("This PDF has "+reader.getNumberOfPages()+" pages.");
        String page = PdfTextExtractor.getTextFromPage(reader, 2);
        System.out.println("Page Content:\n\n"+page+"\n\n");
        System.out.println("Is this document tampered: "+reader.isTampered());
        System.out.println("Is this document encrypted: "+reader.isEncrypted());*/
        /* PdfDecoder pdf = new PdfDecoder();
         pdf.openPdfFile("C:/Users/Third Ev/Downloads/my.pdf");*/
        /*  hostServices = getHostServices();
            hostServices.showDocument("C:/Users/Third Ev/Documents/NetBeansProjects/FinalDemo/src/finaldemo/Controller/iText.pdf");*/
        document.close();
        f2.close();
        final File file = new File("/home/drashti/MyDemo.pdf");

        f4.getHostServices().showDocument(file.toURI().toString());

    }

    private static void addMetaData(Document document) {
        document.addTitle("Item");
        document.addSubject("Item History ");
        document.addKeywords("Item, History, POS");
        document.addAuthor("Store Name");
        document.addCreator("Germanium Inc.");
    }

    private static void addTitlePage(Document document) throws DocumentException {
        Paragraph preface = new Paragraph();
        // We add one empty line
        addEmptyLine(preface, 1);
        // Lets write a big header
        preface.add(new Paragraph("Report Item On History", catFont));

        addEmptyLine(preface, 1);
        // Will create: Report generated by: _name, _date
        /*  preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
              smallBold));*/
        preface.add(new Paragraph("Address 1"));
        //addEmptyLine(preface, 1);
        preface.add(new Paragraph("Address 2"));
        // addEmptyLine(preface, 1);
        preface.add(new Paragraph("City"));
        // addEmptyLine(preface, 1);
        preface.add(new Paragraph("Store"));
        //addEmptyLine(preface, 3);
        //preface.add(new Paragraph("This document describes something which is very important ",smallBold));
        preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(),
                smallBold));
        addEmptyLine(preface, 2);
        createTable("category_detail", preface);
        //addEmptyLine(preface, 8);

        /*preface.add(new Paragraph("This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).",
            redFont));*/
        //preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$        smallBold));
        document.add(preface);
        // Start a new page
        //document.newPage();
    }

    /* private static void addContent(Document document) throws DocumentException {
       Anchor anchor = new Anchor("First Chapter", catFont);
       anchor.setName("First Chapter");
        
       // Second parameter is the number of the chapter
       Chapter catPart = new Chapter(new Paragraph(anchor), 1);
        
       Paragraph subPara = new Paragraph("Subcategory 1", subFont);
       Section subCatPart = catPart.addSection(subPara);
       subCatPart.add(new Paragraph("Hello"));
        
       subPara = new Paragraph("Subcategory 2", subFont);
       subCatPart = catPart.addSection(subPara);
       subCatPart.add(new Paragraph("Paragraph 1"));
       subCatPart.add(new Paragraph("Paragraph 2"));
       subCatPart.add(new Paragraph("Paragraph 3"));
        
       // add a list
       createList(subCatPart);
       Paragraph paragraph = new Paragraph();
       addEmptyLine(paragraph, 5);
       subCatPart.add(paragraph);
        
       // add a table
       createTable(subCatPart);
        
       // now add all this to the document
       document.add(catPart);
        
       // Next section
       anchor = new Anchor("Second Chapter", catFont);
       anchor.setName("Second Chapter");
        
       // Second parameter is the number of the chapter
       catPart = new Chapter(new Paragraph(anchor), 1);
        
       subPara = new Paragraph("Subcategory", subFont);
       subCatPart = catPart.addSection(subPara);
       subCatPart.add(new Paragraph("This is a very important message"));
        
       // now add all this to the document
       document.add(catPart);
        
     }*/

    private static void createTable(/*Section subCatPart*/String tableName, Paragraph preface)
            throws BadElementException {
        try {
            ArrayList<String> list = new ArrayList<String>();

            // t.setBorderColor(BaseColor.GRAY);
            // t.setPadding(4);
            // t.setSpacing(4);
            // t.setBorderWidth(1);
            int length = 0;
            java.sql.Connection conn = Connection.getConnect();
            Statement st = conn.createStatement();
            st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM " + tableName);

            ResultSetMetaData rsMetaData = rs.getMetaData();

            int numberOfColumns = rsMetaData.getColumnCount();
            System.out.println("resultSet MetaData column Count=" + numberOfColumns);
            for (int i = 1; i <= numberOfColumns; i++) {
                length = i;
            }
            MongoClient client = new MongoClient();
            MongoDatabase db = client.getDatabase("FinalDemo");

            PdfPTable table = new PdfPTable(length);

            for (int i = 1; i <= numberOfColumns; i++) {
                // System.out.println("column MetaData ");
                //System.out.println("column number " + i);
                // get the column's name.
                System.out.println(rsMetaData.getColumnName(i));
                PdfPCell c1 = new PdfPCell(new Phrase(rsMetaData.getColumnName(i)));
                c1.setHorizontalAlignment(Element.ALIGN_CENTER);
                list.add(rsMetaData.getColumnName(i));
                table.addCell(c1);
            }
            while (rs.next()) {
                for (String col : list) {
                    //System.out.println("Column name is "+col+"value is "+rs.getString(col));
                    table.addCell(rs.getString(col));
                }
            }
            /*PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1"));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);
                
            c1 = new PdfPCell(new Phrase("Table Header 2"));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);
                
            c1 = new PdfPCell(new Phrase("Table Header 3"));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);*/
            table.setHeaderRows(1);

            preface.add(table);
            //subCatPart.add(table);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(VirtualkeyController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(VirtualkeyController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private static void createList(Section subCatPart) {
        List list = new List(true, false, 10);
        list.add(new ListItem("First point"));
        list.add(new ListItem("Second point"));
        list.add(new ListItem("Third point"));
        subCatPart.add(list);
    }

    private static void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph("\n"));

        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }

}