How to directly Print Word docs without Preview or Printer Dialog from ASP.NET

Neodynamic

Well-known member
Joined
Dec 5, 2005
Messages
137
Programming Experience
10+
In this walkthrough, you'll learn how to print any MS Word document (*.doc; *.docx) from an ASP.NET website to the client printer without displaying a

print dialog. You'll be able to print the Word doc to the Default client printer as well as to any other installed printer at the client machine. This

solution works with any browser on Windows OS like IE (6 or later), Chrome, Firefox, Opera & Safari!

Requirements
Development/Server-side
- WebClientPrint 2.0 for ASP.NET (or greater)
- ASP.NET 3.5 (or greater)
- Visual Studio 2010 / VWD 2010 (or greater)
- jQuery 1.4.1 (or greater)

Client-side
- WebClientPrint Processor 2.0 for Windows (WCPP) - Part of WebClientPrint Solution
- Microsoft Word

Follow up these steps
- Download & install WebClientPrint for ASP.NET
- Open Visual Studio and create a new ASP.NET 3.5 Website naming it PrintWordDocSample
- Add a reference to Neodynamic.SDK.WebClientPrint.dll to your project
- Open your web.config file and add the following entries:
Code:
<system.web>
...
<httpHandlers>
...   
<add verb="*" path="wcp.axd" type="Neodynamic.SDK.Web.WebClientPrint, Neodynamic.SDK.WebClientPrint"/>
...   
</httpHandlers>
...
</system.web>
<system.webServer>
...   
<handlers>
...
<add name="WCP" verb="*" path="wcp.axd" type="Neodynamic.SDK.Web.WebClientPrint, Neodynamic.SDK.WebClientPrint"/>
...
</handlers>
...
</system.webServer>
- Open the default.aspx page and paste the following markup. The task of this page is to try to detect the WCPP and inform the user to install it if

it's missing.
Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<style>
body{font: 13px 'Segoe UI', Tahoma, Arial, Helvetica, sans-serif;}        
</style>

<%-- WCPP-detection meta tag for IE10 --%>       
<%= Neodynamic.SDK.Web.WebClientPrint.GetWcppDetectionMetaTag() %>
</head>
<body>
<form id="form1" runat="server">
<div id="msgInProgress">
<div id="mySpinner" style="width:32px;height:32px"></div>
<br />
Detecting WCPP utility at client side... 
<br />
Please wait a few seconds...
<br />
</div>
<div id="msgInstallWCPP" style="display:none;">    
<h3>#1 Install WebClientPrint Processor (WCPP)!</h3>
<p>
<strong>WCPP is a native Windows app (without any dependencies!)</strong> that handles all print jobs 
generated by the <strong>WebClientPrint ASP.NET component</strong> at the server side. The WCPP 
is in charge of the whole printing process and can be 
installed on Windows 98, 2000, Me, XP, Vista, Windows 7 and Windows 8 (Desk-mode). It can print to Serial Port RS232 (e.g. COM1), 
Parallel Port (e.g. LPT1), your Windows-Installed Printers (USB) and Network/IP Ethernet printers.
</p>
<p>
The <strong>WCPP</strong> utility <strong>is digitally-signed with a Windows Authenticode</strong> issued by <a 

href="http://www.digicert.com">DigiCert, Inc.</a>. <strong>Install WCPP only if the <u>Publisher is Neodynamic 

SRL</u></strong>, otherwise do not proceed. <br /><br />
<a href="http://www.neodynamic.com/downloads/wcpp20-installer.exe" target="_blank">Download and Install WCPP from Neodynamic 

website</a><br />             
</p>        
<h3>#2 After installing WCPP...</h3>
<p>
<a href="PrintDoc.aspx">You can go and test the printing page</a>
</p>

</div>
</form>

<%-- Add Reference to jQuery at Google CDN --%>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<%-- Add Reference to spin.js (an animated spinner) --%>
<script src="http://fgnass.github.io/spin.js/dist/spin.min.js"></script>

<script type="text/javascript">

var wcppPingDelay_ms = 10000; 

function wcppDetectOnSuccess(){
<%-- WCPP utility is installed at the client side
redirect to WebClientPrint sample page --%>

<%-- get WCPP version --%>
var wcppVer = arguments[0];
if(wcppVer == "2.0.0.0")
window.location.href = "PrintDoc.aspx";
else //force to install WCPP v2.0
wcppDetectOnFailure();
}

function wcppDetectOnFailure() {
<%-- It seems WCPP is not installed at the client side
ask the user to install it --%>
$('#msgInProgress').hide();
$('#msgInstallWCPP').show();                
}

$(document).ready(function () {
<%-- Create the Spinner with options (http://fgnass.github.io/spin.js/) --%>
var spinner = new Spinner({
lines: 12, 
length: 7, 
width: 3, 
radius: 10, 
color: '#336699', 
speed: 1, 
trail: 60               
}).spin($('#mySpinner')[0]); 
});

</script>

<%-- WCPP detection script --%>       
<%= Neodynamic.SDK.Web.WebClientPrint.CreateWcppDetectionScript() %>

</body>
</html>
- Finally, add a new ASPX page and name it PrintDoc.aspx (be sure to UNSELECT the "Place code in separate file" checkbox)

This page allows your user to print the Word doc file to local printers. The user is required to have MS Word installed to get this code working.

Please paste the following markup and code depending on your preferred language i.e. C# or VB on your PrintDoc.aspx file

VB
Code:
<%@ Page Language="VB" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Neodynamic.SDK.Web" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Init(sender As Object, e As System.EventArgs)
'Print file?
If (WebClientPrint.ProcessPrintJob(Request)) Then

'IMPORTANT: MS Word needs to be installed at the client side

Dim useDefaultPrinter As Boolean = (Request("useDefaultPrinter") = "checked")
Dim printerName As String = Server.UrlDecode(Request("printerName"))

'full path of the doc file to be printed
Dim docFilePath As String = "c:\myDocument.doc"

'create a temp file name for our doc file...
Dim fileName As String = Guid.NewGuid().ToString("N") + System.IO.Path.GetExtension(docFilePath)

'Create a PrintFile object with the doc file
Dim file As New PrintFile(docFilePath, fileName)
'Create a ClientPrintJob and send it back to the client!
Dim cpj As New ClientPrintJob()
'set file to print...
cpj.PrintFile = file
'set client printer...
If (useDefaultPrinter OrElse printerName = "null") Then
cpj.ClientPrinter = New DefaultPrinter()
Else
cpj.ClientPrinter = New InstalledPrinter(printerName)
End If
'send it...
cpj.SendToClient(Response)

End If
End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to directly Print Word docs without Preview or Printer Dialog</title>
</head>
<body>
<%-- Store User's SessionId --%>
<input type="hidden" id="sid" name="sid" value="<%=Session.SessionID%>" />

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

<h1>How to directly Print Word docs without Preview or Printer Dialog</h1>
<label class="checkbox">
<input type="checkbox" id="useDefaultPrinter" /> <strong>Use default printer</strong> or...
</label>
<div id="loadPrinters">
<br />
WebClientPrint can detect the installed printers in your machine.
<br />
<input type="button" onclick="javascript:jsWebClientPrint.getPrinters();" value="Load installed printers..." />

<br /><br />
</div>
<div id="installedPrinters" style="visibility:hidden">
<br />
<label for="installedPrinterName">Select an installed Printer:</label>
<select name="installedPrinterName" id="installedPrinterName"></select>
</div>

<br /><br />
<input type="button" style="font-size:18px" onclick="javascript:jsWebClientPrint.print('useDefaultPrinter=' + $('#useDefaultPrinter').attr('checked') + 

'&printerName=' + $('#installedPrinterName').val());" value="Print Word Doc..." />

