Print Pdf in C#
Solution 1
A very straight forward approach is to use an installed Adobe Reader or any other PDF viewer capable of printing:
Process p = new Process( );
p.StartInfo = new ProcessStartInfo( )
{
CreateNoWindow = true,
Verb = "print",
FileName = path //put the correct path here
};
p.Start( );
Another way is to use a third party component, e.g. PDFView4NET
Solution 2
I wrote a little helper method around the adobereader to bulk-print pdf from c#...:
public static bool Print(string file, string printer) {
try {
Process.Start(
Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\Microsoft\Windows\CurrentVersion" +
@"\App Paths\AcroRd32.exe").GetValue("").ToString(),
string.Format("/h /t \"{0}\" \"{1}\"", file, printer));
return true;
} catch { }
return false;
}
One cannot rely on the return-value of the method btw...
Solution 3
Another approach, if you simply wish to print a PDF file programmatically, is to use the LPR command: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/lpr
LPR is available on newer versions of Windows too (e.g. Vista/7), but you need to enable it in the Optional Windows Components.
For example:
Process.Start("LPR -S printerdnsalias -P raw C:\files\file.pdf");
You can also use the printer IP address instead of the alias.
This assumes that your printer supports PDF Direct Printing otherwise this will only work for PostScript and ASCII files. Also, the printer needs to have a network interface installed and you need to know it's IP address or alias.
Solution 4
Use PDFiumViewer. I searched for a long time till I came up with a similar solution, then I found this clean piece of code that does not rely on sending raw files to the printer (which is bad if they get interpreted as text files..) or using Acrobat or Ghostscript as a helper (both would need to be installed, which is a hassle):
https://stackoverflow.com/a/41751184/586754
PDFiumViewer comes via nuget, the code example above is complete. Pass in null values for using the default printer.
Solution 5
I had the same problem on printing a PDF file. There's a nuget package called Spire.Pdf that's very simple to use. The free version has a limit of 10 pages although, however, in my case it was the best solution once I don't want to depend on Adobe Reader and I don't want to install any other components.
https://www.nuget.org/packages/Spire.PDF/
PdfDocument pdfdocument = new PdfDocument();
pdfdocument.LoadFromFile(pdfPathAndFileName);
pdfdocument.PrinterName = "My Printer";
pdfdocument.PrintDocument.PrinterSettings.Copies = 2;
pdfdocument.PrintDocument.Print();
pdfdocument.Dispose();
Related videos on Youtube
Corbee
Updated on July 09, 2022Comments
-
Corbee almost 2 years
I'm new to c#. I was looking all over the net for tutorials on how to print pdf, but couldn't find one.
Then I thought, is it possible to read it using itextpdf, like mentioned here
Reading PDF content with itextsharp dll in VB.NET or C#
then print it. If so, how?
-
santa almost 11 years@ColtonMilne afair it's the same name that shows up in the control panel... from code: give the system.drawing.printing.printersettings.installedprinters property a shot!
-
yoel halb over 10 yearsGreat answer, but note that Process inherits from component which implements IDisposable, so it is of course recommended to dispose it or use a using statement, especially if one uses it many times
-
santa almost 10 yearsafaik as soon as you threw your job into the spooler it's "his thing" - so you'd have to talk to the print spooler: codeproject.com/Articles/51085/…
-
Nyerguds about 6 yearsOhh, you're using shellexecute, with the "print" verb. Right. Do note Adobe Reader's latest versions don't seem to properly close the window after printing. Also, this will probably not be a silent print command; it'll show a popup for print options.
-
SwissCoder over 5 yearsPdfiumViewer enabled me printing pdfs. don't waste your time on spire.pdf (outdated examples/documentation and no valid license provided even after registering with email)
-
Jireugi about 5 yearsBut if you don´t mind a bit additional programming work (in Java) then you could replace RawFilePrinter.exe by a custom program using the open source Apache PDFBox link.
-
Imran Faruqi over 3 yearsYou haven't provided any "how-to" and dependencies, would you be kind enough to do so? Also, would it work with .Net Core?
-
oo_dev over 3 yearsI print by commandline using sumatra.exe (just 1 exe and no bloatware necessary) and btw you have some options to configure printing: sumatrapdfreader.org/docs/Command-line-arguments.html
-
stomy over 3 yearsSee PDFsharp on Github for the latest version source code.
-
stomy over 3 yearsPDFsharp uses Adobe
acrord32.exe
to print. See source code. -
Michael about 3 yearsSpire PDF is commercial software that needs a paid license. Comments that recommend packages should be clear on the difference between open source and commercial purchases. The free version prints a banner in the PDF so even if you are printing small files, it will show up.
-
Murilo about 3 yearsThis answer is quite old. At that time the only limitation was the page number. So now it has a banner as you said. Good to know to alert new users.