在C#中设置PDF文件名

时间:2013-05-16 08:43:34

标签: c# file

我正在使用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));

2 个答案:

答案 0 :(得分: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));

希望它适合你。

答案 1 :(得分: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 
        }