Best way to print for Windows Clients (Not Web Apps)?

后端 未结 7 1881
青春惊慌失措
青春惊慌失措 2021-02-05 18:43

What is the best way to print stuff from c#/.net?

The question is in regard to single pages as well as to reports containing lots of pages.

It would be great to

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-05 18:58

    Can Print via Adobe Acrobat

    I use the standard libraries such as System.Diagnostics.ProcessStartInfo to use Adobe Acrobat to print a pdf. The end user will not have to interact with the Acrobat GUI, though, annoyingly, the following code still pulls it on-screen for a few seconds.

        // Sample fileName = System.Environment.GetFolderPath(
        // System.Environment.SpecialFolder.CommonApplicationData)
        //  + @"\MyCompany\MyProject\TestPrint.pdf"
        private void SendPrintJob(string fileName)
        {
            try
            {
               // Start by finding Acrobat from the Registry.
               // This supposedly gets whichever you have of free or paid
                string processFilename = Microsoft.Win32.Registry.LocalMachine
                     .OpenSubKey("Software")
                     .OpenSubKey("Microsoft")
                     .OpenSubKey("Windows")
                     .OpenSubKey("CurrentVersion")
                     .OpenSubKey("App Paths")
                     .OpenSubKey("AcroRd32.exe")
                     .GetValue(String.Empty).ToString();
    
                ProcessStartInfo info = new ProcessStartInfo();
                info.Verb = "print";
                info.FileName = processFilename;
                info.Arguments = String.Format("/p /h {0}", fileName);
                info.CreateNoWindow = true;
                info.WindowStyle = ProcessWindowStyle.Hidden;
                info.UseShellExecute = false;
    
                Process p = new Process();
                p.StartInfo = info;
                p.Start();
    
                p.WaitForInputIdle();
    
                // Recommended to add a time-out feature. Mine is coded here.
            }
            catch (Exception e)
            {
                Console.WriteLine("Error sending print job. " + e.Message);
            }
    

    Can Manipulate via PDFSharp/MigraDoc

    I did not read document manipulation into the OP, but I see other answers commenting on the fact. A few StackOverflow Q&As from 2008-2012 (including @Robert Gowland from this question) say that PDFSharp / MigraDoc have poor documentation.

    In 2018, I have found it to be an easy learning curve, with many samples at the homepage. I read this question this morning to figure out how to print a graph, and now have a button to screen-shot my application and print.

    You will want to go to NuGet package manager for PDFsharp-MigraDocs (or PDFsharp-MigraDocs-WPF, or PDFsharp-MigraDocs-GDI). MigraDocs is the high-level component that can create documents from elements, without care of if they are pdf or image or what have you. PDFSharp is the component that helps to, i.e., rearrange documents, put multiple documents on a page, and break apart content from one to two pages.

提交回复
热议问题