将PDF保存到本地磁盘C#

将PDF保存到本地磁盘C#,c#,winforms,itext,C#,Winforms,Itext,我正在尝试将PDF保存到文档文件夹中。我在谷歌上搜索过很多资源,但没有一个对我有用。我尝试过使用showfiledialog,但没有成功。我想要的是将我的PDF文件保存到documents文件夹中。我需要为一个学校项目做这个,这是唯一让我难堪的部分。到目前为止,这是我的代码: private void savePDF_Click(object sender, EventArgs e) { FileStream fileStream = new FileStream(nameTxtB.

我正在尝试将PDF保存到文档文件夹中。我在谷歌上搜索过很多资源,但没有一个对我有用。我尝试过使用showfiledialog,但没有成功。我想要的是将我的PDF文件保存到documents文件夹中。我需要为一个学校项目做这个,这是唯一让我难堪的部分。到目前为止,这是我的代码:

  private void savePDF_Click(object sender, EventArgs e)
{
    FileStream fileStream = new FileStream(nameTxtB.Text + "Repair.pdf", FileMode.Create, FileAccess.Write, FileShare.None);

    Document document = new Document(); 
    PdfWriter pdfWriter = PdfWriter.GetInstance(document, fileStream);             
    pdfWriter.Open(); 
    PdfContentByte cb = pdfWriter.DirectContent;
    ColumnText ct = new ColumnText(cb);
    document.Open(); 
    ...

您应该将内容(nameTxtB.Text)添加到段落而不是文件流

   using System.IO;
   using iTextSharp.text.pdf;
   using iTextSharp.text;

 static void Main(string[] args) {
        // open the writer
            string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Repair.pdf");
        FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
        Document doc = new Document();

        //Create a New instance of PDFWriter Class for Output File

        PdfWriter.GetInstance(doc, fs);
        //Open the Document
        doc.Open();
        //Add the content of Text File to PDF File

        doc.Add(new Paragraph("Document Content"));

        //Close the Document

        doc.Close();
        System.Diagnostics.Process.Start(fileName);
    }

nameTxtB.Text的内容是什么?另外,在该流周围使用
块抛出
。@Steve
nameTxtB.Text
是用作组织PDF标题的客户名称,如果这是您的问题所在。定义“未工作”,请显示错误等,它在哪一行代码上爆炸?此外,您的代码还远未完成。这样,您就在程序运行的当前目录中创建了一个文件。您需要预先指定要保存文件的路径。要保存在当前用户文档目录中,需要使用Environment.SpecialFolder.MyDocuments枚举。这个网站上有很多例子可以搜索