<script type="text/javascript">
var wcppGetPrintersDelay_ms = 5000; //5 sec

function wcpGetPrintersOnSuccess(){
<%-- Display client installed printers --%>
if(arguments[0].length > 0){
var p=arguments[0].split("|");
var options = '';
for (var i = 0; i < p.length; i++) {
options += '<option>' + p[i] + '</option>';
}
$('#installedPrinters').css('visibility','visible');
$('#installedPrinterName').html(options);
$('#installedPrinterName').focus();
$('#loadPrinters').hide();                                                        
}else{
alert("No printers are installed in your system.");
}
}

function wcpGetPrintersOnFailure() {
<%-- Do something if printers cannot be got from the client --%>
alert("No printers are installed in your system.");
}
</script>
</form>

<%-- 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()%>

</body>
</html>
C#
Code:
<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Neodynamic.SDK.Web" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Init(object sender, EventArgs e)
{
//Print file?
if (WebClientPrint.ProcessPrintJob(Request))
{

//IMPORTANT: MS Word needs to be installed at the client side

bool useDefaultPrinter = (Request["useDefaultPrinter"] == "checked");
string printerName = Server.UrlDecode(Request["printerName"]);

//full path of the doc file to be printed
string docFilePath = @"c:\myDocument.doc";

//create a temp file name for our doc file...
string fileName = Guid.NewGuid().ToString("N") + System.IO.Path.GetExtension(docFilePath);

//Create a PrintFile object with the doc file
PrintFile file = new PrintFile(docFilePath, fileName);
//Create a ClientPrintJob and send it back to the client!
ClientPrintJob cpj = new ClientPrintJob();
//set file to print...
cpj.PrintFile = file;
//set client printer...
if (useDefaultPrinter || printerName == "null")
cpj.ClientPrinter = new DefaultPrinter();
else
cpj.ClientPrinter = new InstalledPrinter(printerName);
//send it...
cpj.SendToClient(Response);            

}
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to directly Print Word docs without Preview or Printer Dialog</title>
</head>
<body>
<%-- Store User's SessionId --%>
<input type="hidden" id="sid" name="sid" value="<%=Session.SessionID%>" />

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

<h1>How to directly Print Word docs without Preview or Printer Dialog</h1>
<label class="checkbox">
<input type="checkbox" id="useDefaultPrinter" /> <strong>Use default printer</strong> or...
</label>
<div id="loadPrinters">
<br />
WebClientPrint can detect the installed printers in your machine.
<br />
<input type="button" onclick="javascript:jsWebClientPrint.getPrinters();" value="Load installed printers..." />

<br /><br />
</div>
<div id="installedPrinters" style="visibility:hidden">
<br />
<label for="installedPrinterName">Select an installed Printer:</label>
<select name="installedPrinterName" id="installedPrinterName"></select>
</div>

<br /><br />
<input type="button" style="font-size:18px" onclick="javascript:jsWebClientPrint.print('useDefaultPrinter=' + $('#useDefaultPrinter').attr('checked') + 

'&printerName=' + $('#installedPrinterName').val());" value="Print Word Doc..." />

<script type="text/javascript">
var wcppGetPrintersDelay_ms = 5000; //5 sec

function wcpGetPrintersOnSuccess(){
<%-- Display client installed printers --%>
if(arguments[0].length > 0){
var p=arguments[0].split("|");
var options = '';
for (var i = 0; i < p.length; i++) {
options += '<option>' + p[i] + '</option>';
}
$('#installedPrinters').css('visibility','visible');
$('#installedPrinterName').html(options);
$('#installedPrinterName').focus();
$('#loadPrinters').hide();                                                        
}else{
alert("No printers are installed in your system.");
}
}

function wcpGetPrintersOnFailure() {
<%-- Do something if printers cannot be got from the client --%>
alert("No printers are installed in your system.");
}
</script>

</form>

<%-- 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()%>

</body>
</html>
- That's it! Run your website and test it. Click on Print Word Doc to print the doc file without preview. You can print it to the Default client

printer
or you can get a list of the installed printers available at the client machine.

Links:
This Demos
Demos
Download WebClientPrint for ASP.NET
More Information about Neodynamic WebClientPrint for ASP.NET

Neodynamic
NET Components & Controls
Barcode, Labeling, Printing & Imaging components for .NET by Neodynamic
 

Neodynamic

Well-known member
Joined
Dec 5, 2005
Messages
137
Programming Experience
10+
How to Print Excel Spreadsheet without Preview or Printer Dialog from ASP.NET

In this walkthrough, you'll learn how to print any Excel spreadsheet (*.xls; *.xlsx) from an ASP.NET website to the client printer

without displaying a print dialog. You'll be able to print the Excel sheet to the Default client printer as well as to any other installed

printer at the client machine
. This solution works with any browser on Windows OS like IE (6 or later), Chrome, Firefox, Opera &

Safari!


Requirements
Development/Server-side
- WebClientPrint 2.0 for ASP.NET (or greater)
- ASP.NET 3.5 (or greater)
- Visual Studio 2010 / VWD 2010 (or greater)
- jQuery 1.4.1 (or greater)

Client-side
- WebClientPrint Processor 2.0 for Windows (WCPP) - Part of WebClientPrint Solution
- Microsoft Excel

Follow up these steps
- Download & install WebClientPrint for ASP.NET
- Open Visual Studio and create a new ASP.NET 3.5 Website naming it PrintExcelSample
- Add a reference to Neodynamic.SDK.WebClientPrint.dll to your project
- Open your web.config file and add the following entries:

<system.web>
...
<httpHandlers>
...
<add verb="*" path="wcp.axd" type="Neodynamic.SDK.Web.WebClientPrint, Neodynamic.SDK.WebClientPrint"/>
...
</httpHandlers>
...
</system.web>
<system.webServer>
...
<handlers>
...
<add name="WCP" verb="*" path="wcp.axd" type="Neodynamic.SDK.Web.WebClientPrint, Neodynamic.SDK.WebClientPrint"/>
...
</handlers>
...
</system.webServer>

- Open the default.aspx page and paste the following markup. The task of this page is to try to detect the WCPP and inform the user to

install it if it's missing.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<style>
body{font: 13px 'Segoe UI', Tahoma, Arial, Helvetica, sans-serif;}
</style>

