【问题标题】:Saving pdf in user folder将pdf保存在用户文件夹中
【发布时间】:2013-03-10 09:05:54
【问题描述】:

在我的 windows 窗体中,我使用 iTextSharp 生成 pdf。

我已经硬编码了保存 pdf 的位置,但我想询问用户保存此 pdf 的位置。我该怎么做?

这是我的代码:

private void button1_Click_1(object sender, EventArgs e)
{
  using (Bitmap b = new Bitmap(this.Width, this.Height))
  {
    using (Graphics g = Graphics.FromImage(b))
    {
      g.CopyFromScreen(this.Location, new Point(0, 0), this.Size);
    }
    Document doc = new Document();
    iTextSharp.text.Image i = iTextSharp.text.Image.GetInstance(b, System.Drawing.Imaging.ImageFormat.Bmp);
    PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(@"C:\Temp\output.pdf", FileMode.Create));
    doc.SetPageSize(new iTextSharp.text.Rectangle(this.Size.Width + doc.LeftMargin + doc.RightMargin, this.Size.Height + doc.TopMargin + doc.BottomMargin));

    doc.Open();
    doc.Add(i);
    doc.Close();
  }
}

【问题讨论】:

  • open0 对话框并从中获取文件位置

标签: c# winforms itextsharp


【解决方案1】:

使用SaveFileDialog 显示一个标准的保存对话框,允许用户选择他想要保存文件的位置。

使用示例:

SaveFileDialog dialog = new SaveFileDialog();
dialog.Title = "Save file as...";
dialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
dialog.RestoreDirectory = true;

if (dialog.ShowDialog() == DialogResult.OK)
{
    MessageBox.Show(dialog.FileName);
}

当然,您必须在创建 FileStream 时使用dialog.FileName

PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(dialog.FileName, FileMode.Create));

【讨论】:

  • 如果我用上面的代码创建方法 save() 并调用这个方法而不是 new FileStream(@"C:\Temp\output.pdf",它不起作用
  • 创建FileStream时必须使用dialog.FileName
猜你喜欢
  • 1970-01-01
  • 2022-06-29
  • 1970-01-01
  • 2014-03-04
  • 2016-12-15
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
相关资源
最近更新 更多