2010-12-03 15 views
7

私のメインフォームでは、SavePDFDocument()というメソッドがあります。ファイルを保存する場所をユーザーに選択させるにはどうすればよいですか?

private void SavePDFDocument() 
{ 
    PDFWrapper pdfWrapper = new PDFWrapper(); 
    pdfWrapper.CreatePDF(horizontalPictureScroller1.GetPictures(), "pdfDocument.pdf"); 
} 

これでわかるように、今は手動でファイルの名前を入力しています。私はそれを保存する場所と名前を選択するようにユーザーに依頼したいと思います。

これは私が上記で使用しているCreatePDF()メソッドです:

public void CreatePDF(List<System.Drawing.Image> images, string filename) 
{ 
    if (images.Count >= 1) 
    { 
     Document document = new Document(PageSize.LETTER); 
     try 
     { 

      // step 2: 
      // we create a writer that listens to the document 
      // and directs a PDF-stream to a file 

      PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create)); 

      // step 3: we open the document 
      document.Open(); 

      foreach (var image in images) 
      { 
       iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg); 

       if (pic.Height > pic.Width) 
       { 
        //Maximum height is 800 pixels. 
        float percentage = 0.0f; 
        percentage = 700/pic.Height; 
        pic.ScalePercent(percentage * 100); 
       } 
       else 
       { 
        //Maximum width is 600 pixels. 
        float percentage = 0.0f; 
        percentage = 540/pic.Width; 
        pic.ScalePercent(percentage * 100); 
       } 

       pic.Border = iTextSharp.text.Rectangle.BOX; 
       pic.BorderColor = iTextSharp.text.BaseColor.BLACK; 
       pic.BorderWidth = 3f; 
       document.Add(pic); 
       document.NewPage(); 
      } 
     } 
     catch (DocumentException de) 
     { 
      Console.Error.WriteLine(de.Message); 
     } 
     catch (IOException ioe) 
     { 
      Console.Error.WriteLine(ioe.Message); 
     } 

     // step 5: we close the document 
     document.Close(); 
    } 
} 

任意の提案ですか?

答えて

13

あなたはSaveFileDialogを見ていましたか?

private void button1_Click(object sender, System.EventArgs e) 
{ 
    Stream myStream ; 
    SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 

    saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ; 
    saveFileDialog1.FilterIndex = 2 ; 
    saveFileDialog1.RestoreDirectory = true ; 

    if(saveFileDialog1.ShowDialog() == DialogResult.OK) 
    { 
     if((myStream = saveFileDialog1.OpenFile()) != null) 
     { 
      // Code to write the stream goes here. 
      myStream.Close(); 
     } 
    } 
} 
+0

これは完璧に機能しました。 –

4

私はthis pageは、あなたが探しているものを説明し信じる:

// Configure save file dialog box 
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); 
dlg.FileName = "Document"; // Default file name 
dlg.DefaultExt = ".text"; // Default file extension 
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension 

// Show save file dialog box 
Nullable<bool> result = dlg.ShowDialog(); 

// Process save file dialog box results 
if (result == true) 
{ 
    // Save document 
    string filename = dlg.FileName; 
} 
+4

私はより高いレベルだと思いますが「System.Windows.Forms.SaveFileDialog」は、この場合、より適切です。誰もが違いが何であるか知っていますか? MSDNを見ても明らかではありません。 –

+0

おかげで、質問はもっと難しいです。このSaveFieldDialog機能を上記のコードとどのように統合できますか? Notice私は新しいFileStream()を使用しています。助言がありますか? –

+0

@Serg私の答えに載せたサンプルコードを見てください。 Streamオブジェクトを処理しています。 –

関連する問題