如何提示用户选择保存文件的位置?
在我的主窗体中,我有一个名为SavePDFDocument()的方法:
private void SavePDFDocument() { PDFWrapper pdfWrapper = new PDFWrapper(); pdfWrapper.CreatePDF(horizontalPictureScroller1.GetPictures(), "pdfDocument.pdf"); }
如您所见,现在我手动输入文件的名称。 我想请用户选择保存位置以及提供它的名称。
这是我上面使用的CreatePDF()方法:
public void CreatePDF(List 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(); } }
有什么建议?
你看过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(); } } }
我相信这个页面描述了你在寻找什么:
// 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 result = dlg.ShowDialog(); // Process save file dialog box results if (result == true) { // Save document string filename = dlg.FileName; }
一个有用的链接: 如何:使用SaveFileDialog组件保存文件
- 多长时间轮询wifi信号强度?
- 为什么String GetHashCode只处理每四个字符?
- 在MVC4问题中使用RenderAction(actionname,values)
- .NET跟踪不能与Diagnostics.TraceSource一起使用,只能使用Diagnostics.Trace
- .NET属性生成“必须声明一个正文,因为它没有标记为抽象或外部”编译错误
- 是否可以使用GPU加速(动态)LINQ查询?
- 没有为Protobuf-net中的类型:System.Management.Automation.PSObject定义的序列化程序
- Kinect错误启用流
- 使用SimpleMembershipProvider更新自定义用户配置文件字段?