How to print a PDF with C#

前端 未结 11 571
抹茶落季
抹茶落季 2020-12-14 17:58

I´ve trying to solve this problem for nearly 2 days. There are a lot of more or fewer good solutions on the net, but not a single one fits my task perfectly.

11条回答
  •  一整个雨季
    2020-12-14 18:29

    I know that the tag has Windows Forms; however, due to the general title, some people might be wondering if they may use that namespace with a WPF application -- they may.

    Here's code:

    var file = File.ReadAllBytes(pdfFilePath);
    var printQueue = LocalPrintServer.GetDefaultPrintQueue();
    
    using (var job = printQueue.AddJob())
    using (var stream = job.JobStream)
    {
        stream.Write(file, 0, file.Length);
    }
    

    Now, this namespace must be used with a WPF application. It does not play well with ASP.NET or Windows Service. It should not be used with Windows Forms, as it has System.Drawing.Printing. I don't have a single issue with my PDF printing using the above code.

    Note that if your printer does not support Direct Printing for PDF files, this won't work.

提交回复
热议问题