I am using C#, asp.net as tools.
I am using the following code to transmit and open a file at client side and want to open built-in print dialog box of any application (like word,pdf reader) on the same time while opening the document at client side.
if (lfileFormat.ToUpper() == "Excel")
{
Response.ContentType = "application/vnd.ms-excel";
}
else if (lfileFormat.ToUpper() == "PDF")
{
Response.ContentType = "application/pdf";
}
else if (lfileFormat.ToUpper() == "HTML")
{
Response.ContentType = "text/HTML";
}
Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}", fi.Name));
Response.AddHeader("Content-Length", fi.Length.ToString());
Response.TransmitFile(fi.FullName);
Response.End();
There is no straight-forward way to do that. You are generating your file server-side, and sending it to the client. You can’t control what is happening on the client’s computer (as HollyStyles mentions, imagine the mess).
Your best bet is to include a macro inside the Excel/Word file so that it will show the print dialog on file open. Easiest way to do without messing with VBE (Visual Basic Extensibility, which allows to dynamically manipulate code modules of Office documents) is to create a template that includes the functionnality, and use that template to generate your documents.
For exemple, in an Excel worbook open event handler, you can have :
But that has other implications (make sure your documents are signed, or that the users allow execution of macros, …). Not to mention that the Print dialog will show up everytime the user the file, which can be… irritating.
Additionnally, that won’t solve the problem for non-office files. You could use Window.Print in JS to handle those case.
But all in all, it’s going to be messy 🙂