我有一些生成PDF的代码,然后在屏幕上显示,而不在服务器上创建PDF。
我使用这段代码生成PDF并在创建按钮所在的同一窗口中显示:
using (Stream theStream = temp.GetStream())
{
Response.Clear();
Response.AddHeader("content-disposition", "inline; filename=MyPDF.PDF");
Response.AddHeader("content-length", theStream.Length.ToString());
Response.ContentType = "application/pdf";
//Response.TransmitFile(theStream.ToString());
long theLen = theStream.Length;
byte[] theData = new byte[theLen >= 32768 ? 32768 : (int)theLen];
while (theLen > 0)
{
theStream.Read(theData, 0, theData.Length);
Response.BinaryWrite(theData);
theLen -= theData.Length;
if (theLen < theData.Length && theLen > 0)
theData = new byte[(int)theLen];
}
}
如何让PDF显示在新窗口/标签中?
答案 0 :(得分:0)
无法从服务器端代码强制新窗口。你应该考虑用链接替换按钮,如下所示:
<form target="_blank" method="get" action="http://somesite.com/user/getpdf">
<input type="submit" value="View PDF Invoice">
</form>
这将在新窗口中打开链接,您只需返回在 / user / getpdf / 电话上生成的PDF内容。