我正在使用 itext7 (7.1.16) 生成 PDF。当我尝试写入磁盘时一切正常。当我试图发回客户端(不保存在磁盘上)时,什么也没有发生。
我将这段代码与 Asp 按钮相关联,但没有任何反应(没有错误也没有目标)。我见过其他线程,这段代码没有问题。我不明白我把手放在哪里了。
protected void mtdCreatePDF(string TypeOutput, object sender, EventArgs e)
{
byte[] content = null;
string suffix = @"Pdf_Produced\Print.pdf";
string nameTGT = HttpContext.Current.Server.MapPath("~") + suffix;
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();
if (TypeOutput == "RESPONSE")
{
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=print.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
//writer.SetCloseStream(false);
Response.BinaryWrite(stream.ToArray());
Response.End();
}
else
{
content = stream.ToArray();
using (FileStream fs = File.Create(nameTGT))
{
fs.Write(content, 0, (int)content.Length);
}
}
}
谢谢你的时间。