2011-12-14 42 views
6

我使用iTextSharp生成了一个pdf,当它创建它时,它会自动保存在我的代码中提供的位置上,而不是在客户端,当然也不会告诉用户。iTextSharp生成PDF:如何将pdf发送到客户端并添加提示?

我需要将其发送给客户端,我需要提示一个对话框,问他在哪里要救自己PDF用户..

我如何能做到这一点吗?

这是我的PDF代码:

using (MemoryStream myMemoryStream = new MemoryStream()) 
{ 
    Document document = new Document(); 
    PdfWriter PDFWriter = PdfWriter.GetInstance(document, myMemoryStream); 

    document.AddHeader("header1", "HEADER1"); 


    document.Open(); 

     //.......... 

    document.Close(); 

    byte[] content = myMemoryStream.ToArray(); 

    // Write out PDF from memory stream. 
    using (FileStream fs =  File.Create(HttpContext.Current.Server.MapPath("~\\report.pdf"))) 
    { 
     fs.Write(content, 0, (int)content.Length); 
    } 

编辑

这是结果的一个例子,我想 http://examples.extjs.eu/?ex=download

感谢您的答复,我修改了代码,这:

HttpContext.Current.Response.ContentType = "application/pdf"; 
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=test.pdf"); 


using (MemoryStream myMemoryStream = new MemoryStream()) 
{  
Document document = new Document();  
PdfWriter PDFWriter = PdfWriter.GetInstance(document, myMemoryStream); 

document.AddHeader("Content-Disposition", "attachment; filename=wissalReport.pdf"); 

document.Open(); 

    //.......... 

document.Close(); 


byte[] content = myMemoryStream.ToArray(); 
HttpContext.Current.Response.Buffer = false; 
HttpContext.Current.Response.Clear();   
HttpContext.Current.Response.ClearContent(); 
HttpContext.Current.Response.ClearHeaders(); 
HttpContext.Current.Response.AppendHeader("content-disposition","attachment;filename=" + "my_report.pdf");     
HttpContext.Current.Response.ContentType = "Application/pdf";   

//Write the file content directly to the HTTP content output stream.  
HttpContext.Current.Response.BinaryWrite(content);   
HttpContext.Current.Response.Flush();     
HttpContext.Current.Response.End(); 

,但我得到这个错误:

Uncaught Ext.Error: You're trying to decode an invalid JSON String: 
%PDF-1.4 %���� 3 0 obj <</Type/XObject/Subtype/Image/Width 994/Height 185/Length 13339/ColorSpace/DeviceGray/BitsPerComponent 8/Filter/FlateDecode>>stream x���|E� 
........... 

IM绝对相信我的iTextSharp的创建PDF是正确的,因为我可以将它保存在服务器上,但那不是什么我需要做的,当我尝试把它发送到在客户端,我得到

由于上述错误提前

+0

适用于WebApp/WinForm/Console程序吗?在继续pdf生成之前,您总是可以请求一个位置。 – YetAnotherUser 2011-12-14 14:24:30

+0

它的一个web应用程序..如何做呢? (对不起,新的C#和iTextSharp) – Armance 2011-12-14 14:29:05

回答

3

解决

的错误是从提交操作试图解释响应,它不能因为它不是一个已知的格式。

我只是设置window.location下载文件,这工作正常。

{ 
xtype:'button',   
text: 'Generate PDF',   
handler: function() { 
    window.location = '/AddData.ashx?action=pdf';     
} 
} 

而不是设置位置,你也可以做window.open()。

文件是否被下载或打开取决于浏览器设置。

5

在你可能希望将PDF作为二进制流,以用户的web应用程序,要么打开PDF或提示用户保存文件的情况。

记住pdf生成在服务器上发生,即使用户提供的路径在服务器上不会有任何用处。请参见以下链接 -

在你的情况你生成的文件,因此将已经具有二进制流而不是文件,因此你可以直接使用Response.BinaryWrite代替Response.WriteFile

修改样本:

Response.Buffer = false; 
Response.Clear(); 
Response.ClearContent(); 
Response.ClearHeaders(); 
//Set the appropriate ContentType. 
Response.ContentType = "Application/pdf"; 
//Write the file content directly to the HTTP content output stream. 
Response.BinaryWrite(content); 
Response.Flush(); 
Response.End(); 
+0

其罚款我不需要它onserver我需要用户能够保存在他的end..will看看这个,让你知道,ty – Armance 2011-12-14 14:43:55

+0

即时通讯不使用Response.WriteFile!你在我的代码中引用的内容与我的代码中的内容相同byte [] content = myMemoryStream.ToArray(); ? – Armance 2011-12-14 14:59:15

+0

是 - 内容与您的代码中相同。你需要使用`Response.BinaryWrite(content)`。 – YetAnotherUser 2011-12-14 15:01:35

3

目前要保存你的文件服务器上的文件,从而覆盖每个请求相同的PDF。如果您同时收到两个PDF请求,可能会导致错误。

使用Response将PDF(从内存流)返回给用户,并跳过将PDF写入本地服务器上的文件。

浏览器会询问用户应该在哪里保存文件。喜欢的东西:

Response.ContentType = "Application/pdf"; 
    myMemoryStream.CopyTo(Response.OutputStream); 

也期待在the answer from Alun,使用content-disposition你可以提出一个文件名给用户。

3

您需要将内容处置标题发送给用户浏览器。从内存代码是这样的:

Response.ContentType = "application/pdf"; 
Response.AppendHeader("Content-Disposition","attachment; filename=nameofthefile.pdf"); 
1

你不需要使用MemoryStream。改为使用Response.OutputStream。这就是它的目的。无需使用Response.BinaryWrite()或任何其他调用来明确写入文档; iTextSharp负责在使用Response.OutputStream时写入流。

这里有一个简单的工作示例:

Response.ContentType = "application/pdf"; 
Response.AppendHeader(
    "Content-Disposition", 
    "attachment; filename=test.pdf" 
); 
using (Document document = new Document()) { 
    PdfWriter.GetInstance(document, Response.OutputStream); 
    document.Open(); 
    document.Add(new Paragraph("This is a paragraph")); 
} 

这里的how to add the proper HTTP headers。 (获取保存文件的提示)如果您的代码位于web form(按钮点击处理程序)中,请在using语句后面添加Response.End()到上面的代码示例,以便Web表单的HTML输出不会附加到PDF文档中。

相关问题