2010-12-03 2 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을 살펴나요 : 나는 this page 생각

+0

고마워 이것이 완벽하게 작동했습니다. –

4

당신이 찾고있는 무엇을 설명?

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(); 
     } 
    } 
} 
+4

나는 더 높은 수준의 'System.Windows.Forms.SaveFileDialog은'이 경우에 더 적합하다고 생각합니다. 아무도 그 차이가 무엇인지 알 수 있습니까? MSDN을 살펴본 후에는 분명하지 않습니다. –

+0

고마워, 질문은 더 어렵다. 이 SaveFieldDialog 기능을 위의 코드와 어떻게 통합 할 수 있습니까? Notice 새로운 FileStream()을 사용하고 있습니다. 어떤 제안? –

+0

@Serg는 내 대답에 게시 한 예제 코드를 살펴보십시오. 그것은 Stream 객체를 처리하고 있습니다. –

관련 문제