0

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

1 に答える 1

2

Use SaveFileDialog to display a standard save dialog, which would allow the user to select where he wants the file to be saved.

Example of usage:

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);
}

Of course, you have to use dialog.FileName when creating your FileStream:

PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(dialog.FileName, FileMode.Create));
于 2013-03-10T09:11:11.830 に答える