Click here to Skip to main content
15,867,765 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:
VB
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.
Posted
Updated 22-Dec-16 23:33pm
Comments
Richard MacCutchan 23-Dec-16 5:30am    
You cannot do it like that. You can only print from an application that can read and format a PDF file, such as Adobe, Adobe Reader etc. And the remaining requirements are dependent on the features of the printer.

1 solution

You cannot print a PDF file without some support application that can read and format the content; such as Acrobat Reader, Foxit etc. Or you could write your own using iTextSharp or similar support library. And the remaining requirements of the print features are dependent on the type of printer installed at the user's site.
 
Share this answer
 
Comments
Carlo Piras 23-Dec-16 6:14am    
Using iTextSharp for converting the .pdf was in fact my first idea, but all images generated by it were too low-quality: that's why I had to look for a different solution.
[no name] 28-Dec-16 20:54pm    
Maybe this will help you: https://code.msdn.microsoft.com/Print-PDF-file-in-C-5b6e66b7
Richard MacCutchan 29-Dec-16 3:26am    
Please make your links clickable.
Carlo Piras 3-Jan-17 9:52am    
As I mentioned in the requisites list, I can't use licensed or paid DLLs for this project: were it not for that, I'd easily agree that Spire would be perfect.
Richard MacCutchan 3-Jan-17 10:42am    
Then you are on your own with this project.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900