1

Am using itextsharp to generate PDF file. I got this error:

"The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters."

Code:

protected void ExportHTMLtoPDF()
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=" + HttpContext.Current.Session["StudentID"].ToString() + ".pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    frmPrint.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A3, 0f, 0f, 0f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter writer;
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}
2
  • Which line of code is actually throwing that exception? Since you're not writing anything to disk you shouldn't be seeing a message like that. Commented Apr 16, 2012 at 12:57
  • Depending on your configured environment, the exception will be thrown by ASP.NET, Visual Studio, or the IIS application pool, not iTextSharp. Try sending an HTML/text file attachment instead and see what happens. (comment out all the iTextSharp code) Google "The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters" (in quotes) and you'll likely find an answer. Commented Apr 17, 2012 at 5:21

2 Answers 2

1

This is nothing to do with your code - that is the maximum filename length for Windows. Chris Haas observes that your code isn't obviously writing any files, so it is most likely that a temporary file is being created - I would guess something in the HTMLWorker or the PDFWriter is creating a temporary file to write to.

You could use ProcessMon to find what files your application is trying to open and see what filename is causing it to complain, that might give you an indicator as to where the problem is originating.

Sign up to request clarification or add additional context in comments.

2 Comments

while that's definitely true, the code above isn't saving anything to disk so it shouldn't be throwing that kind of exception.
Good point. So something ( probably PDFWriter ) is presumably writing a temporary file of some kind during the process and naming it badly.
0

The issues is that iTextSharp uses System.IO. System.IO depends on win32 which has this pathing length limitation. So, the only way around is to try to integrate the long filename hack or another library that gets around this such as Alphaleonis.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.