如何以编程方式在客户端打印机上打印服务器端文本文件

时间:2014-12-31 17:42:29

标签: asp.net vb.net printing

我有一个服务器端应用程序,它在服务器上创建一系列文本文件。我需要允许用户通过单击网页上的按钮直接在客户端打印机上打印一个或多个文件。这应该打开一个打印机选择窗口,然后开始打印所选的网页。我可以处理文件选择和打印机选择窗口,但无法获取要打印的文件。我在代码项目(http://www.codeproject.com/Tips/689325/Send-PDF-files-directly-to-client-printer-without)上找到了代码,这些代码被描述为完全符合我的需要,但用于PDF文件。我试图使它适应文本文件,代码运行没有明显的错误,但没有任何东西到达打印机,我不知道如何找到代码执行网页源代码的Object标签的value属性后发生的事情(见下文)。我在VB.Net中编程。

以下是源代码中的object标记:

 <object id = "Object1" name="Txt1" 
type="file/txt" width="1" height="1" >
            <param  name='SRC' value='<%= SReportFileName %>'/>
</object> 

以下是代码隐藏文件中的代码:

Partial Class ViewResults_PrintingReports
    Inherits System.Web.UI.Page
    Public SReportFileName As String = ""
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim PathName As String = ""
        Dim Uname As String = ""
        Dim Iyr As String = ""
        Dim Iwld As String = ""
        Dim Iqtr As String = ""
        Dim FName As String = ""
        Dim CNumber As String = ""
        Uname = "UserName"
        Iyr = "3"
        Iqtr = "1"
        FName = "ReportA"
        CNumber = "1"
        Iwld = "1"
        '       Dim cs As ClientScriptManager = Page.ClientScript
        PathName = "~/Competitions/" & Uname & "/BP/" & "World" & Iwld & "/Reports/QtrRpts" & Iwld & "." & Iyr & Iqtr & "/" & FName & "." & Iwld & CNumber & ".txt"
        SReportFileName = PathName '// temp/mypdf.pdf 
        Try
            ClientScript.RegisterStartupScript(GetType(Page), "MessagePopUp", "<script language=text/javascript>document.Txt1.printAll()</script>")
        Catch ex As Exception
            MsgBox("Problem printing file", , ex.Message)
        End Try
    End Sub
End Class

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您需要拨打javascript 这是javascript

          function PrintFile(FilePath) {

                var printWin = window.open(FilePath, '','left=0,top=0,width=700,height=700,status=0');

                printWin.focus();
                printWin.print();

            }

这是按钮的代码隐藏

Dim FilePath As String
        FilePath = "This is your File Path"

        Dim PrintFile As String
        PrintFile = "PrintFile('" & FilePath & "')"
        ScriptManager.RegisterStartupScript(Me, Me.GetType(), "ntmtch", PrintFile, True)