1

如何以编程方式打印 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 中以编程方式打印。

4

2 回答 2

2

this code will run on the server not the client. while developing the server and client are the same machine, your local workstation. Once deployed, this would execute on the server, not the user's local work station.

you can open a print dialog box using javascript

window.print();

However that will print the entire webapge, not the document specifically.

If you would like to print only the PDF, you need to stream the file to browser (not an entire webform). The user can then take advantage of the native print options within the adobe reader. There are many examples online about how to stream documents to the client's browser.

于 2013-04-15T13:19:58.627 回答
0

看看这个帖子

此代码添加 javascript 行以打印 pdf

Public Shared Function PrintJStoPDF(thePDF As Byte(), direct As Boolean) As Byte()


    Dim BB As Byte() = Nothing

    Using ms As New MemoryStream
        Using reader As New PdfReader(thePDF)
            Dim stamper = New PdfStamper(reader, ms)

            Dim jsText As String = "var res = app.setTimeOut('this.print({bUI: true, bSilent: " & direct.ToString.ToLower & ", bShrinkToFit: false});', 200);"

            stamper.JavaScript = jsText

            stamper.FormFlattening = True
            stamper.Writer.CloseStream = False
            stamper.Close()


            ms.Position = 0

            BB = ms.ToArray
        End Using
    End Using

    Return BB

End Function
于 2014-02-06T09:17:56.583 回答