|
-
Feb 11th, 2008, 05:32 PM
#1
Silently Print PDF with ShellExecute
I spent hours searching for a way to print pdf's without opening Acrobat and finally got it working using ShellExecute. I figured I would share
vb.net Code:
Imports System.Runtime.InteropServices
Imports System.IO
Public Class PDFPrinter
#Region " CONSTANTS "
Private Const SW_SHOWNORMAL As Integer = 2
#End Region
#Region " API "
<DllImport("shell32")> _
Public Shared Function ShellExecute(ByVal hWnd As IntPtr, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Integer) As IntPtr
End Function
#End Region
#Region " PUBLIC MEMBERS "
Public Function PrintPDF(ByVal FilePath As String) As Boolean
If IO.File.Exists(FilePath) Then
If ShellExecute(1, "Print", FilePath, "", _
Directory.GetDirectoryRoot(FilePath), SW_SHOWNORMAL).ToInt32 <= 32 Then
Return False
Else
Return True
End If
Else
Return False
End If
End Function
#End Region
End Class
-
Jan 7th, 2009, 04:50 PM
#2
Hyperactive Member
Re: Silently Print PDF with ShellExecute
I assume this uses default printer? Is there a way to designate the printer.
I guess i can always change the default printer via code and then set it back.
-
Apr 3rd, 2010, 10:19 PM
#3
New Member
Re: Silently Print PDF with ShellExecute
I'm impressed with the code and basically understand it. However, I'm new to vb and was wondering what the code behind and html would look like in a practical use. Do you have a sample page available to look at?
-
May 4th, 2010, 08:47 AM
#4
Re: Silently Print PDF with ShellExecute
This is not going to work on a website. This will print using Acrobat on the system where the code is executed. It is designed for a Windows application not a website.
-
May 4th, 2010, 12:22 PM
#5
Frenzied Member
Re: Silently Print PDF with ShellExecute
 Originally Posted by bmahler
I spent hours searching for a way to print pdf's without opening Acrobat and finally got it working using ShellExecute.
Why don't you just use the AcroReader command line parameters to print a PDF? It's much simpler than what you posted in #1. Or am I not understanding what you're trying to do here?
-
May 7th, 2010, 08:40 AM
#6
Re: Silently Print PDF with ShellExecute
So this is not specifically for printing a PDF is it, its just for printing any specified file to the default printer right?
-
Oct 26th, 2014, 04:45 AM
#7
New Member
Re: Silently Print PDF with ShellExecute
 Originally Posted by bmahler
I spent hours searching for a way to print pdf's without opening Acrobat and finally got it working using ShellExecute. I figured I would share
vb.net Code:
Imports System.Runtime.InteropServices
Imports System.IO
Public Class PDFPrinter
#Region " CONSTANTS "
Private Const SW_SHOWNORMAL As Integer = 2
#End Region
#Region " API "
<DllImport("shell32")> _
Public Shared Function ShellExecute(ByVal hWnd As IntPtr, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Integer) As IntPtr
End Function
#End Region
#Region " PUBLIC MEMBERS "
Public Function PrintPDF(ByVal FilePath As String) As Boolean
If IO.File.Exists(FilePath) Then
If ShellExecute(1, "Print", FilePath, "", _
Directory.GetDirectoryRoot(FilePath), SW_SHOWNORMAL).ToInt32 <= 32 Then
Return False
Else
Return True
End If
Else
Return False
End If
End Function
#End Region
End Class
I have added this code and tested it with a pdf file, and when I ran it it still opened Adobe before it printed out!
Do i have to make any other settings to stop this happening, as I just want to print pdf files without Adobe opening and closing for each print?
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|