Print different document using different buttons

614 Views Asked by At

I am using the the Neodynamic SDK to print documents to the client side.

We will have 5 documents to print. I see how to print either 1 document or print all documents, but is there a way to print 1 document per button. I.e. button1 prints out doc1, button2 prints doc2, etc.

Here is what I've got so far

  <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>

I thought of changing filetwo() variable to clientprintjob cpj2...

1

There are 1 best solutions below

2
On

What you need is to make a function called PrintDoc() that receives the path of the file to print as a parameter. The signature should look like this:

Private Sub PrintDoc(path as String)

End Sub

You only need this function once and no need for sub fileone() and sub filetwo() ... up to filefive(). You just need this one sub called PrintDoc(path as String)

Then you need 5 buttons, each with their own _Click() event and in that event handler you call the PrintDoc procedure by passing a different param everytime.

Hope this helps.

Good luck!