<%-- WCPP-detection meta tag for IE10 --%>
<%= Neodynamic.SDK.Web.WebClientPrint.GetWcppDetectionMetaTag() %>
</head>
<body>
<form id="form1" runat="server">
<div id="msgInProgress">
<div id="mySpinner" style="width:32px;height:32px"></div>
<br />
Detecting WCPP utility at client side...
<br />
Please wait a few seconds...
<br />
</div>
<div id="msgInstallWCPP" style="display:none;">
<h3>#1 Install WebClientPrint Processor (WCPP)!</h3>
<p>
<strong>WCPP is a native Windows app (without any dependencies!)</strong> that handles all print jobs
generated by the <strong>WebClientPrint ASP.NET component</strong> at the server side. The WCPP
is in charge of the whole printing process and can be
installed on Windows 98, 2000, Me, XP, Vista, Windows 7 and Windows 8 (Desk-mode). It can print to Serial Port RS232 (e.g. COM1),
Parallel Port (e.g. LPT1), your Windows-Installed Printers (USB) and Network/IP Ethernet printers.
</p>
<p>
The <strong>WCPP</strong> utility <strong>is digitally-signed with a Windows Authenticode</strong> issued by <a

href="http://www.digicert.com">DigiCert, Inc.</a>. <strong>Install WCPP only if the <u>Publisher is Neodynamic SRL</u></strong>, otherwise do

not proceed. <br /><br />
<a href="http://www.neodynamic.com/downloads/wcpp20-installer.exe" target="_blank">Download and Install WCPP from Neodynamic website</a><br />


</p>
<h3>#2 After installing WCPP...</h3>
<p>
<a href="PrintExcel.aspx">You can go and test the printing page</a>
</p>

</div>
</form>

<%-- Add Reference to jQuery at Google CDN --%>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<%-- Add Reference to spin.js (an animated spinner) --%>
<script src="http://fgnass.github.io/spin.js/dist/spin.min.js"></script>

<script type="text/javascript">

var wcppPingDelay_ms = 10000;

function wcppDetectOnSuccess(){
<%-- WCPP utility is installed at the client side
redirect to WebClientPrint sample page --%>

<%-- get WCPP version --%>
var wcppVer = arguments[0];
if(wcppVer == "2.0.0.0")
window.location.href = "PrintExcel.aspx";
else //force to install WCPP v2.0
wcppDetectOnFailure();
}

function wcppDetectOnFailure() {
<%-- It seems WCPP is not installed at the client side
ask the user to install it --%>
$('#msgInProgress').hide();
$('#msgInstallWCPP').show();
}

$(document).ready(function () {
<%-- Create the Spinner with options (spin.js) --%>
var spinner = new Spinner({
lines: 12,
length: 7,
width: 3,
radius: 10,
color: '#336699',
speed: 1,
trail: 60
}).spin($('#mySpinner')[0]);
});

</script>

<%-- WCPP detection script --%>
<%= Neodynamic.SDK.Web.WebClientPrint.CreateWcppDetectionScript() %>

</body>
</html>

- Finally, add a new ASPX page and name it PrintExcel.aspx (be sure to UNSELECT the "Place code in separate file"

checkbox)

This page allows your user to print the Excel file (*.xls or *.xlsx) to local printers. The user is required to have MS Excel installed to

get this code working.

Please paste the following markup and code depending on your preferred language i.e. C# or VB on your PrintExcel.aspx file.

VB

<%@ Page Language="VB" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Neodynamic.SDK.Web" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Init(sender As Object, e As System.EventArgs)
'Print file?
If (WebClientPrint.ProcessPrintJob(Request)) Then

'IMPORTANT: MS Excel needs to be installed at the client side

Dim useDefaultPrinter As Boolean = (Request("useDefaultPrinter") = "checked")
Dim printerName As String = Server.UrlDecode(Request("printerName"))

'full path of the Excel file to be printed
Dim excelFilePath As String = "c:\mySpreadsheet.xls"

'create a temp file name for our Excel file...
Dim fileName As String = Guid.NewGuid().ToString("N") + System.IO.Path.GetExtension(excelFilePath)

'Create a PrintFile object with the Excel file
Dim file As New PrintFile(excelFilePath, fileName)
'Create a ClientPrintJob and send it back to the client!
Dim cpj As New ClientPrintJob()
'set file to print...
cpj.PrintFile = file
'set client printer...
If (useDefaultPrinter OrElse printerName = "null") Then
cpj.ClientPrinter = New DefaultPrinter()
Else
cpj.ClientPrinter = New InstalledPrinter(printerName)
End If
'send it...
cpj.SendToClient(Response)

End If
End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to directly Print Excel Spreadsheet without Preview or Printer Dialog</title>
</head>
<body>
<%-- Store User's SessionId --%>
<input type="hidden" id="sid" name="sid" value="<%=Session.SessionID%>" />

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

<h1>How to directly Print Excel Spreadsheet without Preview or Printer Dialog</h1>
<label class="checkbox">
<input type="checkbox" id="useDefaultPrinter" /> <strong>Use default printer</strong> or...
</label>
<div id="loadPrinters">
<br />
WebClientPrint can detect the installed printers in your machine.
<br />
<input type="button" onclick="javascript:jsWebClientPrint.getPrinters();" value="Load installed printers..." />

<br /><br />
</div>
<div id="installedPrinters" style="visibility:hidden">
<br />
<label for="installedPrinterName">Select an installed Printer:</label>
<select name="installedPrinterName" id="installedPrinterName"></select>
</div>

<br /><br />
<input type="button" style="font-size:18px" onclick="javascript:jsWebClientPrint.print('useDefaultPrinter=' + $('#useDefaultPrinter').attr

('checked') + '&printerName=' + $('#installedPrinterName').val());" value="Print Excel Spreadsheet..." />

<script type="text/javascript">
var wcppGetPrintersDelay_ms = 5000; //5 sec

function wcpGetPrintersOnSuccess(){
<%-- Display client installed printers --%>
if(arguments[0].length > 0){
var p=arguments[0].split("|");
var options = '';
for (var i = 0; i < p.length; i++) {
options += '<option>' + p + '</option>';
}
$('#installedPrinters').css('visibility','visible');
$('#installedPrinterName').html(options);
$('#installedPrinterName').focus();
$('#loadPrinters').hide();
}else{
alert("No printers are installed in your system.");
}
}

function wcpGetPrintersOnFailure() {
<%-- Do something if printers cannot be got from the client --%>
alert("No printers are installed in your system.");
}
</script>

</form>

<%-- 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()%>

</body>
</html>

