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 co.ordinate.printer; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font.FontFamily; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.Barcode; import com.itextpdf.text.pdf.BarcodeEAN; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.GrayColor; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfWriter; import com.lowagie.text.Font; import java.io.FileOutputStream; import java.io.IOException; import javax.swing.SwingWorker; /** * * @author developeracer */ public class NewJFrame extends javax.swing.JFrame { /** * Creates new form NewJFrame */ public NewJFrame() { 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() { jButton1 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); A4BTN = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); jButton5 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("jButton1"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jLabel1.setText("Print Co Ordinates"); A4BTN.setText("A4"); A4BTN.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { A4BTNActionPerformed(evt); } }); jButton3.setText("LETTER"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButton4.setText("A6"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); jButton5.setText("A5"); 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().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jButton1) .addComponent(jLabel1) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jButton5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(A4BTN, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addContainerGap(302, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(21, 21, 21).addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(A4BTN) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton5).addContainerGap(110, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed printA6Sheet(); // TODO add your handling code here: }//GEN-LAST:event_jButton1ActionPerformed private void A4BTNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_A4BTNActionPerformed new SwingWorker<Object, Object>() { String filename; @Override protected void done() { ConsoleMsg("Printing PROFILE SHEET IN PROGRESS.. "); } @Override protected Object doInBackground() throws Exception { Document doc = new Document(); try { PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("A4.pdf")); doc.open(); doc.addTitle("Recruitment PET Sheett - " + "Created By SOS : "); doc.addSubject("Confidential Report Eyes Only"); doc.addKeywords(""); doc.addAuthor("SOS"); doc.addCreator("SOS"); // A4 = 210mm x 297mm ~ 605points x 855points doc.setPageSize(PageSize.A4); doc.setMargins(10f, 10f, 10f, 10f); ///////////////////////////////////////////////////////////// int pageno = 1; for (int i = 0; i == pageno; i++) { // doc.add(imageRight); } PdfContentByte cb = writer.getDirectContent(); int i = 1; while (i <= pageno) { doc.newPage(); i++; } BaseFont labelFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", true); float width = doc.getPageSize().getWidth(); float height = doc.getPageSize().getHeight(); for (float h = 0; h <= height; h++) { for (float w = 0; w <= width; w++) { cb.beginText(); // cb.setColorFill(TITLE_COLOR); cb.setFontAndSize(labelFont, 2); cb.setTextMatrix(w, h); cb.showText(String.valueOf(h) + ":" + String.valueOf(w)); cb.endText(); w += 15; } h += 2; } cb.beginText(); // cb.setColorFill(TITLE_COLOR); cb.setFontAndSize(labelFont, 4); cb.setTextMatrix(20f, 220f); cb.showText("abcd"); cb.endText(); } catch (Exception e) { System.err.println(e.getMessage()); ConsoleMsg(e.getMessage()); } finally { doc.close(); } ConsoleMsg("PDF... GENERATED"); return null; // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }.execute(); // TODO add your handling code here: }//GEN-LAST:event_A4BTNActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed new SwingWorker<Object, Object>() { String filename; @Override protected void done() { ConsoleMsg("Printing PROFILE SHEET IN PROGRESS.. "); } @Override protected Object doInBackground() throws Exception { Document doc = new Document(); try { PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("letter.pdf")); doc.open(); doc.addTitle("Recruitment PET Sheett - " + "Created By SOS : "); doc.addSubject("Confidential Report Eyes Only"); doc.addKeywords(""); doc.addAuthor("SOS"); doc.addCreator("SOS"); // A4 = 210mm x 297mm ~ 605points x 855points doc.setPageSize(PageSize.LETTER); doc.setMargins(10f, 10f, 10f, 10f); ///////////////////////////////////////////////////////////// int pageno = 1; for (int i = 0; i == pageno; i++) { // doc.add(imageRight); } PdfContentByte cb = writer.getDirectContent(); int i = 1; while (i <= pageno) { doc.newPage(); i++; } BaseFont labelFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", true); float width = doc.getPageSize().getWidth(); float height = doc.getPageSize().getHeight(); for (float h = 0; h <= height; h++) { for (float w = 0; w <= width; w++) { cb.beginText(); // cb.setColorFill(TITLE_COLOR); cb.setFontAndSize(labelFont, 4); cb.setTextMatrix(w, h); cb.showText(String.valueOf(h) + ":" + String.valueOf(w)); cb.endText(); w += 25; } h += 5; } cb.beginText(); // cb.setColorFill(TITLE_COLOR); cb.setFontAndSize(labelFont, 4); cb.setTextMatrix(20f, 220f); cb.showText("abcd"); cb.endText(); } catch (Exception e) { System.err.println(e.getMessage()); ConsoleMsg(e.getMessage()); } finally { doc.close(); } ConsoleMsg("PDF... GENERATED"); return null; // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }.execute(); // TODO add your handling code here: }//GEN-LAST:event_jButton3ActionPerformed private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed printA6Sheet(); // TODO add your handling code here: }//GEN-LAST:event_jButton4ActionPerformed private void printA6Sheet() { new SwingWorker<Object, Object>() { String filename; @Override protected void done() { ConsoleMsg("Printing PROFILE SHEET IN PROGRESS.. "); System.err.println("Printing Done"); } @Override protected Object doInBackground() throws Exception { Document doc = new Document(); try { PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("A6.pdf")); doc.open(); doc.addTitle("Recruitment PET Sheett - " + "Created By SOS : "); doc.addSubject("Confidential Report Eyes Only"); doc.addKeywords(""); doc.addAuthor("SOS"); doc.addCreator("SOS"); // A4 = 210mm x 297mm ~ 605points x 855points doc.setPageSize(PageSize.A5); doc.setMargins(10f, 10f, 10f, 10f); ///////////////////////////////////////////////////////////// int pageno = 1; for (int i = 0; i == pageno; i++) { // doc.add(imageRight); } PdfContentByte cb = writer.getDirectContent(); int i = 1; while (i <= pageno) { doc.newPage(); i++; } BaseFont labelFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", true); float width = doc.getPageSize().getWidth(); float height = doc.getPageSize().getHeight(); for (float h = 0; h <= height; h++) { for (float w = 0; w <= width; w++) { cb.beginText(); // cb.setColorFill(TITLE_COLOR); cb.setFontAndSize(labelFont, 4); cb.setTextMatrix(w, h); cb.showText(String.valueOf(h) + ":" + String.valueOf(w)); cb.endText(); w += 30; } h += 8; } cb.beginText(); // cb.setColorFill(TITLE_COLOR); cb.setFontAndSize(labelFont, 4); cb.setTextMatrix(20f, 220f); cb.showText("abcd"); cb.endText(); } catch (Exception e) { System.err.println(e.getMessage()); ConsoleMsg(e.getMessage()); } finally { doc.close(); } ConsoleMsg("PDF... GENERATED"); System.err.println("Printing Done"); return null; // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }.execute(); // TODO add your handling code here: } public void ConsoleMsg(String msg) { } /** * @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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton A4BTN; private javax.swing.JButton jButton1; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JButton jButton5; private javax.swing.JLabel jLabel1; // End of variables declaration//GEN-END:variables }