Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package iTextExamples;
  2.  
  3. import com.itextpdf.text.Anchor;
  4. import com.itextpdf.text.Chunk;
  5. import com.itextpdf.text.Document;
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.Element;
  8. import com.itextpdf.text.PageSize;
  9. import com.itextpdf.text.Paragraph;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13.  
  14. import java.awt.Color;
  15. import java.awt.Dimension;
  16. import java.awt.Font;
  17. import java.awt.GridLayout;
  18. import java.awt.HeadlessException;
  19. import java.awt.event.ActionEvent;
  20. import java.awt.event.ActionListener;
  21. import java.io.FileNotFoundException;
  22. import java.io.FileOutputStream;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25.  
  26. import javax.swing.JButton;
  27. import javax.swing.JFrame;
  28. import javax.swing.JLabel;
  29. import javax.swing.JOptionPane;
  30. import javax.swing.JPanel;
  31. import javax.swing.JTextField;
  32. import javax.swing.SwingUtilities;
  33. import javax.swing.UIManager;
  34. import javax.swing.UIManager.LookAndFeelInfo;
  35. import javax.swing.UnsupportedLookAndFeelException;
  36.  
  37. /**
  38.  *
  39.  * @author psychocoder
  40.  */
  41. public class FormReportGenPDF extends JFrame implements ActionListener {
  42.  
  43.     private static final long serialVersionUID = 5754591281120525041L;
  44.     private JPanel panel = null;
  45.     private JLabel fname = null, lname = null, email = null;
  46.     private JTextField fnameField = null, lnameField = null, emailField = null;
  47.     private JButton genPdf = null;
  48.  
  49.     private final int WIDTH = 400;
  50.     private final int HEIGHT = 300;
  51.  
  52.     public FormReportGenPDF(String title) throws HeadlessException {
  53.         super(title);
  54.         initializeVariables();
  55.     }
  56.  
  57.     public FormReportGenPDF() throws HeadlessException {
  58.         setTitle("Form Report Generation in PDF");
  59.         initializeVariables();
  60.     }
  61.  
  62.     private void initializeVariables() {
  63.         panel = new JPanel(new GridLayout(8, 8, 5, 5));
  64.  
  65.         fname = new JLabel("First Name");
  66.         lname = new JLabel("Last Name");
  67.         email = new JLabel("Email");
  68.  
  69.         fnameField = new JTextField();
  70.         lnameField = new JTextField();
  71.         emailField = new JTextField();
  72.  
  73.         genPdf = new JButton("Create Pdf");
  74.  
  75.         initGui();
  76.     }
  77.  
  78.     private void initGui() {
  79.         setSize(new Dimension(WIDTH, HEIGHT));
  80.         this.getInsets().set(10, 10, 10, 10);
  81.         setResizable(false);
  82.         setLocationRelativeTo(null);
  83.         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  84.         setContentPane(panel);
  85.  
  86.         panel.setBackground(Color.white);
  87.  
  88.         fname.setFont(new Font("Arial", Font.BOLD, 14));
  89.         panel.add(fname);
  90.         panel.add(fnameField);
  91.  
  92.         lname.setFont(new Font("Arial", Font.BOLD, 14));
  93.         panel.add(lname);
  94.         panel.add(lnameField);
  95.  
  96.         email.setFont(new Font("Arial", Font.BOLD, 14));
  97.         panel.add(email);
  98.         panel.add(emailField);
  99.  
  100.         panel.add(genPdf);
  101.         genPdf.addActionListener(this);
  102.  
  103.     }
  104.  
  105.     public static void main(String... string) {
  106.         for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
  107.             if ("Nimbus".equals(info.getName())) {
  108.                 try {
  109.                     UIManager.setLookAndFeel(info.getClassName());
  110.                     break;
  111.                 } catch (ClassNotFoundException | InstantiationException
  112.                         | IllegalAccessException
  113.                         | UnsupportedLookAndFeelException ex) {
  114.                     Logger.getLogger(FormReportGenPDF.class.getName()).log(
  115.                             Level.SEVERE, null, ex);
  116.                 }
  117.             }
  118.         }
  119.  
  120.         SwingUtilities.invokeLater(new Runnable() {
  121.  
  122.             @Override
  123.             public void run() {
  124.                 new FormReportGenPDF().setVisible(true);
  125.             }
  126.         });
  127.  
  128.     }
  129.  
  130.     private void processPdfDetails() {
  131.         String fna = fnameField.getText();
  132.         String lna = lnameField.getText();
  133.         String mail = emailField.getText();
  134.  
  135.         if (!"".equals(fna.trim()) && !"".equals(lna.trim())
  136.                 && !"".equals(mail.trim())) {
  137.             Document pdfDoc = new Document();
  138.  
  139.             pdfDoc.setPageSize(PageSize.A5);
  140.             pdfDoc.setMargins(40, 40, 40, 40);
  141.             pdfDoc.setMarginMirroring(true);
  142.  
  143.             try {
  144.                 PdfWriter.getInstance(pdfDoc, new FileOutputStream(
  145.                         "CustomerReport.pdf"));
  146.  
  147.                 pdfDoc.open();
  148.  
  149.                 com.itextpdf.text.Font font = new com.itextpdf.text.Font(
  150.                         com.itextpdf.text.Font.FontFamily.TIMES_ROMAN, 24);
  151.  
  152.                 com.itextpdf.text.Font font1 = new com.itextpdf.text.Font(
  153.                         com.itextpdf.text.Font.FontFamily.TIMES_ROMAN, 15);
  154.  
  155.                 pdfDoc.add(new Paragraph(fna + " " + lna + "'s Report", font));
  156.                 pdfDoc.add(Chunk.NEWLINE);
  157.                 pdfDoc.add(new FormParagraph("First Name : "
  158.                         + new Chunk("\t\t" + fna, font1)));
  159.                 pdfDoc.add(new FormParagraph("Last Name  : "
  160.                         + new Chunk("\t\t" + lna, font1)));
  161.                 pdfDoc.add(new FormParagraph("E-Mail     : " + mail));
  162.  
  163.                 pdfDoc.addAuthor("Animesh Shaw");
  164.                 pdfDoc.addCreationDate();
  165.                 pdfDoc.addTitle("Customer Report of " + fna + " " + lna);
  166.  
  167.                 pdfDoc.close();
  168.  
  169.             } catch (DocumentException | FileNotFoundException e) {
  170.                 e.printStackTrace();
  171.             }
  172.         }
  173.     }
  174.  
  175.     private final class FormParagraph extends Paragraph {
  176.  
  177.         private static final long serialVersionUID = 1L;
  178.  
  179.         public FormParagraph(String string) {
  180.             super(string);
  181.             initParams();
  182.         }
  183.  
  184.         private void initParams() {
  185.             setSpacingAfter(10);
  186.             setIndentationLeft(50);
  187.             setAlignment(Element.ALIGN_JUSTIFIED);
  188.         }
  189.  
  190.     }
  191.  
  192.     @Override
  193.     public void actionPerformed(ActionEvent evt) {
  194.         if (evt.getSource() == genPdf) {
  195.             processPdfDetails();
  196.         } else {
  197.             JOptionPane.showMessageDialog(null, "Required Fields are empty. "
  198.                     + "Please Fill them!", "Empty Fields",
  199.                     JOptionPane.ERROR_MESSAGE);
  200.         }
  201.     }
  202.  
  203. }