How to print a PDF with C#

前端 未结 11 569
抹茶落季
抹茶落季 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:32

    As of July 2018, there is still no answer for the OP. There is no free way to 1) silently print your pdf for a 2) closed source project.

    1) You can most certainly use a process i.e. Adobe Acrobat or Foxit Reader

    2) Free solutions have a GPL (GNU's General Public License). This means you must open your source code if giving the software, even for free, to anyone outside your company.

    As the OP says, if you can get a PDF to Drawing.Image, you can print it with .NET methods. Sadly, software to do this also requires payment or a GPL.

    0 讨论(0)
  • 2020-12-14 18:37

    Another approach would to use spooler function in .NET to send the pre-formatted printer data to a printer. But unfortunately you need to work with win32 spooler API

    you can look at How to send raw data to a printer by using Visual C# .NET

    you only can use this approach when the printer support PDF document natively.

    0 讨论(0)
  • 2020-12-14 18:40
            Process proc = new Process();
            proc.StartInfo.FileName = @"C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe";
            proc.StartInfo.Arguments = @"/p /h C:\Documents and Settings\brendal\Desktop\Test.pdf";
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.CreateNoWindow = true;
            proc.Start();
    
            for (int i = 0; i < 5; i++)
            {
                if (!proc.HasExited)
                {
                    proc.Refresh();
                    Thread.Sleep(2000);
                }
                else
                    break;
            }
            if (!proc.HasExited)
            {
                proc.CloseMainWindow();
            }
    
    0 讨论(0)
  • 2020-12-14 18:46

    I tried many things and the one that worked best for me was launching a SumatraPDF from the command line:

    // Launch SumatraPDF Reader to print
    String arguments = "-print-to-default -silent \"" + fileName + "\"";
    System.Diagnostics.Process.Start("SumatraPDF.exe", arguments);
    

    There are so many advantages to this:

    1. SumatraPDF is much much faster than Adobe Acrobat Reader.
    2. The UI doesn't load. It just prints.
    3. You can use SumatraPDF as a standalone application so you can include it with your application so you can use your own pa. Note that I did not read the license agreement; you should probably check it out yourself.
    0 讨论(0)
  • 2020-12-14 18:49

    If you're interested in commercial solutions which do exactly what you require then there are quite a few options. My company provides one of those options in a developer toolkit called Debenu Quick PDF Library.

    Here is a code sample (key functions are PrintOptions and PrintDocument):

    /* Print a document  */
    
    // Load a local sample file from the input folder
    
    DPL.LoadFromFile("Test.pdf", "");
    
    // Configure print options
    
    iPrintOptions = DPL.PrintOptions(0, 0, "Printing Sample")
    
    // Print the current document to the default 
    // printing using the options as configured above.
    // You can also specify the specific printer.
    
    DPL.PrintDocument(DPL.GetDefaultPrinterName(), 1, 1, iPrintOptions);
    
    0 讨论(0)
提交回复
热议问题