0

我正在用 C# 生成一个 PDF 文件。文件的标题是自动分配的。我想在打开 folderbrowserdialog 时设置文件名。我怎样才能做到这一点?

DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
     string caminho = folderBrowserDialog1.SelectedPath;
     var pasta2 = caminho.Replace(@"\", @"\\");
     Document doc = new Document(PageSize.A4.Rotate(), 10, 10, 42, 35);

     PdfWriter.GetInstance(doc, new FileStream(pasta2 + "\\Relatorio.pdf", FileMode.Append, FileAccess.Write));
4

2 回答 2

1

您不能在FolderBrowserDialog中设置文件名,您需要使用SaveFileDialog。试试这个示例代码

  saveFileDialog1.FileName = "Akshay.pdf";

       saveFileDialog1.FileOk +=new CancelEventHandler(saveFileDialog1_FileOk);
     saveFileDialog1.ShowDialog();


private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
           // MessageBox.Show("Done");
           // do the PDF Method here 
        }
于 2013-05-16T08:57:44.910 回答
1

试试类似的东西,你必须使用SaveFileDialog,有关更多信息,请访问MSDN

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

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

dialog1.FileName在创建时使用FileStream

PdfWriter writertest = PdfWriter.GetInstance(doc, new FileStream(dialog1.FileName, FileMode.Create));

希望对你有效。

于 2013-05-16T08:49:52.630 回答