iTextSharp的生成的PDF:如何将PDF格式发送到客户端,并添加提示? [英] iTextSharp generated PDF: How to send the pdf to the client and add a prompt?

查看:209
本文介绍了iTextSharp的生成的PDF:如何将PDF格式发送到客户端,并添加提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经生成使用iTextSharp的,一个PDF创建它在它没有告诉任何用户在我code提供的服务器上而不是在客户端,当然还有位置自动保存时。

I have generated a pdf using iTextSharp, when its created it saves automatically in the location provided in my code on the server not on the client side and of course without telling anything to the user.

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

I need to send it to the client and I need to prompt a dialogue box to ask the user where he wants to save his pdf..

我如何能做到这一点吗?

how can i do this please?

这是我的PDF code:

this is my pdf code:

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

this is an example of the result i want http://examples.extjs.eu/?ex=download

感谢您的答复,我修改了code这样:

thanks to your replies ,I modified my code to this:

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

但我得到这个错误:

but i get this error:

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是正确的,因为我可以将它保存在服务器上,但那不是我需要做的,当我尝试把它发送给客户端,我得到上述

im absolutely sure my itextsharp to create pdf is correct because i can save it on the server, but thats not what i need to do ,when i try to send it to the client i got the error above

在此先感谢

推荐答案

解决

的错误是从尝试间$ P $角它不能,因为它不是在一个已知的格式的响应提交操作

The error is from the submit operation trying to interpret the response which it can not because it is not in a known format.

我只设置了window.location下载文件,并能正常工作。

I just set window.location to download files and this works fine.

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

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

Instead of setting the location you can also do window.open().

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

Whether the file will be downloaded or opened depends on browser settings.

这篇关于iTextSharp的生成的PDF:如何将PDF格式发送到客户端,并添加提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