简体   繁体   中英

Printing .PDF from Winform Application

I have an application that executes an SSRS report to create a file, which is saved locally in PDF. I would like to print the file, but we have been experiencing problems with the Acrobat Reader security settings, which are stopping the print from happening - our security guys and app support have said that Acrobat is not an option and I need to find another way to print the report.

All of the computers that will be used to run the app have Google Chrome installed, so it seems sensible to me to use this to open and print the PDF. We can set chrome as the default application for opening PDF files, and with this done, I had the following code snippet running (after some research online):

Process process = new Process();
PrintDialog printDialog = new PrintDialog();

numberOfCopies = printDialog.PrinterSettings.Copies;
ProcessStartInfo starter = new ProcessStartInfo();

starter.FileName = "\"" + printFileName + "\"";
starter.Arguments = "\"" + printDialog.PrinterSettings.PrinterName + "\"";

//starter.Verb = "printto";

for (short s = 1; s <= numberOfCopies; s++)
{
    starter.CreateNoWindow = true;
    starter.WindowStyle = ProcessWindowStyle.Hidden;
    starter.UseShellExecute = true;
    process.StartInfo = starter;
    process.Start();

//... more code

With the line starter.Verb = "printto"; commented, chrome opens the PDF, but adding the verb causes an error to be returned saying there is no default app for that file type. I have been doing some research and it seems maybe I should be using headless mode, but all I have found there is command line stuff and ASP.NET.

I believe that a previous developer had problems with MS Word as well, so would prefer to avoid that route if possible - I think there was something to do with Citrix, but not sure.

Does Chrome support the use of the print command like this, or am I on a hiding to nothing? If it does support, what am I doing wrong / how could I do it better (eg headless mode?)? If it does not support, is there some other way that I can use to print a PDF without using acrobat?

Google chrome has no command line switch for "printto". Hence when you use the verb "printto" there is an error. Below are the list of command line switches supported by Chrome.

https://peter.sh/experiments/chromium-command-line-switches/

There is a switch "--print" in chrome. But that doesn't print pdf file.

If you are able to print pages using Acrobat reader then it should work. It would be easier for me to help if you can give more details on the security settings issue that you were facing. Having said that, I have been using Acrobat reader to initiate PDF printing in my Windows forms application for a fair amount of time . They have the "print" and "printto" verbs implemented. And it works well in all of production deployment.

System.Diagonstics.Process class does what "Start-Process" does on Powershell. We can test printing using "Start-Process" without having to run the C# code. Set the default application for opening pdf as Acrobat reader. Run the below sample code by changing pdf location.

Start-Process "C:\DevOps-with-ASP.NET-Core-and-Azure.pdf" -Verb printto "OneNote"

By this we can identify the security issue and do necessary changes to allow printing through Acrobat Reader.

Printing pdf through a browser using Process class is not feasible as of now. Even the Microsoft's own edge browser doesn't support printing through Process. Below is the error we get when we try to do same in powershell

PS C:\Users\darsh> Start-Process "C:\DevOps-with-ASP.NET-Core-and-Azure.pdf" -Verb printto "OneNote"
Start-Process : This command cannot be run due to the error: No application is associated with the specified file for
this operation.
At line:1 char:1
+ Start-Process "C:\DevOps-with-ASP.NET-Core-and-Azure.pdf" -Verb print ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

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