2

We need to print a PDF from our c# application (without the need of an external library to install and without having a print dialog)

I know there is a lot of discussions about it (mostly outdated solution or freeware tool/library) but maybe someone could point me to the best solution to accomplish this ? Another way could be to convert the PDF to image and send the image directly to the printer if I can find an easy way to convert a PDF page to an image.

Thanks!

michelqa
  • 137
  • 1
  • 2
  • 14

1 Answers1

2

This doesn't require a library, but it does require you to have Adobe Reader DC on the machine the application is on. If you don't want to use any type of external tool then you'll need to create your own functionality to do this. Adobe Reader DC can be invoked with a command to allow you to print the document. This isn't an elegant solution at all for error handling or closing the process, but it's a skeleton that you can tweak:

private static void PrintDocument(string fileName)
    {
        var process = new Process
        {
            StartInfo =
            {
                WindowStyle = ProcessWindowStyle.Hidden,
                Verb = "print",
                FileName = @"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe", //You could use an app config string here
                Arguments = $@"/p /h {fileName}",
                UseShellExecute = false,
                CreateNoWindow = true
            }
        };

        process.Start();

        if (process.HasExited == false)
        {
            process.WaitForExit(10000);
        }

        process.EnableRaisingEvents = true;

        try
        {
            //Try to gracefully exit the process first
            var proccessIsClosed = process.CloseMainWindow();

            //If it doesn't gracefully close, kill the process
            if (!proccessIsClosed)
            {
                process.Kill();
            }
        }
        catch
        {
            throw new Exception("Process ID " + process.Id +
                                           " is unable to gracefully close. Please check current running processes.");
        }
    }
Ryan Intravia
  • 420
  • 6
  • 12
  • 1
    Thanks! But launching an adobe process to print the document is not what I need since we can see the adobe reader poping up before sending the document to the printer. If I want to try an external library to juste print a pdf document to a network printer, What is the best / small library to do this ? – michelqa Jun 06 '16 at 18:52
  • There are a handful of third party libraries/solutions mentioned here: http://stackoverflow.com/questions/5566186/print-pdf-in-c-sharp. I haven't used any of them personally so I can't say whether or not a pop-up occurs during printing. – Ryan Intravia Jun 06 '16 at 19:12