C#
<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Neodynamic.SDK.Web" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Init(object sender, EventArgs e)
{
//Print file?
if (WebClientPrint.ProcessPrintJob(Request))
{

//IMPORTANT: MS Excel needs to be installed at the client side

bool useDefaultPrinter = (Request["useDefaultPrinter"] == "checked");
string printerName = Server.UrlDecode(Request["printerName"]);

//full path of the Excel file to be printed
string excelFilePath = @"c:\mySpreadsheet.xls";

//create a temp file name for our Excel file...
string fileName = Guid.NewGuid().ToString("N") + System.IO.Path.GetExtension(excelFilePath);

//Create a PrintFile object with the Excel file
PrintFile file = new PrintFile(excelFilePath, fileName);
//Create a ClientPrintJob and send it back to the client!
ClientPrintJob cpj = new ClientPrintJob();
//set file to print...
cpj.PrintFile = file;
//set client printer...
if (useDefaultPrinter || printerName == "null")
cpj.ClientPrinter = new DefaultPrinter();
else
cpj.ClientPrinter = new InstalledPrinter(printerName);
//send it...
cpj.SendToClient(Response);

}
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to directly Print Excel Spreadsheet without Preview or Printer Dialog</title>
</head>
<body>
<%-- Store User's SessionId --%>
<input type="hidden" id="sid" name="sid" value="<%=Session.SessionID%>" />
<form id="form1" runat="server">

<h1>How to directly Print Excel Spreadsheet without Preview or Printer Dialog</h1>
<label class="checkbox">
<input type="checkbox" id="useDefaultPrinter" /> <strong>Use default printer</strong> or...
</label>
<div id="loadPrinters">
<br />
WebClientPrint can detect the installed printers in your machine.
<br />
<input type="button" onclick="javascript:jsWebClientPrint.getPrinters();" value="Load installed printers..." />

<br /><br />
</div>
<div id="installedPrinters" style="visibility:hidden">
<br />
<label for="installedPrinterName">Select an installed Printer:</label>
<select name="installedPrinterName" id="installedPrinterName"></select>
</div>

<br /><br />
<input type="button" style="font-size:18px" onclick="javascript:jsWebClientPrint.print('useDefaultPrinter=' + $('#useDefaultPrinter').attr

('checked') + '&printerName=' + $('#installedPrinterName').val());" value="Print Excel Spreadsheet..." />
<script type="text/javascript">
var wcppGetPrintersDelay_ms = 5000; //5 sec

function wcpGetPrintersOnSuccess(){
<%-- Display client installed printers --%>
if(arguments[0].length > 0){
var p=arguments[0].split("|");
var options = '';
for (var i = 0; i < p.length; i++) {
options += '<option>' + p + '</option>';
}
$('#installedPrinters').css('visibility','visible');
$('#installedPrinterName').html(options);
$('#installedPrinterName').focus();
$('#loadPrinters').hide();
}else{
alert("No printers are installed in your system.");
}
}

function wcpGetPrintersOnFailure() {
<%-- Do something if printers cannot be got from the client --%>
alert("No printers are installed in your system.");
}
</script>

</form>

<%-- 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()%>

</body>
</html>

- That's it! Run your website and test it. Click on Print Excel Spreadsheet to print the Excel file without preview. You can print it

to the Default client printer or you can get a list of the installed printers available at the client machine.

Links:
This Demos
Demos
Download WebClientPrint for ASP.NET
More Information about Neodynamic WebClientPrint for ASP.NET

Neodynamic
NET Components & Controls
Barcode, Labeling, Printing & Imaging components for .NET by Neodynamic
 

Neodynamic

Well-known member
Joined
Dec 5, 2005
Messages
137
Programming Experience
10+
Print PDF from ASP.NET directly to default printer without print dialog

In this walkthrough, you'll learn how to print Adobe PDF files from an ASP.NET website directly to the client printer without displaying a print

dialog. You'll be able to print PDF files to the Default client printer as well as to any other installed printer at the client machine. This solution

works with any browser on Windows OS like IE (6 or later), Chrome, Firefox, Opera & Safari!


Requirements

Development/Server-side
- WebClientPrint 2.0 for ASP.NET (or greater)
- ASP.NET 3.5 (or greater)
- Visual Studio 2010 / VWD 2010 (or greater)
- jQuery 1.4.1 (or greater)

Client-side
- WebClientPrint Processor 2.0 for Windows
- Adobe Reader or Foxit Reader (recommended)

Follow up these steps
- Download & install WebClientPrint for ASP.NET
- Open Visual Studio and create a new ASP.NET 3.5 Website naming it PrintPdfSample
- Add a reference to Neodynamic.SDK.WebClientPrint.dll to your project
- Open your web.config file and add the following entries:

<system.web>
...
<httpHandlers>
...
<add verb="*" path="wcp.axd" type="Neodynamic.SDK.Web.WebClientPrint, Neodynamic.SDK.WebClientPrint"/>
...
</httpHandlers>
...
</system.web>
<system.webServer>
...
<handlers>
...
<add name="WCP" verb="*" path="wcp.axd" type="Neodynamic.SDK.Web.WebClientPrint, Neodynamic.SDK.WebClientPrint"/>
...
</handlers>
...
</system.webServer>

- Open the default.aspx page and paste the following markup. The task of this page is to try to detect the WCPP and inform the user to install it if

it's missing.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<style>
body{font: 13px 'Segoe UI', Tahoma, Arial, Helvetica, sans-serif;}
</style>

<%-- WCPP-detection meta tag for IE10 --%>
<%= Neodynamic.SDK.Web.WebClientPrint.GetWcppDetectionMetaTag() %>
</head>
<body>
<form id="form1" runat="server">
<div id="msgInProgress">
<div id="mySpinner" style="width:32px;height:32px"></div>
<br />
Detecting WCPP utility at client side...
<br />
Please wait a few seconds...
<br />
</div>
<div id="msgInstallWCPP" style="display:none;">
<h3>#1 Install WebClientPrint Processor (WCPP)!</h3>
<p>
<strong>WCPP is a native Windows app (without any dependencies!)</strong> that handles all print jobs
generated by the <strong>WebClientPrint ASP.NET component</strong> at the server side. The WCPP
is in charge of the whole printing process and can be
installed on Windows 98, 2000, Me, XP, Vista, Windows 7 and Windows 8 (Desk-mode). It can print to Serial Port RS232 (e.g. COM1),
Parallel Port (e.g. LPT1), your Windows-Installed Printers (USB) and Network/IP Ethernet printers.
</p>
<p>
The <strong>WCPP</strong> utility <strong>is digitally-signed with a Windows Authenticode</strong> issued by <a

href="http://www.digicert.com">DigiCert, Inc.</a>. <strong>Install WCPP only if the <u>Publisher is Neodynamic

SRL</u></strong>, otherwise do not proceed. <br /><br />
<a href="http://www.neodynamic.com/downloads/wcpp20-installer.exe" target="_blank">Download and Install WCPP from Neodynamic

website</a><br />
</p>
<h3>#2 After installing WCPP...</h3>
<p>
<a href="PrintPdf.aspx">You can go and test the printing page</a>
</p>

</div>
</form>

<%-- Add Reference to jQuery at Google CDN --%>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<%-- Add Reference to spin.js (an animated spinner) --%>
<script src="http://fgnass.github.io/spin.js/dist/spin.min.js"></script>

