Search code examples
javaswingpdfitextpdf

After generating pdf file how to save dynamic name for this pdf?


I generate a PDF document using a file path to create a PDF file with name test.PDF. However, I want to chance this so that the user can choose a name and that this name is used at the time of PDF generation. I am using iText to creating a PDF file like this.

private String FILE = "e://test.PDF";
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
// add content
document.close();

How do I change this so that the file is saved using the file name chosen by the end user?


Solution

  • I have written this proof of concept and it works exactly as expected. When you run it, a JFrame opens:

    enter image description here

    The JFrame consists of a JButton with text Push ATUL, push! When you click this button a dialog opens:

    enter image description here

    I select a folder (test) and I choose a file name (test.pdf). Then I click Save. This is what shows up in my folder:

    enter image description here

    When I open this file, I see:

    enter image description here

    This is the full code of the example:

    /*
     * Example written in answer to:
     * http://stackoverflow.com/questions/35669782/
     */
    package sandbox.objects;
    
    import com.itextpdf.text.Document;
    import com.itextpdf.text.DocumentException;
    import com.itextpdf.text.Paragraph;
    import com.itextpdf.text.pdf.PdfWriter;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.WindowConstants;
    
    /**
     * @author Bruno Lowagie (iText Software)
     */
    public class PdfOnButtonClick {
    
        public class PdfActionListener implements ActionListener {
    
            public void actionPerformed(ActionEvent e) {
                JFileChooser dialog = new JFileChooser();
                int dialogResult = dialog.showSaveDialog(null);
                if (dialogResult==JFileChooser.APPROVE_OPTION){
                    String filePath = dialog.getSelectedFile().getPath();
                    try {
                        Document document = new Document();
                        PdfWriter.getInstance(document, new FileOutputStream(filePath));
                        document.open();
                        document.add(new Paragraph("File with path " + filePath));
                        document.close();
                    }
                    catch(DocumentException de) {
                        de.printStackTrace();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setSize(300, 300);
            frame.setTitle("ATUL doesn't know how to code");
            frame.setResizable(true);
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            JButton button = new JButton("Push ATUL, push!");
            button.addActionListener(new PdfOnButtonClick().new PdfActionListener());
            frame.getContentPane().add(button);
            frame.setVisible(true);
        }
    }