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

早过忘川 提交于 2019-12-13 08:16:18

问题


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?


回答1:


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

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

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:

When I open this file, I see:

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);
    }
}


来源:https://stackoverflow.com/questions/35669782/after-generating-pdf-file-how-to-save-dynamic-name-for-this-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!