Results 1 to 7 of 7

Thread: Silently Print PDF with ShellExecute

  1. #1

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    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:
    1. Imports System.Runtime.InteropServices
    2. Imports System.IO
    3.  
    4. Public Class PDFPrinter
    5.  
    6. #Region " CONSTANTS "
    7.     Private Const SW_SHOWNORMAL As Integer = 2
    8. #End Region
    9.  
    10. #Region " API "
    11.     <DllImport("shell32")> _
    12.     Public Shared Function ShellExecute(ByVal hWnd As IntPtr, _
    13.                                         ByVal lpOperation As String, _
    14.                                         ByVal lpFile As String, _
    15.                                         ByVal lpParameters As String, _
    16.                                         ByVal lpDirectory As String, _
    17.                                         ByVal nShowCmd As Integer) As IntPtr
    18.     End Function
    19. #End Region
    20.  
    21. #Region " PUBLIC MEMBERS "
    22.     Public Function PrintPDF(ByVal FilePath As String) As Boolean
    23.         If IO.File.Exists(FilePath) Then
    24.             If ShellExecute(1, "Print", FilePath, "", _
    25.             Directory.GetDirectoryRoot(FilePath), SW_SHOWNORMAL).ToInt32 <= 32 Then
    26.                 Return False
    27.             Else
    28.                 Return True
    29.             End If
    30.         Else
    31.             Return False
    32.         End If
    33.     End Function
    34. #End Region
    35. End Class
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  2. #2
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    307

    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.

  3. #3
    New Member
    Join Date
    Apr 2010
    Posts
    1

    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?


  4. #4

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    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.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  5. #5
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Silently Print PDF with ShellExecute

    Quote Originally Posted by bmahler View Post
    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?

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7
    New Member
    Join Date
    Oct 2014
    Posts
    1

    Re: Silently Print PDF with ShellExecute

    Quote Originally Posted by bmahler View Post
    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:
    1. Imports System.Runtime.InteropServices
    2. Imports System.IO
    3.  
    4. Public Class PDFPrinter
    5.  
    6. #Region " CONSTANTS "
    7.     Private Const SW_SHOWNORMAL As Integer = 2
    8. #End Region
    9.  
    10. #Region " API "
    11.     <DllImport("shell32")> _
    12.     Public Shared Function ShellExecute(ByVal hWnd As IntPtr, _
    13.                                         ByVal lpOperation As String, _
    14.                                         ByVal lpFile As String, _
    15.                                         ByVal lpParameters As String, _
    16.                                         ByVal lpDirectory As String, _
    17.                                         ByVal nShowCmd As Integer) As IntPtr
    18.     End Function
    19. #End Region
    20.  
    21. #Region " PUBLIC MEMBERS "
    22.     Public Function PrintPDF(ByVal FilePath As String) As Boolean
    23.         If IO.File.Exists(FilePath) Then
    24.             If ShellExecute(1, "Print", FilePath, "", _
    25.             Directory.GetDirectoryRoot(FilePath), SW_SHOWNORMAL).ToInt32 <= 32 Then
    26.                 Return False
    27.             Else
    28.                 Return True
    29.             End If
    30.         Else
    31.             Return False
    32.         End If
    33.     End Function
    34. #End Region
    35. 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
  •  



Click Here to Expand Forum to Full Width