I'm trying to make a function in VB.NET that allows the user to print a .PDF file from a folder. Said function must have the following specifications:
- The quality of the printed file must be the same as the original, with no degradation.
- The user must be able to select a printer and edit the print settings before printing the file. Duplex printing, in particular, is a must.
- The program must work properly, regardless of the printer's specifications or the user's installed programs (this function will be part of a program that will need to be installed on different computers, so I can't make assumptions on the type of printer, or whether the user has Adobe or not).
- The function must not open other programs while printing.
- The function must not rely on licensed or paid DLLs.
I've already tried the methods detailed below, but none of them met all the requisites: I've also searched many different sites, but all answers led to either a method I've already tried or a suggestion to use third-party DLLs. Can anyone recommend me an alternative method, or give me a suggestion on how to solve the issues with the ones I've already tried? Thanks in advance.
What I have tried:
The methods I've tried are the following:
- I've tried using iTextSharp to convert each page in an image, and then print said images using PrintDocument. This method met almost all specifications, but the images generated this way were really degraded compared to the original, so I had to look for another solution.
- I tried using ProcessStartInfo with code similar to this, so that the system would use the default print program for the task:
Using dialogPrint As New PrintDialog
dialogPrint.AllowPrintToFile = True
dialogPrint.AllowSomePages = True
If dialogPrint.ShowDialog = DialogResult.OK Then
Dim proc As New Process
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
proc.StartInfo.Verb = "PrintTo"
proc.StartInfo.FileName = selectedFile
proc.StartInfo.Arguments = """" & dialogPrint.PrinterSettings.PrinterName & """"
proc.StartInfo.CreateNoWindow = True
proc.StartInfo.UseShellExecute = True
proc.Start()
If Not proc.HasExited Then
proc.WaitForExit(10000)
End If
proc.Close()
End If
End Using
This method, however, failed to meet several specifications: not only it doesn't allow me to print the file in duplex mode, but it also opens Adobe every time I try to print the document. Also, since the function relies on a program already installed, I'm not sure this method is necessarily reliable.
- I tried using the method described in
this page to send the file directly to the printer. This method, however, was too unreliable: the result was perfect when printing to file, but whenever I tried sending the file to a physical printer, it didn't print anything.