使用不同按钮打印不同文档

时间:2016-04-22 17:24:32

标签: asp.net vb.net neodynamic

我正在使用Neodynamic SDK将文档打印到客户端。

我们将打印5份文件。我看到如何打印1个文档或打印所有文档,但有没有办法每个按钮打印1个文档。即button1打印出doc1,button2打印doc2等。

这是我到目前为止所做的事情

  <script runat="server">

        Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
            fileone()
            filetwo()

        End Sub

        Public Sub fileone()
            Dim fileToPrint As New PrintFile(Context.Server.MapPath("~/forms/xmlheader.txt"), "xmlheader.txt")
            If (WebClientPrint.ProcessPrintJob(Request)) Then

                'Create a ClientPrintJob
                Dim cpj As New ClientPrintJob()
                'set client printer, for multiple files use DefaultPrinter...
                cpj.ClientPrinter = New DefaultPrinter()
                'set files-printers group by using special formatting!!!
                'Invoice.doc PRINT TO Printer1
                cpj.PrintFile = fileToPrint
                'send it...
                cpj.SendToClient(Response)
            End If
        End Sub
        Public Sub filetwo()
            Dim fileToPrint As New PrintFile(Context.Server.MapPath("~/forms/ How To Recover Office Doc.pdf"), " How To Recover Office Doc.pdf")
            If (WebClientPrint.ProcessPrintJob(Request)) Then

                'Create a ClientPrintJob
                Dim cpj As New ClientPrintJob()
                'set client printer, for multiple files use DefaultPrinter...
                cpj.ClientPrinter = New DefaultPrinter()
                'set files-printers group by using special formatting!!!
                'Invoice.doc PRINT TO Printer1
                cpj.PrintFile = fileToPrint
                'send it...
                cpj.SendToClient(Response)
            End If
        End Sub



        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            fileone()
            ScriptManager.RegisterStartupScript(Me, Me.GetType(), "printForm1", "printForm1();", True)
        End Sub

        Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            filetwo()
            ScriptManager.RegisterStartupScript(Me, Me.GetType(), "printForm2", "printForm2();", True)
        End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to print multiple files to client printers from ASP.NET</title>
<script type="text/javascript">
    function printForm1() {
        jsWebClientPrint.print(fileone());
    }
    function printForm2() {
        jsWebClientPrint.print(filetwo());
    }
</script>
</head>
<body>
<%-- Store User's SessionId --%>
<input type="hidden" id="sid" name="sid" value="<%=Session.SessionID%>" />

<form id="form1" runat="server">

<h1>How to print multiple files to client printers from ASP.NET</h1>
Please change the source code to match your printer names and files to test it locally
<br /><br />
&nbsp;<%-- Add Reference to jQuery at Google CDN --%><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script><%-- Register the WebClientPrint script code --%><%=Neodynamic.SDK.Web.WebClientPrint.CreateScript()%>&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" 
    Height="156px" Width="156px" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" Height="156px" Width="156px"/>
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Button" 
    Height="156px" Width="156px"/>
<asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="Button" 
    Height="156px" Width="156px"/>
<asp:Button ID="Button5" runat="server" onclick="Button5_Click" Text="Button" 
    Height="156px" Width="156px"/>
    </form>

</body>
</html>

我想过将filetwo()变量更改为clientprintjob cpj2 ...

1 个答案:

答案 0 :(得分:0)

您需要的是创建一个名为PrintDoc()的函数,该函数接收要作为参数打印的文件的路径。签名应如下所示:

Private Sub PrintDoc(path as String)

End Sub

你只需要这个功能一次,不需要sub fileone()和sub filetwo()......直到filefive()。你只需要这个名为PrintDoc的子程序(路径为字符串)

然后你需要5个按钮,每个按钮都有自己的_Click()事件,在那个事件处理程序中,你通过每次传递一个不同的参数来调用PrintDoc过程。

希望这有帮助。

祝你好运!