<script type="text/javascript">

var wcppPingDelay_ms = 10000;

function wcppDetectOnSuccess(){
<%-- WCPP utility is installed at the client side
redirect to WebClientPrint sample page --%>

<%-- get WCPP version --%>
var wcppVer = arguments[0];
if(wcppVer == "2.0.0.0")
window.location.href = "PrintPdf.aspx";
else //force to install WCPP v2.0
wcppDetectOnFailure();
}

function wcppDetectOnFailure() {
<%-- It seems WCPP is not installed at the client side
ask the user to install it --%>
$('#msgInProgress').hide();
$('#msgInstallWCPP').show();
}

$(document).ready(function () {
<%-- Create the Spinner with options (spin.js) --%>
var spinner = new Spinner({
lines: 12,
length: 7,
width: 3,
radius: 10,
color: '#336699',
speed: 1,
trail: 60
}).spin($('#mySpinner')[0]);
});

</script>

<%-- WCPP detection script --%>
<%= Neodynamic.SDK.Web.WebClientPrint.CreateWcppDetectionScript() %>

</body>
</html>

- Finally, add a new ASPX page and name it PrintPdf.aspx (be sure to UNSELECT the "Place code in separate file" checkbox)

This page allows your user to print the PDF file to local printers available at the client-side. The user is required to have Adobe Reader installed to get

this code working.

Please paste the following markup and code depending on your preferred language i.e. C# or VB on your PrintPdf.aspx file.


VB
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Neodynamic.SDK.Web" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Init(sender As Object, e As System.EventArgs)
'Print file?
If (WebClientPrint.ProcessPrintJob(Request)) Then

'IMPORTANT: Adobe Reader needs to be installed at the client side

Dim useDefaultPrinter As Boolean = (Request("useDefaultPrinter") = "checked")
Dim printerName As String = Server.UrlDecode(Request("printerName"))

'full path of the PDF file to be printed
Dim pdfFilePath As String = "c:\myDocument.pdf"

'create a temp file name for our PDF file...
Dim fileName As String = Guid.NewGuid().ToString("N") + System.IO.Path.GetExtension(pdfFilePath)

'Create a PrintFile object with the PDF file
Dim file As New PrintFile(pdfFilePath, fileName)
'Create a ClientPrintJob and send it back to the client!
Dim cpj As New ClientPrintJob()
'set file to print...
cpj.PrintFile = file
'set client printer...
If (useDefaultPrinter OrElse printerName = "null") Then
cpj.ClientPrinter = New DefaultPrinter()
Else
cpj.ClientPrinter = New InstalledPrinter(printerName)
End If
'send it...
cpj.SendToClient(Response)

End If
End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to directly Print PDF without Preview or Printer Dialog</title>
</head>
<body>
<%-- Store User's SessionId --%>
<input type="hidden" id="sid" name="sid" value="<%=Session.SessionID%>" />

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

<h1>How to directly Print PDF without Preview or Printer Dialog</h1>
<label class="checkbox">
<input type="checkbox" id="useDefaultPrinter" /> <strong>Use default printer</strong> or...
</label>
<div id="loadPrinters">
<br />
WebClientPrint can detect the installed printers in your machine.
<br />
<input type="button" onclick="javascript:jsWebClientPrint.getPrinters();" value="Load installed printers..." />

<br /><br />
</div>
<div id="installedPrinters" style="visibility:hidden">
<br />
<label for="installedPrinterName">Select an installed Printer:</label>
<select name="installedPrinterName" id="installedPrinterName"></select>
</div>

<br /><br />
<input type="button" style="font-size:18px" onclick="javascript:jsWebClientPrint.print('useDefaultPrinter=' + $('#useDefaultPrinter').attr('checked') +

'&printerName=' + $('#installedPrinterName').val());" value="Print PDF..." />

<script type="text/javascript">
var wcppGetPrintersDelay_ms = 5000; //5 sec

function wcpGetPrintersOnSuccess(){
<%-- Display client installed printers --%>
if(arguments[0].length > 0){
var p=arguments[0].split("|");
var options = '';
for (var i = 0; i < p.length; i++) {
options += '<option>' + p + '</option>';
}
$('#installedPrinters').css('visibility','visible');
$('#installedPrinterName').html(options);
$('#installedPrinterName').focus();
$('#loadPrinters').hide();
}else{
alert("No printers are installed in your system.");
}
}

function wcpGetPrintersOnFailure() {
<%-- Do something if printers cannot be got from the client --%>
alert("No printers are installed in your system.");
}
</script>

</form>

<%-- 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()%>

</body>
</html>

C#

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Neodynamic.SDK.Web" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Init(object sender, EventArgs e)
{
//Print file?
if (WebClientPrint.ProcessPrintJob(Request))
{

//IMPORTANT: Adobe Reader needs to be installed at the client side

bool useDefaultPrinter = (Request["useDefaultPrinter"] == "checked");
string printerName = Server.UrlDecode(Request["printerName"]);

//full path of the PDF file to be printed
string pdfFilePath = @"c:\myDocument.pdf";

//create a temp file name for our PDF file...
string fileName = Guid.NewGuid().ToString("N") + System.IO.Path.GetExtension(pdfFilePath);

//Create a PrintFile object with the PDF file
PrintFile file = new PrintFile(pdfFilePath, fileName);
//Create a ClientPrintJob and send it back to the client!
ClientPrintJob cpj = new ClientPrintJob();
//set file to print...
cpj.PrintFile = file;
//set client printer...
if (useDefaultPrinter || printerName == "null")
cpj.ClientPrinter = new DefaultPrinter();
else
cpj.ClientPrinter = new InstalledPrinter(printerName);
//send it...
cpj.SendToClient(Response);

}
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to directly Print PDF without Preview or Printer Dialog</title>
</head>
<body>
<%-- Store User's SessionId --%>
<input type="hidden" id="sid" name="sid" value="<%=Session.SessionID%>" />

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

<h1>How to directly Print PDF without Preview or Printer Dialog</h1>
<label class="checkbox">
<input type="checkbox" id="useDefaultPrinter" /> <strong>Use default printer</strong> or...
</label>
<div id="loadPrinters">
<br />
WebClientPrint can detect the installed printers in your machine.
<br />
<input type="button" onclick="javascript:jsWebClientPrint.getPrinters();" value="Load installed printers..." />

<br /><br />
</div>
<div id="installedPrinters" style="visibility:hidden">
<br />
<label for="installedPrinterName">Select an installed Printer:</label>
<select name="installedPrinterName" id="installedPrinterName"></select>
</div>

<br /><br />
<input type="button" style="font-size:18px" onclick="javascript:jsWebClientPrint.print('useDefaultPrinter=' + $('#useDefaultPrinter').attr('checked') +

'&printerName=' + $('#installedPrinterName').val());" value="Print PDF..." />

<script type="text/javascript">
var wcppGetPrintersDelay_ms = 5000; //5 sec

