2012-11-06 64 views
0

我正在使用iTextSharp爲ASP.Net(c#)中的報表創建PDF。創建 全文成功&可以運行應用程序時可以打開,但是當IAM託管在IIS它可以在PDF無法打開,並給出錯誤:iTextsharp pdf在IIS中託管時無法打開

「的Adobe Reader無法打開PDF,因爲它要麼不支持的文件類型或因爲該文件已損壞「

我的代碼,如:

Document pdfDoc = new Document(); 
pdfDoc.SetPageSize(PageSize.A4.Rotate()); 

string filepath = "Demo" + ".pdf"; 

string attachment = "attachment; filename=" + filepath; 
Response.Charset = String.Empty; 
Response.ClearContent(); 
Response.AddHeader("content-disposition", attachment); 
Response.ContentType = "application/PDF"; 

PdfWriter.GetInstance(pdfDoc, Response.OutputStream); 

pdfDoc.Open(); 

//Adding some paragraph in to the pdfdoc.    

pdfDoc.Close(); 

Response.Write(pdfDoc); 
Response.End(); 

我等着你的greatfull想法

+3

什麼是「Response.Write(pdfDoc);」線路好嗎? PDF本身通過「PdfWriter.GetInstance(pdfDoc,Response.OutputStream)」的方式導出。因此,該Response.Write調用很可能會將垃圾添加到響應中。 – mkl

+0

有沒有更好的方法來做到這一點? – Adersh

+0

爲什麼你想這樣做(即返回PDF以外的東西)呢?如果你不想,只需刪除「Response.Write(pdfDoc);」線。 – mkl

回答

1

我知道,這舊的文章,但我有一個PROBL em和itextsharp,在我的情況下是關於iis用戶的文件夾權限。

我只是右鍵點擊我的主文件夾,去安全選項卡,並選擇.Net用戶(爲我2),並給了完全控制。那是我的問題,並解決了。希望這可以幫助別人。