0

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?

Jongware
  • 22,200
  • 8
  • 54
  • 100
ATUL
  • 25
  • 1
  • 6
  • 1
    I think by 'dynamic file' and Swing you should maybe look at `JFileChooser` but I really don't understand the sentence before the question.. Could you rephrase what the requirement is? – Andrew Thompson Feb 27 '16 at 15:15
  • I fixed the code that would never compile. I also corrected your English. I removed a very strange sentence about `JFileChooser` that said: *so this point of you i am using j file chooser for generation of user defined name PDF.* Your English is poor, but if I understand correctly, you say that you are using `JFileChooser`. This is not reflected in your code. Have you tried the code I shared in my answer? What is wrong with it? Why was it down-voted? Please explain. – Bruno Lowagie Feb 28 '16 at 12:33
  • You have just made your question even worse. Now nobody has a clue what you're talking about! Read [the documentation](https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html) and explain your problem with `JFileChooser`. Maybe your problem isn't even about the file chooser. Maybe you don't know how to add an `ActionListener` to a `JButton` (http://stackoverflow.com/questions/16351875/jfilechooser-on-a-button-click). In any case, your question still doesn't deserve a vote to be reopened. Your edit *confuses*, you need to *clarify*!!! – Bruno Lowagie Feb 28 '16 at 12:45
  • PdfWriter.getInstance(document, new FileOutputStream("F:/"+ "filename"+(new Date().getTime()/1000)+".pdf")); – Fakipo Jul 03 '20 at 17:00

1 Answers1

1

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);
    }
}
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 1
    @ATUL You should learn the principle of [Occam's Razor](https://en.wikipedia.org/wiki/Occam%27s_razor). You should simplify your question and only talk about the essence of the problem. You have tried my example and you've seen that it works. If this doesn't work in your code at your office, then **your code at your office has a bug.** Your code at your office has a bug **because you didn't follow the instructions correctly.** Nobody can tell you what you did wrong, **because you're not showing your code!** You make people angry at you by saying *you can't understand my problem.* – Bruno Lowagie Feb 28 '16 at 14:02
  • 1
    If this answer was helpful, please accept the answer (check the checkmark). Right now, the answer is downvoted, which means that other people might think it's a bad answer. If you have a new question, please post a new question. Note that the new question isn't really about iText. It's about `JFileChooser` and file permissions: should the system allow overwriting existing documents or not? – Bruno Lowagie Feb 29 '16 at 18:29
  • one more question related to this code how to check when we first time generate pdf using filechooser it is z:\\test.pdf but when we again try to generate the same name they show error like name is already generated please choose another name. how can we do this type of problem?? – ATUL Mar 01 '16 at 13:42
  • 1
    @ATUL Please post this as a separate question. Do not use the comment section to ask a new question. – Bruno Lowagie Mar 01 '16 at 15:03
  • they show error message.You have reached your question limit You've asked 3 questions recently, some of which have not been received very well by the community. Everyone learns at their own pace, and it’s okay to make some mistakes. However, the reception your questions have received thus far might ultimately block your account from asking questions entirely. It's been 3 days since you asked your last question. We ask that you wait 1 days before asking again. Use this time to revisit your previous questions, editing to address any issues that folks have pointed out in comments. – ATUL Mar 01 '16 at 16:17
  • 1
    @ATUL I only see 1 question, so I guess you've asked 2 other questions that were so bad that they were deleted. As a penalty, you have to wait one more day. So be it. I tried to help you by editing this question. You refused that help by making your question worse (that wasn't appreciated). I advised you to use [Occam's razor](https://en.wikipedia.org/wiki/Occam%27s_razor) to clarify, but you did the exact opposite. You introduced plenty of irrelevant information confusing the reader, after which Rad Lexus reverted the question to my version. That's how SO works. – Bruno Lowagie Mar 01 '16 at 16:29
  • 1
    If you still don't understand why people with your attitude get these kind of messages, please read [The Hitchhiker’s Guide to StackOverflow](http://blog.safedk.com/technology/technologythe-hitchhikers-guide-to-stackoverflow/). I have been on SO for a long time, nevertheless I still get frustrated by people like you. I even [tweeted about you](https://twitter.com/bruno1970/status/703944654463623168). When you are asking for help, you shouldn't annoy the people who are willing to help you. Don't you know the saying *Don't bite the hand that feeds you?* – Bruno Lowagie Mar 01 '16 at 16:34