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

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

问题描述

我使用 iTextSharp 生成了一个 pdf,当它创建时它会自动保存在我的代码中提供的位置在服务器上而不是在客户端,当然不会告诉用户任何事情.

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

请问我该怎么做?

这是我的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

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

感谢您的回复,我将代码修改为:

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

但我收到此错误:

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

我绝对确定我创建 pdf 的 itextsharp 是正确的,因为我可以将它保存在服务器上,但这不是我需要做的,当我尝试将它发送到客户端时,我收到了上述错误

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

提前致谢

推荐答案

SOLVED

错误来自提交操作试图解释响应,因为它不是已知格式.

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天全站免登陆