Java tutorial
import com.itextpdf.text.BadElementException; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; import com.itextpdf.text.FontFactory; import com.itextpdf.text.Image; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.awt.Desktop; import static java.awt.image.ImageObserver.ALLBITS; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.sql.SQLException; import java.text.SimpleDateFormat; /* * 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. */ /** * * @author Y.T */ public class imprimer extends javax.swing.JFrame { private String text; private String text2; private String text3; public imprimer() { text = " Je soussign, Belahsen Youness, Doyen de la Facult Polydisciplinaire de Ouarzazate atteste que Monsieur: \n" + " - NOM : \n" + " - PRENOM : \n" + " - CIN : \n" + " - D.R.P.P : \n" + " \n" + "Exerce la fonction dadministrateur 3 me grade au sein de notre tablissement depuis le.\n" + "\n" + " Cette attestation est dlivre lintress(e) pour servir et valoir ce que de droit.\n\n"; text2 = "Le Doyen de la Facult Polydisciplinaire Ouarzazate :\n" + "- Vu le Dahir n1.58.008 du 04 chabane 1377 (24 fvrier 1958) portant statut gnral de la fonction publique tel quil a t modifi et complt.\n" + "- Vu la demande prsente par lintresse. "; text3 = " Je soussign, Belahsen Youness, Doyen de la Facult Polydisciplinaire de Ouarzazate atteste que Monsieur: \n" + " - NOM : \n" + " - PRENOM : \n" + " - CIN : \n" + " - D.R.P.P : \n" + " - GRADE : \n" + " \n" + "Exerce la fonction denseignant chercheur au sein de notre tablissement depuis le.\n" + "\n" + " Cette attestation est dlivre lintress(e) pour servir et valoir ce que de droit.\n\n"; 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(); jButton2 = 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("A Travail Employe"); jButton1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jButton1MouseClicked(evt); } }); jButton2.setText("Conjee"); jButton2.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jButton2MouseClicked(evt); } }); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("conjee exp"); jButton3.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jButton3MouseClicked(evt); } }); jButton4.setText("A Travail PRof"); jButton4.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jButton4MouseClicked(evt); } }); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); jButton5.setText("Quitter territoire"); jButton5.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jButton5MouseClicked(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(33, 33, 33) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap(250, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jButton4).addGap(18, 18, 18) .addComponent(jButton1).addGap(18, 18, 18).addComponent(jButton2).addGap(18, 18, 18) .addComponent(jButton3).addGap(18, 18, 18).addComponent(jButton5) .addContainerGap(102, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked openPDF(); }//GEN-LAST:event_jButton1MouseClicked private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked conje(); }//GEN-LAST:event_jButton2MouseClicked private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jButton2ActionPerformed private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked conje_exp(); }//GEN-LAST:event_jButton3MouseClicked private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jButton4ActionPerformed private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton4MouseClicked attestation_travail_prof(); }//GEN-LAST:event_jButton4MouseClicked private void jButton5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton5MouseClicked quitter_territoire(); }//GEN-LAST:event_jButton5MouseClicked public void imagee(Document document) throws Exception { URL imageUrl = getClass().getProtectionDomain().getCodeSource().getLocation(); System.out.println(imageUrl); Image image = Image.getInstance(imageUrl + "logo.png"); image.scaleAbsolute(224, 70); image.setAlignment(Image.MIDDLE); document.add(image); } public void openPDF() { Document document = new Document(PageSize.A4); try { PdfWriter.getInstance(document, new FileOutputStream("imprimer test.pdf")); document.open(); imagee(document); SimpleDateFormat td = new SimpleDateFormat("dd-MM-yyyy"); java.util.Date now = new java.util.Date(); String tdnow = td.format(now); Paragraph pa = new Paragraph("Attestation de travail", FontFactory.getFont(FontFactory.TIMES, 30, Font.UNDERLINE, BaseColor.DARK_GRAY)); pa.setAlignment(Paragraph.ALIGN_CENTER); document.add(pa); for (int i = 0; i < 3; i++) { document.add(new Paragraph(" ")); } Paragraph p = new Paragraph(text, FontFactory.getFont(FontFactory.TIMES, 16, Font.NORMAL, BaseColor.DARK_GRAY)); document.add(p); Paragraph time = new Paragraph(" Fait Ouarzazate le : " + tdnow, FontFactory.getFont(FontFactory.TIMES, 17, Font.BOLD, BaseColor.DARK_GRAY)); Paragraph a = new Paragraph(""); Paragraph si = new Paragraph(" Signature:", FontFactory.getFont(FontFactory.TIMES, 17, Font.BOLD, BaseColor.DARK_GRAY)); time.setAlignment(Paragraph.ALIGN_CENTER); si.setAlignment(Paragraph.ALIGN_CENTER); document.add(time); document.add(si); document.add(a); document.close(); System.out.println("date2"); /* Open Pdf */ Desktop desktop = Desktop.getDesktop(); if (desktop.isSupported(Desktop.Action.OPEN)) { desktop.open(new File("imprimer test.pdf")); } else { System.out.println("Open is not supported"); } System.out.println("date"); } catch (Exception ex) { System.out.println("qsdqs"); } } public void attestation_travail_prof() { Document document = new Document(PageSize.A4); try { PdfWriter.getInstance(document, new FileOutputStream("Attestation de travail Professeur.pdf")); document.open(); imagee(document); SimpleDateFormat td = new SimpleDateFormat("dd-MM-yyyy"); java.util.Date now = new java.util.Date(); String tdnow = td.format(now); Paragraph pa = new Paragraph("Attestation de travail", FontFactory.getFont(FontFactory.TIMES, 30, Font.UNDERLINE, BaseColor.DARK_GRAY)); pa.setAlignment(Paragraph.ALIGN_CENTER); document.add(pa); for (int i = 0; i < 3; i++) { document.add(new Paragraph(" ")); } Paragraph p = new Paragraph(text3, FontFactory.getFont(FontFactory.TIMES, 16, Font.NORMAL, BaseColor.DARK_GRAY)); document.add(p); Paragraph time = new Paragraph(" Fait Ouarzazate le : " + tdnow, FontFactory.getFont(FontFactory.TIMES, 17, Font.BOLD, BaseColor.DARK_GRAY)); Paragraph a = new Paragraph(""); Paragraph si = new Paragraph(" Signature:", FontFactory.getFont(FontFactory.TIMES, 17, Font.BOLD, BaseColor.DARK_GRAY)); time.setAlignment(Paragraph.ALIGN_CENTER); si.setAlignment(Paragraph.ALIGN_CENTER); document.add(time); document.add(si); document.add(a); document.close(); System.out.println("date2"); /* Open Pdf */ Desktop desktop = Desktop.getDesktop(); if (desktop.isSupported(Desktop.Action.OPEN)) { desktop.open(new File("Attestation de travail Professeur.pdf")); } else { System.out.println("Open is not supported"); } System.out.println("date"); } catch (Exception ex) { System.out.println("qsdqs"); } } public void conje() { Document document = new Document(PageSize.A4); try { PdfWriter.getInstance(document, new FileOutputStream("conjee.pdf")); document.open(); imagee(document); SimpleDateFormat td = new SimpleDateFormat("dd-MM-yyyy"); java.util.Date now = new java.util.Date(); String tdnow = td.format(now); Paragraph pa = new Paragraph("Dcision de cong", FontFactory.getFont(FontFactory.TIMES, 30, Font.UNDERLINE, BaseColor.DARK_GRAY)); pa.setAlignment(Paragraph.ALIGN_CENTER); document.add(pa); for (int i = 0; i < 4; i++) { document.add(new Paragraph(" ")); } Paragraph p = new Paragraph(text2, FontFactory.getFont(FontFactory.TIMES, 14, Font.NORMAL, BaseColor.DARK_GRAY)); document.add(p); Paragraph p3 = new Paragraph("DECIDE :", FontFactory.getFont(FontFactory.TIMES, 14, Font.BOLD, BaseColor.DARK_GRAY)); p3.setAlignment(Paragraph.ALIGN_CENTER); document.add(p3); Paragraph p4 = new Paragraph("ARTICLE I Un cong dun .", FontFactory.getFont(FontFactory.TIMES, 14, Font.BOLDITALIC, BaseColor.DARK_GRAY)); document.add(p4); Paragraph p5 = new Paragraph( " Est accord : \n" + " C.I.N : \n" + " GRADE : \n" + " Pour la priode : ", FontFactory.getFont(FontFactory.TIMES, 15, Font.NORMAL, BaseColor.DARK_GRAY)); document.add(p5); Paragraph p6 = new Paragraph("ARTICLE II", FontFactory.getFont(FontFactory.TIMES, 16, Font.BOLDITALIC, BaseColor.DARK_GRAY)); document.add(p6); Paragraph p7 = new Paragraph( "Lintress(e) est autoris quitter le territoire national durant la priode indique ci-dessus", FontFactory.getFont(FontFactory.TIMES, 18, Font.NORMAL, BaseColor.DARK_GRAY)); document.add(p7); Paragraph p8 = new Paragraph("ARTICLE III", FontFactory.getFont(FontFactory.TIMES, 16, Font.BOLDITALIC, BaseColor.DARK_GRAY)); document.add(p8); Paragraph p9 = new Paragraph( "Lintress(e) est tenu daviser le service du personnel de sa reprise de travail aprs expiration du cong sus-vis.", FontFactory.getFont(FontFactory.TIMES, 18, Font.NORMAL, BaseColor.DARK_GRAY)); document.add(p9); for (int i = 0; i < 4; i++) { document.add(new Paragraph(" ")); } Paragraph time = new Paragraph( " Cachet et signature Fait Ouarzazate le :" + tdnow + "\n" + "\n", FontFactory.getFont(FontFactory.TIMES, 14, Font.BOLD, BaseColor.DARK_GRAY)); Paragraph si = new Paragraph("Signature du demandeur", FontFactory.getFont(FontFactory.TIMES, 14, Font.BOLD, BaseColor.DARK_GRAY)); time.setAlignment(Paragraph.ALIGN_LEFT); si.setAlignment(Paragraph.ALIGN_RIGHT); document.add(time); document.add(si); document.close(); System.out.println("date2"); /* Open Pdf */ Desktop desktop = Desktop.getDesktop(); if (desktop.isSupported(Desktop.Action.OPEN)) { desktop.open(new File("conjee.pdf")); } else { System.out.println("Open is not supported"); } System.out.println("date"); } catch (Exception ex) { System.out.println("qsdqs"); } } public void conje_exp() { Document document = new Document(PageSize.A4); try { PdfWriter.getInstance(document, new FileOutputStream("conjee_exp.pdf")); document.open(); imagee(document); SimpleDateFormat td = new SimpleDateFormat("dd-MM-yyyy"); java.util.Date now = new java.util.Date(); String tdnow = td.format(now); Paragraph pa = new Paragraph("Dcision de cong", FontFactory.getFont(FontFactory.TIMES, 30, Font.UNDERLINE, BaseColor.DARK_GRAY)); pa.setAlignment(Paragraph.ALIGN_CENTER); document.add(pa); for (int i = 0; i < 4; i++) { document.add(new Paragraph(" ")); } Paragraph p = new Paragraph(text2, FontFactory.getFont(FontFactory.TIMES, 14, Font.NORMAL, BaseColor.DARK_GRAY)); document.add(p); Paragraph p3 = new Paragraph("DECIDE :", FontFactory.getFont(FontFactory.TIMES, 14, Font.BOLD, BaseColor.DARK_GRAY)); p3.setAlignment(Paragraph.ALIGN_CENTER); document.add(p3); Paragraph p4 = new Paragraph("ARTICLE I", FontFactory.getFont(FontFactory.TIMES, 14, Font.BOLDITALIC, BaseColor.DARK_GRAY)); document.add(p4); Paragraph p5 = new Paragraph( " Un cong Exceptionnel dune dure de jours\n" + " Est accord : \n" + " C.I.N : \n" + " GRADE : \n" + " Pour la priode : ", FontFactory.getFont(FontFactory.TIMES, 15, Font.NORMAL, BaseColor.DARK_GRAY)); document.add(p5); Paragraph p6 = new Paragraph("ARTICLE II", FontFactory.getFont(FontFactory.TIMES, 16, Font.BOLDITALIC, BaseColor.DARK_GRAY)); document.add(p6); Paragraph p7 = new Paragraph( "Lintress(e) est autoris quitter le territoire national durant la priode indique ci-dessus", FontFactory.getFont(FontFactory.TIMES, 18, Font.NORMAL, BaseColor.DARK_GRAY)); document.add(p7); Paragraph p8 = new Paragraph("ARTICLE III", FontFactory.getFont(FontFactory.TIMES, 16, Font.BOLDITALIC, BaseColor.DARK_GRAY)); document.add(p8); Paragraph p9 = new Paragraph( "Lintress(e) est tenu daviser le service du personnel de sa reprise de travail aprs expiration du cong sus-vis.", FontFactory.getFont(FontFactory.TIMES, 18, Font.NORMAL, BaseColor.DARK_GRAY)); document.add(p9); for (int i = 0; i < 4; i++) { document.add(new Paragraph(" ")); } Paragraph time = new Paragraph( " Cachet et signature Fait Ouarzazate le :" + tdnow + "\n" + "\n", FontFactory.getFont(FontFactory.TIMES, 14, Font.BOLD, BaseColor.DARK_GRAY)); Paragraph si = new Paragraph("Signature du demandeur", FontFactory.getFont(FontFactory.TIMES, 14, Font.BOLD, BaseColor.DARK_GRAY)); time.setAlignment(Paragraph.ALIGN_LEFT); si.setAlignment(Paragraph.ALIGN_RIGHT); document.add(time); document.add(si); document.close(); System.out.println("date2"); /* Open Pdf */ Desktop desktop = Desktop.getDesktop(); if (desktop.isSupported(Desktop.Action.OPEN)) { desktop.open(new File("conjee_exp.pdf")); } else { System.out.println("Open is not supported"); } System.out.println("date"); } catch (Exception ex) { System.out.println("qsdqs"); } } public void quitter_territoire() { Document document = new Document(PageSize.A4); try { PdfWriter.getInstance(document, new FileOutputStream("quitter territoire.pdf")); document.open(); imagee(document); document.open(); imagee(document); SimpleDateFormat td = new SimpleDateFormat("dd-MM-yyyy"); java.util.Date now = new java.util.Date(); String tdnow = td.format(now); Paragraph pa = new Paragraph("Autorisation de quitter le territoire", FontFactory.getFont(FontFactory.TIMES, 30, Font.UNDERLINE, BaseColor.DARK_GRAY)); pa.setAlignment(Paragraph.ALIGN_CENTER); document.add(pa); for (int i = 0; i < 4; i++) { document.add(new Paragraph(" ")); } // cell = new PdfPCell(new Phrase("Fusion de 2 cellules de la premire colonne")); // cell.setRowspan(2); // table.addCell(cell); PdfPTable table = new PdfPTable(2); PdfPCell cell = new PdfPCell(); //contenu du tableau. table.addCell("\nNom:\n\n"); table.addCell("\n\n"); table.addCell("\nPrenom:\n\n"); table.addCell("\n\n"); table.addCell("\nGRADE :\n\n"); table.addCell("\n\n"); table.addCell("\nADRESSE PERSONNELLE :\n\n"); table.addCell("\n\n"); table.addCell("\nAFFECTATION :\n\n"); table.addCell("\n\n"); cell = new PdfPCell( new Phrase("\nJai lhonneur de solliciter une autorisation dabsence.\n\n")); cell.setColspan(2); table.addCell(cell); table.addCell("\nDU:\n\n"); table.addCell("\n\n"); table.addCell("\nAU:\n\n"); table.addCell("\n\n"); table.addCell("\nMOTIF :\n\n"); table.addCell("\n\n"); table.addCell("\nAVIS DU CHEF \nDETABLISSEMENT:\n\n"); table.addCell("\n\nFAVORABLE.def\n\n"); cell = new PdfPCell(new Phrase("\nFait Ouarzazate le :" + tdnow + "\n\n")); cell.setColspan(2); table.addCell(cell); table.addCell("Signature du demandeur\n\n\n\n\n\n\n\n\n"); table.addCell("Cachet et signature\n\n\n\n\n\n\n\n"); document.add(table); document.close(); Desktop desktop = Desktop.getDesktop(); if (desktop.isSupported(Desktop.Action.OPEN)) { desktop.open(new File("quitter territoire.pdf")); } else { System.out.println("Open is not supported"); } } catch (Exception ex) { System.out.print("catch"); } } 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(imprimer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(imprimer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(imprimer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(imprimer.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 imprimer().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JButton jButton5; // End of variables declaration//GEN-END:variables }