ItextSharp can't find PDF
我正在.net 3.5应用程序中使用itextsharp 5.4.5。从文件路径创建PdfReader对象时,出现文件未找到异常但文件存在的情况。我已经确认File.Open可以打开文件。测试代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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 />Message:" + cex.Message.Replace("<","<").Replace(">",">") + "<br /><br />Exception .ToString:" + cex.ToString().Replace("<","<").Replace(">",">") + "<br /><br />Source:" + cex.Source + "<br /><br />StackTrace:" + cex.StackTrace + "<br /><br />TargetSite:" + cex.TargetSite); } catch (Exception cex) { Response.Write("Fail: <br /><br />Message:" + cex.Message.Replace("<","<").Replace(">",">") + "<br /><br />Exception .ToString:" + cex.ToString().Replace("<","<").Replace(">",">") + "<br /><br />Source:" + cex.Source + "<br /><br />StackTrace:" + cex.StackTrace + "<br /><br />TargetSite:" + cex.TargetSite); } |
异常详细信息:
1 2 3 4 5 6 7 8 9 10 11 | Message: C:\\inetpub\\wwwroot\\HepBnet\ esearch\\DataSystem\\App_Data\\pdf\\EC_1.0.pdf not found as file or resource. Exception .ToString: System.IO.IOException: C:\\inetpub\\wwwroot\\HepBnet\ esearch\\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) |
我假设这是一个权限问题,但是它不应该使用与.net应用程序相同的用户吗?
我弄清楚了问题所在。由于某种原因,Web服务器正在"阻止" itext库。右键单击该库以访问属性,然后单击"取消阻止",然后强制应用程序重新启动,从而解决了该问题。
我希望这对其他人有帮助...
我也有这个问题。事实证明它很复杂,因此不确定是否会有其他人遇到相同的问题,但这值得检查。
就我而言,我的系统正在从数据库中存储的文件名中打开PDF文件。当我移动文件时,我的系统无法再打开文件,因为现在文件名太长了。例如:
还请确保在Windows中检查您的文件权限。应用程序的运行身份必须有权访问该文件。