Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package iTextExamples;
- import com.itextpdf.text.Anchor;
- import com.itextpdf.text.Chunk;
- import com.itextpdf.text.Document;
- import com.itextpdf.text.DocumentException;
- import com.itextpdf.text.Element;
- 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.PdfWriter;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.GridLayout;
- import java.awt.HeadlessException;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.SwingUtilities;
- import javax.swing.UIManager;
- import javax.swing.UIManager.LookAndFeelInfo;
- import javax.swing.UnsupportedLookAndFeelException;
- /**
- *
- * @author psychocoder
- */
- public class FormReportGenPDF extends JFrame implements ActionListener {
- private static final long serialVersionUID = 5754591281120525041L;
- private JPanel panel = null;
- private JLabel fname = null, lname = null, email = null;
- private JTextField fnameField = null, lnameField = null, emailField = null;
- private JButton genPdf = null;
- private final int WIDTH = 400;
- private final int HEIGHT = 300;
- public FormReportGenPDF(String title) throws HeadlessException {
- super(title);
- initializeVariables();
- }
- public FormReportGenPDF() throws HeadlessException {
- setTitle("Form Report Generation in PDF");
- initializeVariables();
- }
- private void initializeVariables() {
- panel = new JPanel(new GridLayout(8, 8, 5, 5));
- fname = new JLabel("First Name");
- lname = new JLabel("Last Name");
- email = new JLabel("Email");
- fnameField = new JTextField();
- lnameField = new JTextField();
- emailField = new JTextField();
- genPdf = new JButton("Create Pdf");
- initGui();
- }
- private void initGui() {
- setSize(new Dimension(WIDTH, HEIGHT));
- this.getInsets().set(10, 10, 10, 10);
- setResizable(false);
- setLocationRelativeTo(null);
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- setContentPane(panel);
- panel.setBackground(Color.white);
- fname.setFont(new Font("Arial", Font.BOLD, 14));
- panel.add(fname);
- panel.add(fnameField);
- lname.setFont(new Font("Arial", Font.BOLD, 14));
- panel.add(lname);
- panel.add(lnameField);
- email.setFont(new Font("Arial", Font.BOLD, 14));
- panel.add(email);
- panel.add(emailField);
- panel.add(genPdf);
- genPdf.addActionListener(this);
- }
- public static void main(String... string) {
- for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- try {
- UIManager.setLookAndFeel(info.getClassName());
- break;
- } catch (ClassNotFoundException | InstantiationException
- | IllegalAccessException
- | UnsupportedLookAndFeelException ex) {
- Logger.getLogger(FormReportGenPDF.class.getName()).log(
- Level.SEVERE, null, ex);
- }
- }
- }
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- new FormReportGenPDF().setVisible(true);
- }
- });
- }
- private void processPdfDetails() {
- String fna = fnameField.getText();
- String lna = lnameField.getText();
- String mail = emailField.getText();
- if (!"".equals(fna.trim()) && !"".equals(lna.trim())
- && !"".equals(mail.trim())) {
- Document pdfDoc = new Document();
- pdfDoc.setPageSize(PageSize.A5);
- pdfDoc.setMargins(40, 40, 40, 40);
- pdfDoc.setMarginMirroring(true);
- try {
- PdfWriter.getInstance(pdfDoc, new FileOutputStream(
- "CustomerReport.pdf"));
- pdfDoc.open();
- com.itextpdf.text.Font font = new com.itextpdf.text.Font(
- com.itextpdf.text.Font.FontFamily.TIMES_ROMAN, 24);
- com.itextpdf.text.Font font1 = new com.itextpdf.text.Font(
- com.itextpdf.text.Font.FontFamily.TIMES_ROMAN, 15);
- pdfDoc.add(new Paragraph(fna + " " + lna + "'s Report", font));
- pdfDoc.add(Chunk.NEWLINE);
- pdfDoc.add(new FormParagraph("First Name : "
- + new Chunk("\t\t" + fna, font1)));
- pdfDoc.add(new FormParagraph("Last Name : "
- + new Chunk("\t\t" + lna, font1)));
- pdfDoc.add(new FormParagraph("E-Mail : " + mail));
- pdfDoc.addAuthor("Animesh Shaw");
- pdfDoc.addCreationDate();
- pdfDoc.addTitle("Customer Report of " + fna + " " + lna);
- pdfDoc.close();
- } catch (DocumentException | FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
- private final class FormParagraph extends Paragraph {
- private static final long serialVersionUID = 1L;
- public FormParagraph(String string) {
- super(string);
- initParams();
- }
- private void initParams() {
- setSpacingAfter(10);
- setIndentationLeft(50);
- setAlignment(Element.ALIGN_JUSTIFIED);
- }
- }
- @Override
- public void actionPerformed(ActionEvent evt) {
- if (evt.getSource() == genPdf) {
- processPdfDetails();
- } else {
- JOptionPane.showMessageDialog(null, "Required Fields are empty. "
- + "Please Fill them!", "Empty Fields",
- JOptionPane.ERROR_MESSAGE);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement