I want to send a pdf file to a printer programmatically and I'm wondering if there is a way of printing a file (send for example a pdf to a printer ) using .net standard libraries (PrintDowument class) or an open source one with MIT license?
Thanks
I tried this
ProcessStartInfo info = new ();
info.Verb = "print";
info.Arguments = "\"" +printerName + "\"";
info.FileName = pdfFilePath;
info.CreateNoWindow = true;
info.UseShellExecute = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
using Process p = new ();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
Thread.Sleep(3000);
if (!p.HasExited)
{
p.Kill();
}
here I have to specify which process to use (Adobe or another) but what I want is to take a pdf and send it to a specified printer without calling an external process.
One possibility is CoreWebView2. It is the same component used by Microsoft Edge for showing content - including PDF documents. Since this is a graphical user control, it only really makes sense for use in client facing applications (WinUI, WPF, Winforms, etc.) where you want to print documents from a document preview. It was not designed for a service even though you may be able to get it to work. (I haven't tried.)
Check these two methods:
Here's an overview of three methods for printing from the control.
Also, a hint for getting started is to make sure to use the CoreWebView2InitializationCompleted event to initialize the control or you'll run into errors related to null values while setting properties. This is a rough template for getting started. I used this with a C# Winforms application in .Net6: