【问题标题】:Send PDF to the browser rather than saving to the server - ASP.NET iText 7 C# Web Forms将 PDF 发送到浏览器而不是保存到服务器 - ASP.NET iText 7 C# Web 表单
【发布时间】:2020-10-29 13:20:34
【问题描述】:

这是我第一次在 SO 上发帖

我一直在使用 iText 7,因此我的网络应用程序的用户可以生成文档的 pdf。

我希望将文档发送到浏览器,以便将其保存到用户下载文件夹,或者用户可以使用浏览器保存对话框选择保存位置。

但是,我发现的所有 C# 示例都需要硬编码的路径和文件名,因此它会保存在服务器上而不是客户端计算机上。

这几天我一直在研究这个问题,并在寻找解决方案时苦苦挣扎,这就是我所拥有的:

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

}

这会在浏览器的“下载”文件夹中创建 print.pdf,但文件已损坏。

如果有人能指出我哪里出错了,我将不胜感激,这方面的大多数文章都与较旧的 itextsharp 相关,并且 iText 7 示例具有硬编码的文件路径和文件名。

我找到了一种看起来不错的可能解决方案,但不幸的是它是用 Java 编写的。多年来,我一直在苦苦挣扎,试图将其转换为 C#,但我不懂任何 Java,所以它变成了猪的早餐。这是 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();

任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 我不是 C# 程序员,但您不应该在响应中使用 BinaryWrite 而不是 Write 吗?
  • 您尝试编写一个Document 实例(Response.Write(document))。但是 iText 文档只提供了访问 PDF 读写功能的外观,您的代码实际上在 MemoryStream stream 中创建了文档。因此,您应该编写它的内容(类似于Response.Write(stream.ToArray()))。 (我不喜欢 .Net Web 服务创建 API,因此可能需要更正详细信息。)
  • @AndréLemos 这是 Stack Overflow 上的一个常见问题。也许我们需要更多不使用文件的代码示例?
  • 在我的工作中,我们最终找到了一个用于此目的的免费组件,多年来我们一直在使用这个组件来创建 pdf 并将其输出到浏览器:asppdf.com 我知道这并不完全是问题的答案,但它可能是一个不同的可能方向。
  • 感谢您的建议,我已经使用了您的两个建议: Response.BinaryWrite(stream.ToArray()) 它似乎工作得很好。我将发布完整的代码以帮助其他人解决同样的问题。感谢您的帮助,我花了几天的时间试图弄清楚这一点。

标签: c# asp.net pdf webforms itext7


【解决方案1】:

这个解决方案似乎运行良好。

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

【讨论】:

    猜你喜欢
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 2014-09-09
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多