2014-02-08 55 views
1

我在.net 3.5应用程序中使用itextsharp 5.4.5。当从文件路径创建一个PdfReader对象时,我得到一个文件未找到的异常,但该文件存在。我已确认File.Open能够打开文件。测试代码:ItextSharp无法找到PDF

try 
{ 
    FileStream f = File.Open(Server.MapPath("~/App_Data/pdf/EC_1.0.pdf"), FileMode.Open); 
    f.Close(); 
    f.Dispose(); 

    PdfReader pdfReader = new PdfReader(Server.MapPath("~/App_Data/pdf/EC_1.0.pdf")); 
    pdfReader.Close(); 
    pdfReader.Dispose(); 

    Response.Write("Succeed"); 
} 
catch (IOException cex) 
{ 
    Response.Write("Fail: <br /><br /><b>Message:</b> " + cex.Message.Replace("<", "&lt;").Replace(">", "&gt;") + 
     "<br /><br /><b>Exception .ToString:</b> " + cex.ToString().Replace("<", "&lt;").Replace(">", "&gt;") + 
     "<br /><br /><b>Source:</b> " + cex.Source + 
     "<br /><br /><b>StackTrace:</b> " + cex.StackTrace + 
     "<br /><br /><b>TargetSite:</b> " + cex.TargetSite); 
} 
catch (Exception cex) 
{ 
    Response.Write("Fail: <br /><br /><b>Message:</b> " + cex.Message.Replace("<", "&lt;").Replace(">", "&gt;") + 
     "<br /><br /><b>Exception .ToString:</b> " + cex.ToString().Replace("<", "&lt;").Replace(">", "&gt;") + 
     "<br /><br /><b>Source:</b> " + cex.Source + 
     "<br /><br /><b>StackTrace:</b> " + cex.StackTrace + 
     "<br /><br /><b>TargetSite:</b> " + cex.TargetSite); 
} 

异常详细信息:

Message: C:\inetpub\wwwroot\HepBnet\research\DataSystem\App_Data\pdf\EC_1.0.pdf not found as file or resource. 

Exception .ToString: System.IO.IOException: C:\inetpub\wwwroot\HepBnet\research\DataSystem\App_Data\pdf\EC_1.0.pdf not found as file or resource. at iTextSharp.text.io.RandomAccessSourceFactory.CreateByReadingToMemory(String filename) at iTextSharp.text.io.RandomAccessSourceFactory.CreateBestSource(String filename) at iTextSharp.text.pdf.PdfReader..ctor(String filename, Byte[] ownerPassword, Boolean partial) at iTextSharp.text.pdf.PdfReader..ctor(String filename) at ASP.errors_pdf_test_aspx.Page_Load() 

Source: itextsharp 

StackTrace: at iTextSharp.text.io.RandomAccessSourceFactory.CreateByReadingToMemory(String filename) at iTextSharp.text.io.RandomAccessSourceFactory.CreateBestSource(String filename) at iTextSharp.text.pdf.PdfReader..ctor(String filename, Byte[] ownerPassword, Boolean partial) at iTextSharp.text.pdf.PdfReader..ctor(String filename) at ASP.errors_pdf_test_aspx.Page_Load() 

TargetSite: iTextSharp.text.io.IRandomAccessSource CreateByReadingToMemory(System.String) 

我假定这是一个权限问题,但不应该被的iText使用相同的用户.net应用程序?

回答

2

我想出了问题所在。由于某种原因,网络服务器“阻塞”了itext库。右键单击库访问属性并单击“取消阻止”,然后强制重新启动应用程序可解决问题。

我希望这可以帮助别人...

+1

我最初被这个答案困惑,因为我从来没有听说过Unblock按钮。只是为了解决可能遇到此问题的任何其他人。您必须右键单击itextsharp.dll文件并选择“属性”。从那里您将看到“常规”选项卡底部的“解除阻止”按钮。 –

+0

我没有开锁按钮。答案在这里:http://stackoverflow.com/a/32394334/287516是我解决这个问题更简单的方法。 – ilasno

0

我也有这个问题。结果相当精细,所以不确定其他人会有同样的问题,但它值得检查。

在我的情况下,我的系统打开存储在数据库中的文件名的PDF文件。当我移动文件时,我的系统无法再打开这些文件,因为现在总文件名太长了。例如:c:\the\path\here\in\total\was\just\way\too\long\anditbroke.pdf

另外一定要在Windows中检查您的文件权限。应用程序的运行身份必须有权访问该文件。