简体   繁体   中英

How to open print dialog box of any application (like word,pdf reader) on the same time while opening the document, Programmatically

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

You can do this with some components like Aspose

Exemple : Here

you could do something like the following

String FileName = filename;
String FilePath = filepath;
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath + FileName);
response.Flush();
response.End();

If you are wanting to download the .pdf file for example , once downloaded you could open then file via code behind .. but you would have to write separate code for that..

You can't do that from ASP.NET or any web-platform, browsers are sandboxed, imagine the chaos if you were aloud to execute commands on a users PC !!!

You would have to create your own client app that the end user would have to install.

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 :

Private Sub Workbook_Open()

    Application.Dialogs(xlDialogPrint).Show

End Sub

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM