Working with PDFs in the way you do with other programming languages, ain't so easy with C#. Pitifully, most of the libraries are commercial and you will need to pay for a license. Other libraries like Spire PDF offers a free version of the library, however it only supports up to 10 pages per PDF and then you will see a watermark message that it's only for test purposes.
In case you are willing to print a PDF from your Winforms application without using a paid API, we'll show you 2 workarounds that will help you to print a PDF file easily.
A. Using Adobe Acrobat
The first way requires that you user has Adobe Acrobat installed. Usually every computer has a program to read PDF files namely Acrobat Reader, so be sure to check that you user has this PDF reader installed.
- We can use the iframe to display contents of our PDF document and then print the contents using a JavaScript code. However, you can also directly print the PDF document, without opening the file. In both the cases, we'll need an iframe. Print PDF using inline iframe. In the first example, I have an iframe on my web page with a source (the PDF.
- PDF files created using PDF version 1.5 Crypt Filter functions or more than eight DeviceN Color Space components cannot be printed. PDF files created using PDF version 1.6 watermark note functions, or extended optional contents cannot be printed. PDF version 1.7 (Acrobat 8.0) files created without any functions that are exclusive to PDF version.
- Why is my pdf printing to file instead of to printer selected? (Acrobat Pro 10) Recently I have been unable to print to a physical printer (to print hard copy). Regardless of which printer is selected in the Printer dialog box, Acrobat tries to print to file - a ps file.
Print Documents Directly To Pdf File
Load the PDF document you want to print. Check to see if you're still connected to the same wireless network as the printer. Make adjustments to how you want to print (one-sided, two-sided, font, etc.) Search for a printer from the list of available devices and tap print.
This method is recommended as it allows the user to choose which pages to print, which printer to use and other settings with the native print dialog:
This code runs the print command in a hidden command line and displays the Print Dialog of the system: Starcraft starter edition.
In some Windows versions, the Acrobat Reader may start for a second but it will be automatically closed after you click ok and the file will be printed.
B. Using the RawPrint package
If you don't want to use Acrobat Reader, you may want to use a custom package that sends a file directly to the printer. We are talking about the RawPrint package. RawPrint is a ibrary to send files directly to a Windows printer bypassing the printer driver, it can send PostScript, PCL or other print file types directly to a printer.
To install this library on your project, you can use the NuGet package manager of Visual Studio. Go to the solution explorer of the project in Visual Studio and do right click on your project, from the list select the Manage NuGet Packages option:
Once the Manager shows up, search for the RawPrint library, select (probably) the first option by Tony Edgecombe and click on install:
After the installation you will be able to use the RawPrint class in your project. For more information about this library, please visit the official repository at Github here.
1. List Printer Names
It's recommendable to list all the available printers in some combo box, so the user only needs to select the desired Printer and then print it. You can list all the available printers using the InstalledPrinters property in the PrinterSettings type:
Print Documents Directly To Pdf Files
If you don't want to use Acrobat Reader, you may want to use a custom package that sends a file directly to the printer. We are talking about the RawPrint package. RawPrint is a ibrary to send files directly to a Windows printer bypassing the printer driver, it can send PostScript, PCL or other print file types directly to a printer.
To install this library on your project, you can use the NuGet package manager of Visual Studio. Go to the solution explorer of the project in Visual Studio and do right click on your project, from the list select the Manage NuGet Packages option:
Once the Manager shows up, search for the RawPrint library, select (probably) the first option by Tony Edgecombe and click on install:
After the installation you will be able to use the RawPrint class in your project. For more information about this library, please visit the official repository at Github here.
1. List Printer Names
It's recommendable to list all the available printers in some combo box, so the user only needs to select the desired Printer and then print it. You can list all the available printers using the InstalledPrinters property in the PrinterSettings type:
Print Documents Directly To Pdf Files
To use the RawPrint methods, you will need to specify which printer to use. The only way to specify which to use is the name, so be sure to have a name of the printer before sending a file to print. In our case, we have available a real Brother Printer, so we'll use it in this example.
Print Documents Directly To Pdf Document
2. Printing PDF
To print the PDF from a file, you only need to use the PrintRawFile method from an instance of RawPrint. This method expects as first argument, the name of the Printer that you want to use to print the file, as second argument the absolute path to the PDF file (including the filename) that you want to print and as last argument the name of the same file. This last argument is used only to display it as name in the queue:
If you execute the previous code and the file exists, the printer should now doing its Job.
Print Documents Directly To Pdf Converter
Happy coding !