简体   繁体   English

Send PDF to the browser rather than saving to the server - ASP.NET iText 7 C# Web Forms

[英]Send PDF to the browser rather than saving to the server - ASP.NET iText 7 C# Web Forms

this is my first time posting on SO这是我第一次在 SO 上发帖

I have been using iText 7 so users of my web app can generate a pdf of a document.我一直在使用 iText 7,因此我的 web 应用程序的用户可以生成文档的 pdf。

I would like the document sent to the browser so either it gets saved to the user Downloads folder or the user can choose where to save it with the browser save dialog box.我希望将文档发送到浏览器,以便将其保存到用户下载文件夹,或者用户可以使用浏览器保存对话框选择保存位置。

However, all the C# examples I have found require a hard-coded path & file name, so it gets saved on the server rather than client machines.但是,我发现的所有 C# 示例都需要硬编码的路径和文件名,因此它保存在服务器而不是客户端计算机上。

I have been researching this for a few days now and floundering around with solutions, this is what I have:我已经研究了几天,并在解决方案中挣扎,这就是我所拥有的:

public void mtdCreatePDF()
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=print.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
var stream = new MemoryStream();
var writer = new PdfWriter(stream);
var pdf = new PdfDocument(writer);
var document = new Document(pdf);

document.Add(new Paragraph("Hello world!"));
document.Close();

Response.Write(document);
Response.End();

}

This creates print.pdf in browser Downloads folder, but the file is corrupt.这会在浏览器的下载文件夹中创建 print.pdf,但文件已损坏。

I would be grateful if someone could point out where I am going wrong, the majority of articles in this regard relate to the older itextsharp, and the iText 7 examples have hard-coded file paths and file names.如果有人能指出我哪里出错了,我将不胜感激,这方面的大多数文章都与较旧的 itextsharp 相关,并且 iText 7 示例具有硬编码的文件路径和文件名。

I have found one possible solution that looks good, but unfortunately it is in Java.我找到了一种看起来不错的可能解决方案,但不幸的是它在 Java 中。 I've been floundering around for ages trying to convert it to C#, but I don't know any Java so it's turned into a pig's breakfast.多年来,我一直在苦苦挣扎,试图将其转换为 C#,但我不知道任何 Java,所以它变成了猪的早餐。 This is the Java solution:这是 Java 解决方案:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(baos));
Document doc = new Document(pdfDoc);
doc.add(new Paragraph("Hello world!"));
doc.close();

// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
// the contentlength
response.setContentLength(baos.size());
// write ByteArrayOutputStream to the ServletOutputStream
OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
os.close();

Any help would be greatly appreciated.任何帮助将不胜感激。

Thank you谢谢

This solution seems to be working perfectly.该解决方案似乎运行良好。

public void mtdCreatePDF()
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=print.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            var stream = new MemoryStream();
            var writer = new PdfWriter(stream);
            var pdf = new PdfDocument(writer);
            var document = new Document(pdf);
            document.Add(new Paragraph("Hello world!"));
            document.Close();
            Response.BinaryWrite(stream.ToArray());
            Response.End();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM