Results 1 to 12 of 12

Thread: [2008] I need to print a PDF via code

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    [2008] I need to print a PDF via code

    I have to print out a PDF via code, and the code will assume adobe reader (or acrobat) is installed and configured properly to open PDF files.

    I have it working, with the exception that it prints to the default printer always. I am looking for a way to do this, but have it prompt with a printer dialog.

    I am pretty sure this can be accomplished by shelling the reader exe with some command line params, but I don't want to do that, because the users will have either reader OR the full acrobat installed, so I can't assume they will have reader, only that they will have one or the other.

    If there is no known way to do this, I will try my second method, which is to get the exe associated with PDF documents, and launch that, passing the doc and the printer as params, and test that.

    Any ideas?


    This is the code that works, but prints to the default printer.
    Code:
    Dim PSI As New ProcessStartInfo("c:\document.pdf")
    PSI.Verb = "print"
    Process.Start(PSI)

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] I need to print a PDF via code

    What I've done in the past is to display a PrintDialog to the users and let them pick the printer they like (also change any printer settings as needed). I then read the selected printer back from the dialog, and temporarily set it as the default printer (save the name of the current default printer to a variable first), do the printing then reset the default back to the original printer.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2008] I need to print a PDF via code

    I had considered that, but the people who have the "final say" said no to that idea.

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2008] I need to print a PDF via code

    You might be able to do what Stanav suggested with the printerdialog and then use the PrintTo verb instead, so you don't have to change the default printer.

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2008] I need to print a PDF via code

    I looked at PrintTo, but I could not get it to perform any differently than Print

    When I tried setting the verb to PrintTo, I then set the argument property of the ProcessStartInfo to the printer I want to print to, but it has no effect. Perhaps I am missing some syntax somewhere, but I couldn't really find much info on it.

  6. #6
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2008] I need to print a PDF via code

    This worked for me:
    Code:
            Dim pd As New PrintDialog
            If pd.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim p As New ProcessStartInfo
                p.FileName = "C:\Docs\Test Documents\Small PDF.pdf"
                p.Verb = "printto"
                p.Arguments = pd.PrinterSettings.PrinterName
                Process.Start(p)
            End If

  7. #7
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2008] I need to print a PDF via code

    Ok, I found a little quirk with the code. Your arguments need to be surrounded by double quotes (it will not work if you don't have the quotes and your printer has a space in the name):
    Code:
            Dim pd As New PrintDialog
            If pd.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim p As New ProcessStartInfo
                p.FileName = "C:\Docs\Test Documents\Small PDF.pdf"
                p.Verb = "printto"
                p.CreateNoWindow = False
                p.Arguments = Chr(34) & pd.PrinterSettings.PrinterName.ToString() & Chr(34)
                Process.Start(p)
            End If

  8. #8
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [2008] I need to print a PDF via code

    another way could be

    Code:
    If PrintDialog1.ShowDialog = DialogResult.OK Then
                Dim application As String = "AcroRd32.exe"
    
                Dim strPath = "C\Test.PDF" 'Complete name/path of PDF file
    
                Dim strPrinter = PrintDialog1.PrinterSettings.PrinterName 'Name Of printer
    
                Dim prcInfo As New ProcessStartInfo(application, "/t " + strPath + " " + strPrinter + "")
    
                Process.Start(prcInfo)
            End If
    __________________
    Rate the posts that helped you

  9. #9

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2008] I need to print a PDF via code

    Quote Originally Posted by riteshjain1982
    another way could be

    Code:
    If PrintDialog1.ShowDialog = DialogResult.OK Then
                Dim application As String = "AcroRd32.exe"
    
                Dim strPath = "C\Test.PDF" 'Complete name/path of PDF file
    
                Dim strPrinter = PrintDialog1.PrinterSettings.PrinterName 'Name Of printer
    
                Dim prcInfo As New ProcessStartInfo(application, "/t " + strPath + " " + strPrinter + "")
    
                Process.Start(prcInfo)
            End If
    This won't work because this assumes acrord32.exe is on the system.

    Negative0, I will test that and see if it works for me.

  10. #10

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2008] I need to print a PDF via code

    ok, I got it to work using either your code Negative0, or similar code that launches the given exe file for adobe instead. I grab it via FindExecutable Win32 API so I don't have to hard code a specific exe name they might be using.

    The problem is with either approach, that adobe is shown no matter what, even when you specify CreateNoWindow or WindowStyle of hidden.

    So the only thing I am thinking at this point, is some ugly hack like calling WaitForExit() on the process with a timeout, and killing the process after the timeout, but that doesn't seem reliable. I don't want to leave adobe open after printing the doc. It is just a PDF they are supposed to be able to print out from a menu in the software. It seems as though adobe made it rather difficult to just send a PDF doc to the printer when you don't want the default printer.

  11. #11
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2008] I need to print a PDF via code

    I saw the same thing in my testing. You could go with a third party toolkit to do the PDF printing, totally taking Adobe (or whatever the user has installed) off the table.

  12. #12

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2008] I need to print a PDF via code

    I don't really want to couple any further dependacy into the app.

    To be honest, if it comes to it, I will convert the PDF to a series of PNG files, and print them via a PrintDocument object. I just don't want to have to do that if its not needed. I figured sending a PDF to a specific printer would not be a major task, but for whatever reason, that appears to be the case. Even if the issue looks to be a bug with adobe.

    I will mess around a bit more and see if I can't get something working. Another scenario to worry about is when the user already has reader open for some other reason. I don't want to go killing instances on a user machine because I dont know what those instances might be.

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