by Mikael Henriksson
27. January 2009 18:59
This turns out to be a royal pain in the a$$. Actually I do not want to print a pdf, I want to print from SQL Reporting Services Express and it CAN be done but the result is less than flattering. After hours of searching and only finding libraries I either have to pay for or spend too much time configuring I finally found a pdf reader that takes a file AND printer as arguments and redirects to that specified printer without even opening a “window” (yes I am using windows :) ). Don’t even think about using Adobe ever! If you are using Adobe please uninstall that piece of crap and install the free version of Foxit PDF Reader. It is completely free of charge and let’s you print pdf’s from the the command prompt.
Anyway, the code to generate the pdf from C# isn’t all that hard to find on the internet. The code to send that piece of work to the printer probably is not common knowledge though so I’ll at least share that with you! First generate your report, get the stream then write that SQL Report stream to a PDF and get the full path to the file and this is all you have to do.
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = @"C:\Program Files\Foxit Software\Foxit Reader\FoxitReader.exe";
string Arguments = String.Format(@"{0} /t {1}", @"C:\Full Path To\File.pdf", @"\\Server\Printer");
proc.StartInfo.Arguments = Arguments;
proc.Start();
Compare that to Adobe’s Print Verb. If you are using Adobe you have to wait for dull/slow/boring/not working-exit. I found that when I used the proc.StartInfo.Verb = “Print” from adobe it would not print all of the times I used it. Further more it would give me file system errors etc until I restarted the server because Adobe Reader would actually lock the file and keep that lock. So if something went wrong with the print a new file could not overwrite the existing one until restart.
This new approach is just beautiful, fast & furious!
Have fun printing those documents of yours :)
88a332e4-5633-4673-ac97-0d8572f15ad4|0|.0
Tags: C#
C#