2011-06-02 230 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