2017-09-26 156 views
-1

我正在尝试将PDF保存到文档文件夹。我搜索了很多资源,但都没有为我工作。我曾尝试使用showfiledialog哪些不起作用。我想要的是将我的PDF文件保存到文档文件夹。我需要为学校项目完成这一点,而这是唯一让我难过的部分。到目前为止,这是我的代码:将PDF保存到本地磁盘C#

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(); 
    ... 
+0

什么是_nameTxtB.Text_的内容? – Steve

+0

另外,在该Stream周围放置一个'using'块。 –

+0

@Steve''nameTxtB.text''是客户名称,用作组织的PDF标题,如果这是您的问题所要求的。 – Yuvraj

回答

1

你应该添加内容(nameTxtB.Text)段落不FILESTREAM

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