如何通过编程方式打印 Pdf 文档?
我正在使用以下代码来打印 PDF 文件。但是当我直接单击打印图标时,它会开始打印。但我不想要它。
<asp:ImageButton ID="PrintButton" runat="server" ImageUrl="~/images/print-icon.png"
OnClick="PrintButton_Click" ToolTip="Print Document" />
我的 Cs 代码是
protected void PrintButton_Click(object sender, EventArgs e)
{
ProcessStartInfo infoPrint = new ProcessStartInfo();
infoPrint.FileName = Session["filename"].ToString();
infoPrint.Verb = "PrintTo";
infoPrint.CreateNoWindow = true;
infoPrint.WindowStyle = ProcessWindowStyle.Normal;
infoPrint.UseShellExecute = true;
Process printProcess = new Process();
printProcess = Process.Start(infoPrint);
}
我想在用户单击打印图标时打开一个打印对话框。如果用户单击“打印”对话框中的“打印”按钮,那么我想开始打印文档。我的 PDF 文件位于服务器上的一个文件夹中,我希望在 ASP.NET 中以编程方式打印它。
最佳答案
此代码将在服务器而不是客户端上运行。开发时服务器和客户端是同一台机器,即您的本地工作站。部署后,这将在服务器上执行,而不是在用户的本地工作站上。
您可以使用 javascript 打开打印对话框
window.print();
但是,这将打印整个 webapge,而不是具体的文档。
如果您只想打印 PDF,则需要将文件流式传输到浏览器(而不是整个 Web 表单)。然后,用户可以利用 adobe reader 中的 native 打印选项。网上有很多关于如何将文档流式传输到客户端浏览器的示例。
关于asp.net - 以编程方式打印 PDF 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16015594/