ItextSharp can't find PDF

北城余情 提交于 2019-12-13 18:11:51

问题


I am using itextsharp 5.4.5 in a .net 3.5 app. When creating a PdfReader object from a file path I am getting a file not found exception but the file exists. I have confirmed that File.Open is able to open the file. Test code:

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

Exception detail:

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)

I'm assuming this a permission issue but shouldn't itext be using the same user as the .net app?


回答1:


I figured out what the issue was. For some reason the webserver was 'blocking' the itext library. Right clicking the library to access the properties and clicking 'Unblock' then forcing an app restart fixed the issue.

I hope this helps someone else...




回答2:


I had this problem as well. Turned out to be rather elaborate so not sure anyone else will have the same problem, but it's worth checking.

In my case, my system is opening the PDF files from a filename stored in a database. When I moved my files around, my system couldn't open the files anymore because now the total filename was too long. e.g: c:\the\path\here\in\total\was\just\way\too\long\anditbroke.pdf

Also be sure to check your file permissions in Windows. The application's running identity must have access to the file.



来源:https://stackoverflow.com/questions/21649197/itextsharp-cant-find-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!