2011-06-02 150 views
0

這可能看起來像一個微不足道的問題,我想打開一個現有的pdf模板,編輯並壓扁文件,然後作爲電子郵件附件發送。但是,如何設置PdfReader來讀取位於我的Content文件夾(Content/Documents/PDFFile.pdf)中的文件。這是我給出的錯誤「(無論我嘗試的路徑).pdf找不到作爲文件或資源」。如何設置PdfReader文件路徑?

 using (MemoryStream ms = new MemoryStream()) 
     { 
      //Error is here... 
      PdfReader reader = new PdfReader("~/Content/Documents/PDFFile.pdf"); 

      PdfStamper formFiller = new PdfStamper(reader, ms); 
      AcroFields formFields = formFiller.AcroFields; 
      formFields.SetField("Name", formData.Name); 
      formFields.SetField("Location", formData.Address); 
      formFields.SetField("Date", DateTime.Today.ToShortDateString()); 
      formFields.SetField("Email", formData.Email); 
      formFiller.FormFlattening = true; 
      formFiller.Close(); 

      MailMessage msg = new MailMessage(); 

      msg.To.Add(new MailAddress("[email protected]")); 
      msg.From = new MailAddress("[email protected]"); 
      msg.Subject = "Application Form"; 
      msg.Body = "TEST"; 
      msg.IsBodyHtml = true; 
      msg.Attachments.Add(new Attachment(ms, "Application.pdf", "application/x-pdf")); 
      SmtpClient client = new SmtpClient("10.1.1.15"); 
      client.UseDefaultCredentials = true; 
     } 

任何建議/想法/建議?

+0

,如果你使用絕對路徑是否行得通?我還沒有嘗試,但我的第一個猜測是PDFReader沒有正確解釋'〜/'。 – Kendrick 2011-06-02 15:23:12

+0

我使用了「../」,「../../」以及仍然沒有運氣 – MrM 2011-06-02 15:26:37

+0

我正在考慮完整的本地路徑,包括驅動器號等。 – Kendrick 2011-06-02 15:31:27

回答

5

嘗試使用Server.MapPath("/Path/Here.pdf");Request.PhysicalApplicationPath("/Path/Here.pdf");

+0

我不知道你希望我怎麼做這...對不起 – MrM 2011-06-02 15:27:44

+0

'PdfReader reader = new PdfReader(Server.MapPath(「〜/ Content/Documents/PDFFile.pdf」));' – Kendrick 2011-06-02 15:29:51

+0

我認爲是這樣,但我沒有參考cs文件中的「服務器」爲模型。所以我得到一個錯誤 – MrM 2011-06-02 15:32:33