# VBForums CodeBank > CodeBank - Visual Basic .NET >  Silently Print PDF with ShellExecute

## 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

----------


## melvin74

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.

----------


## DennisA

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?

 :Thumb:

----------


## bmahler

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.

----------


## nbrege

> 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?

----------


## chris128

So this is not specifically for printing a PDF is it, its just for printing any specified file to the default printer right?

----------


## timberlad

> 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
> ...



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

----------

