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 edu.esprit.pi.gui.internalframes; import com.itextpdf.text.Anchor; import com.itextpdf.text.BadElementException; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Chapter; 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 edu.esprit.pi.dao.ArticleDAO; import edu.esprit.pi.entites.Article; import java.io.FileOutputStream; import java.util.Date; import javax.swing.JOptionPane; /** * * @author Amine */ public class PDFwithItextInternalFrame extends javax.swing.JInternalFrame { private Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD); private Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED); private Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD); private Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD); /** * Creates new form PDFwithItextInternalFrame */ public PDFwithItextInternalFrame() { 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() { jPanel2 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); titrePDFjTextField = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); subjectjTextField = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); keywordjTextField = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); autheurjTextField = new javax.swing.JTextField(); jLabel5 = new javax.swing.JLabel(); creatorjTextField = new javax.swing.JTextField(); jPanel3 = new javax.swing.JPanel(); jLabel6 = new javax.swing.JLabel(); titreDocumentTextField = new javax.swing.JTextField(); jLabel7 = new javax.swing.JLabel(); sousTitrejTextField = new javax.swing.JTextField(); jLabel8 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); descriptionTitlejTextArea = new javax.swing.JTextArea(); jPanel4 = new javax.swing.JPanel(); jPanel1 = new javax.swing.JPanel(); jLabel9 = new javax.swing.JLabel(); jLabel10 = new javax.swing.JLabel(); titreChapitre1jTextField = new javax.swing.JTextField(); titreParagraphe1TextField = new javax.swing.JTextField(); jLabel11 = new javax.swing.JLabel(); jScrollPane2 = new javax.swing.JScrollPane(); descriptionjTextArea = new javax.swing.JTextArea(); jLabel15 = new javax.swing.JLabel(); jScrollPane4 = new javax.swing.JScrollPane(); description3jTextArea = new javax.swing.JTextArea(); jPanel6 = new javax.swing.JPanel(); jLabel12 = new javax.swing.JLabel(); jLabel13 = new javax.swing.JLabel(); jLabel14 = new javax.swing.JLabel(); titreChapitre2jTextField = new javax.swing.JTextField(); titreParagraphe2jTextField = new javax.swing.JTextField(); jScrollPane3 = new javax.swing.JScrollPane(); description2jTextArea = new javax.swing.JTextArea(); jScrollPane5 = new javax.swing.JScrollPane(); description4jTextArea = new javax.swing.JTextArea(); jLabel16 = new javax.swing.JLabel(); jPanel5 = new javax.swing.JPanel(); jLabel17 = new javax.swing.JLabel(); jLabel18 = new javax.swing.JLabel(); generatePDFjButton = new javax.swing.JButton(); setClosable(true); setTitle("Gnrer PDF with iText"); jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder( javax.swing.BorderFactory.createEtchedBorder(), "Partie optionnelle (Meta data du PDF)")); jLabel1.setText("- Titre du PDF"); titrePDFjTextField.setText("MonpremierPDF"); jLabel2.setText("- Subject "); subjectjTextField.setText("Using iText"); jLabel3.setText("- Mots cls"); keywordjTextField.setText("pdf, itext"); jLabel4.setText("- Autheur "); autheurjTextField.setText("Amine"); jLabel5.setText("- Creator"); creatorjTextField.setText("Amine inspired by Vogella"); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1).addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(titrePDFjTextField).addComponent(subjectjTextField))) .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jLabel3).addGap(18, 18, 18) .addComponent(keywordjTextField)) .addGroup(jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4).addComponent(jLabel5)) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(18, 18, 18) .addComponent(creatorjTextField)) .addGroup(jPanel2Layout.createSequentialGroup().addGap(20, 20, 20) .addComponent(autheurjTextField))))); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(titrePDFjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(subjectjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(keywordjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(autheurjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(creatorjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5)) .addGap(306, 306, 306))); jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Titre du document")); jLabel6.setText(" - Titre "); titreDocumentTextField.setText("Gestion des projets Esprit1"); jLabel7.setText(" - Sous Titre"); sousTitrejTextField.setText("Ce document dcrit comment sont grs les projets Esprit "); jLabel8.setText(" - Description "); descriptionTitlejTextArea.setColumns(20); descriptionTitlejTextArea.setRows(5); descriptionTitlejTextArea.setText( "C'est une version prliminaire du Document, \ntoute autres modifications sera sous vos responsabilits. \n MAK ;-)."); jScrollPane1.setViewportView(descriptionTitlejTextArea); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addComponent(jLabel8).addGap(0, 0, Short.MAX_VALUE)) .addGroup(jPanel3Layout.createSequentialGroup() .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel7).addComponent(jLabel6)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(titreDocumentTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE) .addComponent(sousTitrejTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 1, Short.MAX_VALUE))) .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6).addComponent(titreDocumentTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel7).addComponent(sousTitrejTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel8) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(20, 20, 20))); jPanel4.setBorder(javax.swing.BorderFactory .createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Contenu du Document")); jLabel9.setText("- Titre du chapitre "); jLabel10.setText("- Titre paragraphe"); titreChapitre1jTextField.setText("Premier Chapitre"); titreParagraphe1TextField.setText("Projet Intgration"); jLabel11.setText("- Description"); descriptionjTextArea.setColumns(20); descriptionjTextArea.setRows(5); descriptionjTextArea.setText( "On commence par le Sprint 1 qui est en Java,\nil a une dure de 4 semaines.\nLe Sprint 2 est J2ME, sa dure est de 2 semaines.\nEnfin le Sprint 3 qui est le web dure encore 4 semaines."); jScrollPane2.setViewportView(descriptionjTextArea); jLabel15.setText("- Descripton "); description3jTextArea.setColumns(20); description3jTextArea.setRows(5); description3jTextArea.setText("Exemple mtier pour le remplissage \nd'une liste"); jScrollPane4.setViewportView(description3jTextArea); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel9) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(titreChapitre1jTextField)) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel10) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(titreParagraphe1TextField)) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel11) .addGap(32, 32, 32).addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel15) .addGap(31, 31, 31).addComponent(jScrollPane4))) .addContainerGap())); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel9) .addComponent(titreChapitre1jTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel10).addComponent(titreParagraphe1TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel11)) .addGap(9, 9, 9) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel15).addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(25, Short.MAX_VALUE))); jLabel12.setText("- Titre du chapitre "); jLabel13.setText("- Titre paragraphe"); jLabel14.setText("- Description"); titreChapitre2jTextField.setText("Deuxime Chapitre"); titreParagraphe2jTextField.setText("Validation du projet d'intgration"); description2jTextArea.setColumns(20); description2jTextArea.setRows(5); description2jTextArea.setText("Validation du projet \nd'intgration"); jScrollPane3.setViewportView(description2jTextArea); javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6); jPanel6.setLayout(jPanel6Layout); jPanel6Layout.setHorizontalGroup(jPanel6Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel6Layout.createSequentialGroup().addContainerGap().addGroup(jPanel6Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel6Layout.createSequentialGroup().addComponent(jLabel12) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(titreChapitre2jTextField)) .addGroup(jPanel6Layout.createSequentialGroup() .addGroup( jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel13).addComponent(jLabel14)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel6Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel6Layout.createSequentialGroup().addGap(10, 10, 10) .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)) .addComponent(titreParagraphe2jTextField)))) .addContainerGap())); jPanel6Layout.setVerticalGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel6Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel12).addComponent(titreChapitre2jTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel13).addComponent(titreParagraphe2jTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel14).addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); description4jTextArea.setColumns(20); description4jTextArea.setRows(5); description4jTextArea.setText("Remplissage dynamique"); jScrollPane5.setViewportView(description4jTextArea); jLabel16.setText("- Description"); jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Gnrer un Tableau")); jLabel17.setText( "- Reagarder la mthode createTable(Section subCatPart) pour voir comment crer et remplir un "); jLabel18.setText("tableau."); javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5); jPanel5.setLayout(jPanel5Layout); jPanel5Layout.setHorizontalGroup(jPanel5Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup().addComponent(jLabel18).addGap(410, 410, 410)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout .createSequentialGroup().addComponent(jLabel17).addContainerGap())))); jPanel5Layout.setVerticalGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup().addComponent(jLabel17) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE) .addComponent(jLabel18))); generatePDFjButton.setText("Gnrer PDF"); generatePDFjButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { generatePDFjButtonActionPerformed(evt); } }); javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel4Layout.createSequentialGroup().addGap(10, 10, 10) .addComponent(jLabel16) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGroup(jPanel4Layout.createSequentialGroup().addContainerGap() .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 478, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(generatePDFjButton))); jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel16).addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(generatePDFjButton, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))) .addGap(2, 2, 2))); pack(); }// </editor-fold>//GEN-END:initComponents public void addMetaData(Document document) { document.addTitle(titrePDFjTextField.getText()); document.addSubject(subjectjTextField.getText()); document.addKeywords(keywordjTextField.getText()); document.addAuthor(autheurjTextField.getText()); document.addCreator(creatorjTextField.getText()); } public void addEmptyLine(Paragraph paragraph, int number) { for (int i = 0; i < number; i++) { paragraph.add(new Paragraph(" ")); } } public void createTable(Section subCatPart) throws BadElementException { PdfPTable table = new PdfPTable(3); PdfPCell c1 = new PdfPCell(new Phrase("Client")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Article")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Quantit")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); table.setHeaderRows(1); // Rcupration de la liste des articles. java.util.List<Article> listArticles = new ArticleDAO().findAll(); // remplissare des cellules de la liste for (Article article : listArticles) { table.addCell("" + article.getClient().getNom()); table.addCell("" + article.getStock().getLibelle()); table.addCell("" + article.getQuantite()); } subCatPart.add(table); } public void createList(Section subCatPart) { List list = new List(true, false, 10); list.add(new ListItem("Premier point")); list.add(new ListItem("Deuxime point")); list.add(new ListItem("Troisime point")); subCatPart.add(list); } public void addTitlePage(Document document) throws DocumentException { Paragraph preface = new Paragraph(); // On ajoute une seule ligne vide. addEmptyLine(preface, 1); // Ecrire un titre avec un grand caractre. preface.add(new Paragraph(titreDocumentTextField.getText(), catFont)); addEmptyLine(preface, 1); // Va ajouter une ligne Rapport gnr par : Nom, date preface.add(new Paragraph("Rapport gnr par : " + System.getProperty("user.name") + ", " + new Date(), smallBold)); addEmptyLine(preface, 3); preface.add(new Paragraph(sousTitrejTextField.getText(), smallBold)); addEmptyLine(preface, 8); preface.add(new Paragraph(descriptionTitlejTextArea.getText(), redFont)); document.add(preface); // Commencer une nouvelle page document.newPage(); } public void addContent(Document document) throws DocumentException { Anchor anchor = new Anchor(titreChapitre1jTextField.getText(), catFont); //anchor.setName("First Chapter"); // le socond paramtre est le numro du chapitre. Chapter catPart = new Chapter(new Paragraph(anchor), 1); Paragraph subPara = new Paragraph(titreParagraphe1TextField.getText(), subFont); Section subCatPart = catPart.addSection(subPara); subCatPart.add(new Paragraph(descriptionjTextArea.getText())); // Nouvelle Paragraph subPara2 = new Paragraph(description3jTextArea.getText(), subFont); Section subCatPart2 = catPart.addSection(subPara2); subCatPart2.add(new Paragraph(description4jTextArea.getText())); // Ajouter une liste de sections createList(subCatPart2); Paragraph paragraph = new Paragraph(); addEmptyLine(paragraph, 3); subCatPart2.add(paragraph); // Ajouter un tableau createTable(subCatPart2); // Ajouter tout cela au document. document.add(catPart); // Nouvelle Section anchor = new Anchor(titreChapitre2jTextField.getText(), catFont); //anchor.setName("Second Chapter"); // Le socond paramtre est le numro du chapitre catPart = new Chapter(new Paragraph(anchor), 1); subPara = new Paragraph(titreParagraphe2jTextField.getText(), subFont); subCatPart = catPart.addSection(subPara); subCatPart.add(new Paragraph(description2jTextArea.getText())); // ajouter au document document.add(catPart); } private void generatePDFjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generatePDFjButtonActionPerformed try { String FILE = "D:\\" + titrePDFjTextField.getText() + ".pdf"; Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(FILE)); System.out.println("Ouverture du document."); document.open(); System.out.println("Ajout des mta donnes du document."); addMetaData(document); System.out.println("Ajout du titre du document."); addTitlePage(document); System.out.println("Ajout du contenu du document."); addContent(document); System.out.println("Fermeture du document."); document.close(); JOptionPane.showMessageDialog(this, "Gnration du PDF effectu avec succs : " + FILE); } catch (Exception e) { e.printStackTrace(); } }//GEN-LAST:event_generatePDFjButtonActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextField autheurjTextField; private javax.swing.JTextField creatorjTextField; private javax.swing.JTextArea description2jTextArea; private javax.swing.JTextArea description3jTextArea; private javax.swing.JTextArea description4jTextArea; private javax.swing.JTextArea descriptionTitlejTextArea; private javax.swing.JTextArea descriptionjTextArea; private javax.swing.JButton generatePDFjButton; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel12; private javax.swing.JLabel jLabel13; private javax.swing.JLabel jLabel14; private javax.swing.JLabel jLabel15; private javax.swing.JLabel jLabel16; private javax.swing.JLabel jLabel17; private javax.swing.JLabel jLabel18; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JPanel jPanel5; private javax.swing.JPanel jPanel6; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JScrollPane jScrollPane4; private javax.swing.JScrollPane jScrollPane5; private javax.swing.JTextField keywordjTextField; private javax.swing.JTextField sousTitrejTextField; private javax.swing.JTextField subjectjTextField; private javax.swing.JTextField titreChapitre1jTextField; private javax.swing.JTextField titreChapitre2jTextField; private javax.swing.JTextField titreDocumentTextField; private javax.swing.JTextField titrePDFjTextField; private javax.swing.JTextField titreParagraphe1TextField; private javax.swing.JTextField titreParagraphe2jTextField; // End of variables declaration//GEN-END:variables }