function wcpGetPrintersOnSuccess(){
%-- Display client installed printers --%>
if(arguments[0].length > 0){
var p=arguments[0].split("|");
var options = '';
for (var i = 0; i < p.length; i++) {
options += '<option>' + p + '</option>';
}
$('#installedPrinters').css('visibility','visible');
$('#installedPrinterName').html(options);
$('#installedPrinterName').focus();
$('#loadPrinters').hide();
}else{
alert("No printers are installed in your system.");
}
}

function wcpGetPrintersOnFailure() {
<%-- Do something if printers cannot be got from the client --%>
alert("No printers are installed in your system.");
}
</script>

</form>

<%-- 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()%>

</body>
</html>

- That's it! Run your website and test it. Click on Print PDF to print the PDF file without preview. You can print it to the Default client

printer
or you can get a list of the installed printers available at the client machine.

Links:
This Demos
Demos
Download WebClientPrint for ASP.NET
More Information about Neodynamic WebClientPrint for ASP.NET

Neodynamic
NET Components & Controls
Barcode, Labeling, Printing & Imaging components for .NET by Neodynamic
 

Neodynamic

Well-known member
Joined
Dec 5, 2005
Messages
137
Programming Experience
10+
How to Print RAW Text Files without Print Dialog from ASP.NET

In this walkthrough, you'll learn how to print any text-plain file (*.txt) from an ASP.NET website to the client printer without displaying a print

dialog. You'll be able to print the Text file to the Default client printer as well as to any other installed printer at the client machine. This

solution works with any browser on Windows OS like IE (6 or later), Chrome, Firefox, Opera & Safari!

Requirements

Development/Server-side
- WebClientPrint 2.0 for ASP.NET (or greater)
- ASP.NET 3.5 (or greater)
- Visual Studio 2010 / VWD 2010 (or greater)
- jQuery 1.4.1 (or greater)

Client-side

- WebClientPrint Processor 2.0 for Windows
- Microsoft Notepad

Follow up these steps
- Download & install WebClientPrint for ASP.NET
- Open Visual Studio and create a new ASP.NET 3.5 Website naming it PrintTXTSample
- Add a reference to Neodynamic.SDK.WebClientPrint.dll to your project
- Open your web.config file and add the following entries:

<system.web>
...
<httpHandlers>
...
<add verb="*" path="wcp.axd" type="Neodynamic.SDK.Web.WebClientPrint, Neodynamic.SDK.WebClientPrint"/>
...
</httpHandlers>
...
</system.web>
<system.webServer>
...
<handlers>
...
<add name="WCP" verb="*" path="wcp.axd" type="Neodynamic.SDK.Web.WebClientPrint, Neodynamic.SDK.WebClientPrint"/>
...
</handlers>
...
</system.webServer>

- Open the default.aspx page and paste the following markup. The task of this page is to try to detect the WCPP and inform the user to install it if

it's missing.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<style>
body{font: 13px 'Segoe UI', Tahoma, Arial, Helvetica, sans-serif;}
</style>

<%-- WCPP-detection meta tag for IE10 --%>
<%= Neodynamic.SDK.Web.WebClientPrint.GetWcppDetectionMetaTag() %>
</head>
<body>
<form id="form1" runat="server">
<div id="msgInProgress">
<div id="mySpinner" style="width:32px;height:32px"></div>
<br />
Detecting WCPP utility at client side...
<br />
Please wait a few seconds...
<br />
</div>
<div id="msgInstallWCPP" style="display:none;">
<h3>#1 Install WebClientPrint Processor (WCPP)!</h3>
<p>
<strong>WCPP is a native Windows app (without any dependencies!)</strong> that handles all print jobs
generated by the <strong>WebClientPrint ASP.NET component</strong> at the server side. The WCPP
is in charge of the whole printing process and can be
installed on Windows 98, 2000, Me, XP, Vista, Windows 7 and Windows 8 (Desk-mode). It can print to Serial Port RS232 (e.g. COM1),
Parallel Port (e.g. LPT1), your Windows-Installed Printers (USB) and Network/IP Ethernet printers.
</p>
<p>
The <strong>WCPP</strong> utility <strong>is digitally-signed with a Windows Authenticode</strong> issued by <a href="http://www.digicert.com">DigiCert,

Inc.</a>. <strong>Install WCPP only if the <u>Publisher is Neodynamic SRL</u></strong>, otherwise do not proceed. <br /><br />
<a href="http://www.neodynamic.com/downloads/wcpp20-installer.exe" target="_blank">Download and Install WCPP from Neodynamic website</a><br />
</p>
<h3>#2 After installing WCPP...</h3>
<p>
<a href="PrintTxt.aspx">You can go and test the printing page</a>
</p>

</div>
</form>

<%-- Add Reference to jQuery at Google CDN --%>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<%-- Add Reference to spin.js (an animated spinner) --%>
<script src="http://fgnass.github.io/spin.js/dist/spin.min.js"></script>

<script type="text/javascript">

var wcppPingDelay_ms = 10000;

function wcppDetectOnSuccess(){
<%-- WCPP utility is installed at the client side
redirect to WebClientPrint sample page --%>

<%-- get WCPP version --%>
var wcppVer = arguments[0];
if(wcppVer.substring(0,1) == "2")
window.location.href = "PrintTxt.aspx";
else //force to install WCPP v2.0
wcppDetectOnFailure();
}

function wcppDetectOnFailure() {
<%-- It seems WCPP is not installed at the client side
ask the user to install it --%>
$('#msgInProgress').hide();
$('#msgInstallWCPP').show();
}

$(document).ready(function () {
<%-- Create the Spinner with options (spin.js) --%>
var spinner = new Spinner({
lines: 12,
length: 7,
width: 3,
radius: 10,
color: '#336699',
speed: 1,
trail: 60
}).spin($('#mySpinner')[0]);
});

</script>

<%-- WCPP detection script --%>
<%= Neodynamic.SDK.Web.WebClientPrint.CreateWcppDetectionScript() %>

</body>
</html>

- Finally, add a new ASPX page and name it PrintTxt.aspx (be sure to UNSELECT the "Place code in separate file" checkbox)

This page allows your user to print the Text (*.txt) file to local printers. The user is required to have Notepad installed to get this code working.

Notepad is installed with any version of Windows OS.

Please paste the following markup and code depending on your preferred language i.e. C# or VB on your PrintTxt.aspx file.


VB
<%@ Page Language="VB" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Neodynamic.SDK.Web" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Init(sender As Object, e As System.EventArgs)
'Print file?
If (WebClientPrint.ProcessPrintJob(Request)) Then

Dim useDefaultPrinter As Boolean = (Request("useDefaultPrinter") = "checked")
Dim printerName As String = Server.UrlDecode(Request("printerName"))

'full path of the txt file to be printed
Dim txtFilePath As String = "c:\myFile.txt"

'create a temp file name for our txt file...
Dim fileName As String = Guid.NewGuid().ToString("N") + System.IO.Path.GetExtension(txtFilePath)

