您可以使用以下代码示例来在浏览器中打开并打印PDF文件:
using System;
using System.IO;
using System.Web;
public partial class PrintPDF : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string filePath = "your-file-path.pdf"; // 替换为您的PDF文件路径
// 设置响应头
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "inline;filename=" + Path.GetFileName(filePath));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
// 将PDF文件写入响应流
Response.WriteFile(filePath);
// 结束响应
Response.Flush();
Response.End();
}
}
请注意,您需要将上述代码放在ASP.NET C#页面中,例如PrintPDF.aspx.cs。然后,您可以通过导航到PrintPDF.aspx来调用该页面并在浏览器中打开和打印PDF文件。