2016-02-27 119 views
-1

我生成使用文件路徑來創建名稱test.PDF PDF文件PDF文檔。但是,我希望這樣做,以便用戶可以選擇一個名稱,並在PDF生成時使用該名稱。我正在使用iText來創建一個像這樣的PDF文件。生成pdf文件後如何保存這個pdf的動態名稱?

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

如何更改此設置,以便使用最終用戶選擇的文件名保存文件?

+1

我想通過「動態文件」和Swing你也許應該看看'JFileChooser'但我真的不明白這個問題之前,這句話..莫非你rephras e要求是什麼? –

+0

我修復了永遠不會編譯的代碼。我也糾正了你的英語。我刪除了關於'JFileChooser'一個很奇怪的句子說:*所以這點你,我使用j個文件選擇用於生成用戶定義名稱的PDF *你的英語不好,但如果我理解正確的話,你說你是。使用'JFileChooser'。這不會反映在您的代碼中。你有沒有試過我在我的答案中分享的代碼?它有什麼問題?爲什麼它被低估?請解釋。 –

+0

你剛剛讓你的問題變得更糟。現在沒人知道你在說什麼!閱讀[文檔](https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html)並用'JFileChooser'解釋你的問題。也許你的問題甚至不在於文件選擇器。也許你不知道如何將「ActionListener」添加到「JButton」中(http://stackoverflow.com/questions/16351875/jfilechooser-on-a-button-click)。無論如何,你的問題仍然不值得重新投票。你的編輯*迷惑*,你需要*澄清*! –

回答

1

我已經寫了概念證明了這一點,它的工作原理究竟預期。當你運行它,一個JFrame打開:

enter image description here

JFrameJButton的文本ATUL推,推!當你點擊這個按鈕會打開一個對話框:

enter image description here

我選擇一個文件夾(test),我選擇一個文件名(test.pdf)。然後點擊保存。這是我的文件夾中顯示的內容:

enter image description here

當我打開這個文件,我看到:

enter image description here

這是例子的完整代碼:

/* 
* 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); 
    } 
} 
+1

@ATUL你應該學會[奧卡姆剃刀(https://en.wikipedia.org/wiki/Occam%27s_razor)的原則。你應該簡化你的問題,只討論問題的實質。你已經試過我的例子,你已經看到它的作品。如果這在你的辦公室的代碼中不起作用,那麼你的辦公室裏的代碼有一個錯誤。**你的辦公室裏的代碼有一個錯誤**,因爲你沒有正確地按照指示進行操作。可以說*你可以不理解我的問題告訴你,你做錯了什麼,**因爲你沒有展示你的代碼!**你讓人們在你生氣。* –

+1

如果這個答案是有幫助的,請接受答案(勾選複選標記)。現在,答案是downvoted,這意味着其他人可能會認爲這是一個不好的答案。如果您有新問題,請發佈新問題。請注意,這個新問題並不關於iText。它關於'JFileChooser'和文件權限:系統是否允許覆蓋現有文檔? –

+0

還有一個與此代碼相關的問題如何檢查當我們第一次使用filechooser生成pdf時,它是z:\\ test.pdf,但是當我們再次嘗試生成相同的名稱時,它們顯示錯誤如名稱已經生成,請選擇另一個名稱。我們怎麼能做這種類型的問題? – ATUL