'Create a PrintFile object with the txt file
Dim file As New PrintFile(txtFilePath, fileName)
'Create a ClientPrintJob and send it back to the client!
Dim cpj As New ClientPrintJob()
'set file to print...
cpj.PrintFile = file
'set client printer...
If (useDefaultPrinter OrElse printerName = "null") Then
cpj.ClientPrinter = New DefaultPrinter()
Else
cpj.ClientPrinter = New InstalledPrinter(printerName)
End If
'send it...
cpj.SendToClient(Response)

End If
End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to directly Print Text files without Printer Dialog</title>
</head>
<body>
<%-- Store User's SessionId --%>
<input type="hidden" id="sid" name="sid" value="<%=Session.SessionID%>" />

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

<h1>How to directly Print Text files without Printer Dialog</h1>
<label class="checkbox">
<input type="checkbox" id="useDefaultPrinter" /> <strong>Use default printer</strong> or...
</label>
<div id="loadPrinters">
<br />
WebClientPrint can detect the installed printers in your machine.
<br />
<input type="button" onclick="javascript:jsWebClientPrint.getPrinters();" value="Load installed printers..." />

<br /><br />
</div>
<div id="installedPrinters" style="visibility:hidden">
<br />
<label for="installedPrinterName">Select an installed Printer:</label>
<select name="installedPrinterName" id="installedPrinterName"></select>
</div>

<br /><br />
<input type="button" style="font-size:18px" onclick="javascript:jsWebClientPrint.print('useDefaultPrinter=' + $('#useDefaultPrinter').attr('checked') +

'&printerName=' + $('#installedPrinterName').val());" value="Print Text File..." />

<script type="text/javascript">
var wcppGetPrintersDelay_ms = 5000; //5 sec

function wcpGetPrintersOnSuccess(){
<%-- Display client installed printers --%>
if(arguments[0].length > 0){
var p=arguments[0].split("|");
var options = '';
for (var i = 0; i < p.length; i++) {
options += '<option>' + p + '</option>';
}
$('#installedPrinters').css('visibility','visible');
$('#installedPrinterName').html(options);
$('#installedPrinterName').focus();
$('#loadPrinters').hide();
}else{
alert("No printers are installed in your system.");
}
}

function wcpGetPrintersOnFailure() {
<%-- Do something if printers cannot be got from the client --%>
alert("No printers are installed in your system.");
}
</script>

</form>

<%-- 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()%>

</body>
</html>

C#
<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Neodynamic.SDK.Web" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Init(object sender, EventArgs e)
{
//Print file?
if (WebClientPrint.ProcessPrintJob(Request))
{

bool useDefaultPrinter = (Request["useDefaultPrinter"] == "checked");
string printerName = Server.UrlDecode(Request["printerName"]);

//full path of the txt file to be printed
string txtFilePath = @"c:\myFile.txt";

//create a temp file name for our txt file...
string fileName = Guid.NewGuid().ToString("N") + System.IO.Path.GetExtension(txtFilePath);

//Create a PrintFile object with the txt file
PrintFile file = new PrintFile(txtFilePath, fileName);
//Create a ClientPrintJob and send it back to the client!
ClientPrintJob cpj = new ClientPrintJob();
//set file to print...
cpj.PrintFile = file;
//set client printer...
if (useDefaultPrinter || printerName == "null")
cpj.ClientPrinter = new DefaultPrinter();
else
cpj.ClientPrinter = new InstalledPrinter(printerName);
//send it...
cpj.SendToClient(Response);

}
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to directly Print Text files without Printer Dialog</title>
</head>
<body>
<%-- Store User's SessionId --%>
<input type="hidden" id="sid" name="sid" value="<%=Session.SessionID%>" />

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

<h1>How to directly Print Text files without Printer Dialog</h1>
<label class="checkbox">
<input type="checkbox" id="useDefaultPrinter" /> <strong>Use default printer</strong> or...
</label>
<div id="loadPrinters">
<br />
WebClientPrint can detect the installed printers in your machine.
<br />
<input type="button" onclick="javascript:jsWebClientPrint.getPrinters();" value="Load installed printers..." />

<br /><br />
</div>
<div id="installedPrinters" style="visibility:hidden">
<br />
<label for="installedPrinterName">Select an installed Printer:</label>
<select name="installedPrinterName" id="installedPrinterName"></select>
</div>

<br /><br />
<input type="button" style="font-size:18px" onclick="javascript:jsWebClientPrint.print('useDefaultPrinter=' + $('#useDefaultPrinter').attr('checked') +

'&printerName=' + $('#installedPrinterName').val());" value="Print Text File..." />

<script type="text/javascript">
var wcppGetPrintersDelay_ms = 5000; //5 sec

function wcpGetPrintersOnSuccess(){
<%-- Display client installed printers --%>
if(arguments[0].length > 0){
var p=arguments[0].split("|");
var options = '';
for (var i = 0; i < p.length; i++) {
options += '<option>' + p + '</option>';
}
$('#installedPrinters').css('visibility','visible');
$('#installedPrinterName').html(options);
$('#installedPrinterName').focus();
$('#loadPrinters').hide();
}else{
alert("No printers are installed in your system.");
}
}

function wcpGetPrintersOnFailure() {
<%-- Do something if printers cannot be got from the client --%>
alert("No printers are installed in your system.");
}
</script>

</form>

<%-- 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()%>

</body>
</html>

- That's it! Run your website and test it. Click on Print Text File to print the txt file without print dialog. You can print it to the Default

client printer
or you can get a list of the installed printers available at the client machine.


Links:
This Demos
Demos
Download WebClientPrint for ASP.NET
More Information about Neodynamic WebClientPrint for ASP.NET

Neodynamic
NET Components & Controls
Barcode, Labeling, Printing & Imaging components for .NET by Neodynamic
 

Neodynamic

Well-known member
Joined
Dec 5, 2005
Messages
137
Programming Experience
10+
How to print multiple files to client printers from ASP.NET

Suppose you have an ASP.NET website and you need to print two different documents or reports to two different printers installed at the client machine. Let's

say you want to print an "Invoice" to Printer1 and a "Dispatch Form" to Printer2. Both Printer1 & Printer2 are available at the client machine.

The new PrintFileGroup feature of WebClientPrint for ASP.NET will allow you to get that working by just writing a few lines of code. The great

thing about this is that you can print those files without displaying any print dialog to the user i.e. a silently printing approach!

In this walkthrough, you'll learn how to print multiple files or documents (like MS Word *.doc or *.docx; MS Excel *.xls or *.xlsx; Adobe PDF; Text files or

images) from an ASP.NET website to different printers installed at the client machine without displaying any print dialog. Best of, this solution works with

any browser on Windows OS like IE (6 or later), Chrome, Firefox, Opera & Safari!

Requirements

Development/Server-side
- WebClientPrint 2.0 for ASP.NET (or greater)
- ASP.NET 3.5 (or greater)
- Visual Studio 2010 / VWD 2010 (or greater)
- jQuery 1.4.1 (or greater)

Client-side

