Reputation: 91
I want to print a PDF file (url) from windows application without opening print dialog.
I have tried the code bellow
string pdfUrl="mysite.com/test.pdf";
string printerName = "Microsoft Print To PDF";
using (var client = new System.Net.WebClient())
{
client.Proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
client.DownloadFile(pdfUrl, filePath);
}
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = filePath;
info.Arguments = "\"" + printerName + "\"";
info.UseShellExecute = true;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.WorkingDirectory = Path.GetDirectoryName(filePath);
Process p = new Process();
p.StartInfo = info;
p.Start();
//p.WaitForInputIdle();
//System.Threading.Thread.Sleep(3000);
//if (false == p.CloseMainWindow())
// p.Kill();
but getting error in p.Start(); bellow System.ComponentModel.Win32Exception: No application is associated with the specified file for this operation
What is missing?
Please suggest how to solve this.
Upvotes: 0
Views: 7804
Reputation: 19
My opinion is that you should use any third party library to printing PDFs. We are using following C# PDF library https://pdfium.patagames.com/c-pdf-library/
And yes, this is commercial library, so I don't know if I have a right to place a link here.
Here is a code to print PDF's without any user interaction:
public void PrintPdf()
{
var doc = PdfDocument.Load("c:\test.pdf");
var printDoc = new PdfPrintDocument(doc);
PrintController printController = new StandardPrintController();
printDoc.PrintController = printController;
printDoc.Print(); // Print PDF document
}
Upvotes: 1