Domanda

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?

È stato utile?

Soluzione

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...

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top