- WebClientPrint Processor 2.0 for Windows
- Microsoft Word for printing *.doc/*.docx files
- Microsoft Excel for printing *.xls/*.xlsx files
- Adobe Reader or Foxit Reader (recommended) for printing *.pdf files

Follow up these steps
- Download & install WebClientPrint for ASP.NET
- Open Visual Studio and create a new ASP.NET 3.5 Website naming it PrintMultipleFilesSample
- Add a reference to Neodynamic.SDK.WebClientPrint.dll to your project
- Open your web.config file and add the following entries:

<system.web>
...
<httpHandlers>
...
<add verb="*" path="wcp.axd" type="Neodynamic.SDK.Web.WebClientPrint, Neodynamic.SDK.WebClientPrint"/>
...
</httpHandlers>
...
</system.web>

<system.webServer>
...
<handlers>
...
<add name="WCP" verb="*" path="wcp.axd" type="Neodynamic.SDK.Web.WebClientPrint, Neodynamic.SDK.WebClientPrint"/>
...
</handlers>
...
</system.webServer>

- Open the default.aspx page and paste the following markup. The task of this page is to try to detect the WCPP and inform the user to install it if

it's missing.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<style>
body{font: 13px 'Segoe UI', Tahoma, Arial, Helvetica, sans-serif;}
</style>

<%-- WCPP-detection meta tag for IE10 --%>
<%= Neodynamic.SDK.Web.WebClientPrint.GetWcppDetectionMetaTag() %>
</head>
<body>
<form id="form1" runat="server">
<div id="msgInProgress">
<div id="mySpinner" style="width:32px;height:32px"></div>
<br />
Detecting WCPP utility at client side...
<br />
Please wait a few seconds...
<br />
</div>
<div id="msgInstallWCPP" style="display:none;">
<h3>#1 Install WebClientPrint Processor (WCPP)!</h3>
<p>
<strong>WCPP is a native Windows app (without any dependencies!)</strong> that handles all print jobs
generated by the <strong>WebClientPrint ASP.NET component</strong> at the server side. The WCPP
is in charge of the whole printing process and can be
installed on Windows 98, 2000, Me, XP, Vista, Windows 7 and Windows 8 (Desk-mode). It can print to Serial Port RS232 (e.g. COM1),
Parallel Port (e.g. LPT1), your Windows-Installed Printers (USB) and Network/IP Ethernet printers.
</p>
<p>
The <strong>WCPP</strong> utility <strong>is digitally-signed with a Windows Authenticode</strong> issued by <a href="http://www.digicert.com">DigiCert,

Inc.</a>. <strong>Install WCPP only if the <u>Publisher is Neodynamic SRL</u></strong>, otherwise do not proceed. <br /><br />
<a href="http://www.neodynamic.com/downloads/wcpp20-installer.exe" target="_blank">Download and Install WCPP from Neodynamic website</a><br />

</p>
<h3>#2 After installing WCPP...</h3>
<p>
<a href="PrintMyFiles.aspx">You can go and test the printing page</a>
</p>

</div>
</form>

<%-- Add Reference to jQuery at Google CDN --%>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<%-- Add Reference to spin.js (an animated spinner) --%>
<script src="http://fgnass.github.io/spin.js/dist/spin.min.js"></script>

<script type="text/javascript">

var wcppPingDelay_ms = 10000;

function wcppDetectOnSuccess(){
<%-- WCPP utility is installed at the client side
redirect to WebClientPrint sample page --%>

<%-- get WCPP version --%>
var wcppVer = arguments[0];
if(wcppVer.substring(0,1) == "2")
window.location.href = "PrintMyFiles.aspx";
else //force to install WCPP v2.0
wcppDetectOnFailure();
}

function wcppDetectOnFailure() {
<%-- It seems WCPP is not installed at the client side
ask the user to install it --%>
$('#msgInProgress').hide();
$('#msgInstallWCPP').show();
}

$(document).ready(function () {
<%-- Create the Spinner with options (spin.js) --%>
var spinner = new Spinner({
lines: 12,
length: 7,
width: 3,
radius: 10,
color: '#336699',
speed: 1,
trail: 60
}).spin($('#mySpinner')[0]);
});

</script>

<%-- WCPP detection script --%>
<%= Neodynamic.SDK.Web.WebClientPrint.CreateWcppDetectionScript() %>

</body>
</html>

- Finally, add a new ASPX page and name it PrintMyFiles.aspx (be sure to UNSELECT the "Place code in separate file" checkbox)

Here we'll construct the code that allows many files to be printed to different printers at the client side. To get a file to be printed to a specified

printer, you must specify a file name with the following format:

fileName + "_PRINT_TO_" + printerName + fileExtension

So, if we have an Invoice.doc file that we want to print to Printer1 on the client machine, then the file name must be formatted to this

Invoice_PRINT_TO_Printer1.doc


Please paste the following markup and code depending on your preferred language i.e. C# or VB on your PrintMyFiles.aspx file.

VB

<%@ Page Language="VB" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Neodynamic.SDK.Web" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

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

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.PrintFileGroup.Add(New PrintFile(context.Server.MapPath("~/files/Invoice.doc"), "Invoice_PRINT_TO_Printer1.doc"))
'DispatchForm.xls PRINT TO Printer2
cpj.PrintFileGroup.Add(New PrintFile(context.Server.MapPath("~/files/DispatchForm.xls"), "DispatchForm_PRINT_TO_Printer2.xls"))
'send it...
cpj.SendToClient(Response)

End If

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to print multiple files to client printers from ASP.NET</title>
</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 />
<input type="button" style="font-size:18px" onclick="javascript:jsWebClientPrint.print();" value="Print Files..." />

</form>

<%-- 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()%>

</body>
</html>

C#

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Neodynamic.SDK.Web" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Init(object sender, EventArgs e)
{

if (WebClientPrint.ProcessPrintJob(Request))
{
//Create a ClientPrintJob
ClientPrintJob cpj = 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.PrintFileGroup.Add(new PrintFile(context.Server.MapPath("~/files/Invoice.doc"), "Invoice_PRINT_TO_Printer1.doc"));
//DispatchForm.xls PRINT TO Printer2
cpj.PrintFileGroup.Add(new PrintFile(context.Server.MapPath("~/files/DispatchForm.xls"), "DispatchForm_PRINT_TO_Printer2.xls"));
//send it...
cpj.SendToClient(Response);

}
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to print multiple files to client printers from ASP.NET</title>
</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 />
<input type="button" style="font-size:18px" onclick="javascript:jsWebClientPrint.print();" value="Print Files..." />

</form>

<%-- 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()%>

</body>
</html>

- That's it! Run your website and test it. Remember to change the file names & printer names in the source code to match yours there.


Links:
This Demos
Demos
Download WebClientPrint for ASP.NET
More Information about Neodynamic WebClientPrint for ASP.NET

Neodynamic
NET Components & Controls
Barcode, Labeling, Printing & Imaging components for .NET by Neodynamic
 
Top Bottom