20 Replies Latest reply: Jul 19, 2012 7:04 AM by Onyrix RSS

    VB.Net Printing of PDFs

    Community Member
      I am trying to print a pdf document to a specific printer from VB.Net app. I would like this to be a silent process i.e. no print dialog and no Acrobat Reader popping up.

      This solution must only use reader as it needs to work without licensing issues.

      Anyone got any ideas?

      I have managed to do it using the command line options but a reader window pops up and wont go away.
        • 1. Re: VB.Net Printing of PDFs
          Community Member
          Reader is designed to "pop up" to ensure that no one uses Acrobat/Reader as "malware" to start doing unrequested silent printing on the user's computer.

          You can certainly use Windows APIs to "hide" it after the fact, but it will first pop up.
          • 2. Re: VB.Net Printing of PDFs
            Community Member
            OK nevermind.

            Anyone know a non adobe component that is either free or cheap that could do this?
            • 3. Re: VB.Net Printing of PDFs
              Community Member
              I won't comment about why Adobe does this, but you might be able to work around the issue using the techniques I employed in pdfp / pdfp8 here:

              http://www.esnips.com/web/PDFTools
              • 4. Re: VB.Net Printing of PDFs
                Community Member
                " Reader is designed to "pop up" to ensure that no one uses Acrobat/Reader as "malware" to start doing unrequested silent printing on the user's computer. "

                So, then how can one do
                requested
                silent printing???
                • 5. Re: VB.Net Printing of PDFs
                  Community Member
                  What is the workflow that you envision where you need to "silent print"?

                  Leonard
                  • 6. Re: VB.Net Printing of PDFs
                    Community Member
                    Try the following code. It is working for us..
                    Const SEE_MASK_NOCLOSEPROCESS = &H40
                    Const SEE_MASK_FLAG_NO_UI = &H400
                    Private Type SHELLEXECUTEINFO
                    cbSize As Long
                    fMask As Long
                    hwnd As Long
                    lpVerb As String
                    lpFile As String
                    lpParameters As String
                    lpDirectory As String
                    nShow As Long
                    hInstApp As Long
                    lpIDList As Long
                    lpClass As String
                    hkeyClass As Long
                    dwHotKey As Long
                    hIcon As Long
                    hProcess As Long
                    End Type

                    Declare Function ShellExecuteEx Lib "shell32.dll"Alias "ShellExecuteEx" (SEI As SHELLEXECUTEINFO) As Long
                    Declare Function TerminateProcess Lib "kernel32"Alias "TerminateProcess" (Byval hProcess As Long, Byval uExitCode As Long) As Long

                    Function PrintPDF() as Boolean
                    On Error Goto PrintDocErrorHandle

                    PrintDoc = False

                    Dim SEI As SHELLEXECUTEINFO
                    SEI.cbSize = Len(SEI)
                    SEI.fMask = SEE_MASK_NOCLOSEPROCESS Or SEE_MASK_FLAG_NO_UI
                    SEI.lpVerb = "Print"
                    ' SEI.lpVerb = "Open"
                    SEI.lpFile = strFname
                    SEI.lpDirectory = s.GetEnvironmentString( "Directory" , True ) & strExtractPath
                    SEI.nShow = 1
                    SEI.hInstApp = 0
                    SEI.lpIDList = 0
                    Call ShellExecuteEx(SEI)
                    Sleep(3) ' file was being deleted before it could be printed.
                    Call TerminateProcess(SEI.hProcess, 0)
                    'Print "Return Code = " & Cstr( SEI.hInstApp )
                    If SEI.hInstApp < 32 Then
                    errMsg = errMsg & Chr(13) & "Error printing document: " & strFname & ". Document Subject: " & doc.subject(0) & ". Document: " & doc.UniversalID & ". Print return code: " & Cstr( SEI.hInstApp)
                    Else
                    successMsg = successMsg & Chr(13) & "Successfully printed document: " & strFname & " from " & doc.subject(0)
                    End If

                    PrintDoc = True
                    exitFunctionCode:
                    Exit Function

                    PrintDocErrorHandle:
                    flag = 1300
                    errMsg = errMsg & Chr(13) & "Error printing document: " & strFname & ". " & Error() & ". Document subject: " & doc.subject(0) & ". Document: " & doc.UniversalID
                    Resume exitFunctionCode
                    • 7. Re: VB.Net Printing of PDFs
                      Community Member
                      I work with production printing customers and some would like to batch print PDF files; and they would like to do that in the background on their PC. "Silent" printing would be nice because they don't want a print dialog for each and every PDF being printed.
                      • 8. Re: VB.Net Printing of PDFs
                        Community Member
                        I have got arround the problem by writing some code to change the default printer to the required printer and source tray etc then print using /t on the command line - see below. I hope this helps anyone trying to do this.

                        Dim objStartInfo As New ProcessStartInfo
                        objProcess = New System.Diagnostics.Process

                        ' set start info properties
                        With objStartInfo
                        '.CreateNoWindow = True
                        .FileName = AcrobateViewerPath
                        .Arguments = "/t """ & Filename & """"
                        .UseShellExecute = False
                        '.WindowStyle = ProcessWindowStyle.Hidden
                        .ErrorDialog = True
                        .RedirectStandardError = True
                        End With

                        ' start process
                        Try
                        objProcess = Process.Start(objStartInfo)
                        Catch ex As Exception
                        MsgBox("Error printing")
                        End Try

                        'wait for process to print
                        System.Threading.Thread.Sleep(10000)

                        'kill processes called AcroRd32
                        Dim myprocesses As Process() = System.Diagnostics.Process.GetProcessesByName("AcroRd32")
                        For Each myproces As Process In myprocesses
                        myproces.Kill()
                        Next
                        • 9. Re: VB.Net Printing of PDFs
                          Community Member
                          No method that waits for a defined period before killing the Reader process will work correctly for all PDFs and printers.

                          It's much better to know for sure that print spooling is done before closing Reader.

                          See pdfp8 here for a utility (with C source) that does this:

                          http://www.esnips.com/web/PDFTools
                          • 10. Re: VB.Net Printing of PDFs
                            Community Member
                            I have to say this is pretty bull that there is no way to stop adobe from popping up into the focus.

                            I run a server program for our business which does batch printing via ole automation in vb6(among other things) and the server can print up to 1000 pdfs a day.

                            Now before our company reviewed and approved adobe 8 for use I was stuck using version 6 and had to deal with the msgbox "this is a newer version of adobe, click ok to continue" for every...single...document...because Adobe does not save your settings outside of your current session with adobe(ie checking the box in the pop up that says "check here to never display this again" does nothing).

                            Now I have to deal with Adobe popping up every...single...pdf... I could live with that if it did not always force itself to be the focus and interrupt what else I may be working on. I am one of the most patient people but this has me in a near rage.
                            • 11. Re: VB.Net Printing of PDFs
                              Patrick Leckey Community Member
                              > I run a server program for our business which does batch printing via ole automation

                              Well, there's your problem. Acrobat is not designed for server use, and it's actually expressly forbid in the EULA (Section 2.3, Server Use). Using it in a server-based automation fashion is a violation of the EULA, and since you've just publicly posted on Adobe forums that you're using it in that way it would probably be a good idea to remove it from your server before you get a call from Adobe Legal (believe me, they do follow-up on license violations).

                              The OLE automation is designed to automate desktop installs of Acrobat. It is, per the EULA and documentation, required that each end-user have a licensed copy of Acrobat on their machine and that the automation software be installed locally on their machine as well. There can be no situation where multiple users are submitting requests to a single copy of Acrobat in an automated fashion.

                              The reason its not working the way you expect it to is because it was expressly designed NOT to work in a server environment. That's what the LiveCycle products are for. Using Acrobat as part of any server-based workflow is illegal, so Adobe certainly don't develop it to be server-friendly.
                              • 12. Re: VB.Net Printing of PDFs
                                Community Member
                                Sorry it is not run on a server. It is a stand-alone application that we call a server because it handles document management requests that are outside of adobe(ie our file structure) as well as has a lot of utilities for managing our database and files. I create the batch print jobs and manually submit them. These are by request though if that matters and if so since everyone has Adobe installed already would it be fine if we just had them run a striped down version to print? This still brings it back to the issue that adobe pops up and interrupts your workflow for anyone that prints. This is completely unacceptable for programming adobe into an end user application.

                                Either way if we misread what means to be a stand alone application and not I apologize. We will gladly switch to an alternative since this is small piece of it.
                                • 13. Re: VB.Net Printing of PDFs
                                  Community Member
                                  And by request I mean someone physically tells me they need some hardcopies sent somewhere and I select the files to print and push a button to batch print them.

                                  I apologize if the language I used out of habit is confusing.
                                  • 14. Re: VB.Net Printing of PDFs
                                    Patrick Leckey Community Member
                                    No, based on your more complete description of your workflow you should be within the bounds of the EULA so no problem there.

                                    Your best bet, since you have a full version of Acrobat, would be to create a silent print plug-in in C/C++ and then call that plug-in from your VB application. That way you can assure it is a silent, dialog-less print (and quite frankly, a simple silent print plug-in that doesn't require any advanced configuration options will not take long to build at all for an experienced developer).
                                    • 15. Re: VB.Net Printing of PDFs
                                      Community Member
                                      Thank you very much Saradhi Cheruvu... The code worked for me too by increasing the sleep time to 30 seconds...
                                      • 16. Re: VB.Net Printing of PDFs
                                        Community Member
                                        download the PDFCreator from Sourceforge - http://sourceforge.net/projects/pdfcreator/

                                        Set it as the default printer (can do this in the code if you want)
                                        Set the settings in the PDFCreator Printer options in the Auto-save section to not have pop-ups and where to save the document.

                                        If you want to get fancy, you can call the word, excel, or whatever with a minimized window to get NO windows popping up.

                                        Let it fly:

                                        Imports System
                                        Imports System.Diagnostics
                                        Imports System.IO

                                        Module Module1
                                        Const pdfTempFolder As String = "c:\tempPDF"
                                        Const pdfResultsFolder As String = "c:\tempPDF\"
                                        Dim DocumentName As String
                                        ' ------------------------------------------------------------
                                        Dim di As New IO.DirectoryInfo("c:\tempPDF")
                                        Dim aryFi As IO.FileInfo() = di.GetFiles("*.doc")
                                        Dim fi As IO.FileInfo

                                        Sub Main()
                                        For Each fi In aryFi
                                        Console.WriteLine("File Full Name: {0}", fi.FullName)
                                        DocumentName = fi.FullName
                                        Print(DocumentName)
                                        Next
                                        End Sub

                                        Sub Print(ByVal DocumentName)
                                        Dim proc As New Process
                                        Console.WriteLine("DocumentName Sent to Process Start is: " + DocumentName)
                                        proc.StartInfo.FileName = DocumentName
                                        proc.StartInfo.Verb = "Print"
                                        proc.StartInfo.CreateNoWindow = True
                                        proc.Start()
                                        End Sub
                                        End Module
                                        • 17. Re: VB.Net Printing of PDFs
                                          Community Member
                                          You can change this to C code or what ever your preference is. You will need to have the Ghostscript loaded with the PDFCreator program.
                                          • 18. Re: VB.Net Printing of PDFs
                                            Community Member
                                            test
                                            • 19. Re: VB.Net Printing of PDFs
                                              Community Member
                                              Change the sentences
                                              VB.NET
                                              Arguments = "/t """ & Filename & """"
                                              by
                                              C#
                                              Arguments = "/t " + path_file + " " + path_printer + ""
                                              • 20. Re: VB.Net Printing of PDFs
                                                Onyrix Community Member

                                                i wrote an article how to open and show and silent print pdfs in Windows and Mac OSX with examples (using native commands):
                                                http://www.onyrix.com/2012/04/adobe-air-pdf-silent-print-with-acrobat-reader/

                                                bye
                                                d

                                                even if is for Adobe Air you can you native commands examples for any other programming language