Results 1 to 18 of 18

Thread: (RESOLVED)Print External PDF without opening it, via string entered in a textbox.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2013
    Posts
    29

    Post (RESOLVED)Print External PDF without opening it, via string entered in a textbox.

    Hello,

    I just want to thank you for reading this post first of all

    I want to print a pdf file located in a external (P:\) drive, without opening up the file.

    I have a userform consisting of 2 buttons and a textbox for user input, a PRINT button and a Load button. lets work with PRINT button for now, aka button1

    The user will enter a tex in the textbox field. and click on button1, the code will store that text entered as a string and add the extension .PDF then find that file within the P:\ and send it to the default printer.

    Here is what I have so far...



    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim psi As New ProcessStartInfo
    
            psi.UseShellExecute = True
    
            psi.Verb = "print"
    
            psi.WindowStyle = ProcessWindowStyle.Hidden
    
            psi.Arguments = PrintDialog1.PrinterSettings.PrinterName.ToString()
    
            psi.FileName = "P:\pdffilename.pdf"  ' Need to replace this with a string based on the textbox1 input and add .pdf to the string and the location of it for printing.
    
            Process.Start(psi)
    
        End Sub
    Not sure where this part of code will go, but I'm assuming it will save the text enter in textbox1 as string.

    Code:
     Dim pdffilename as String
    pdffilename = Textbox1.Text
    The pdffilename is the text the user will enter and with the click of button1 it will store that as string and should add P:\ AND .pdf extension at the end of it and should replace that part of psi.filename = "P:\pdffilename.pdf"


    Sorry if i seem too repetitive but I really want a way to print pdf files, (hundreds of them) via entering the name of the file in a textbox and click on a print button (button1). rather than searching for the file, opening the file, and clicking print, and clicking "ok" to finally print.

    Thanks,

    Steve.
    Last edited by kingpain; Dec 10th, 2013 at 07:19 PM.

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Dec 2013
    Posts
    29

    Re: Print External PDF without opening it, via string entered in a textbox.

    This post has been edited and delete most junk.

    Steve
    Last edited by kingpain; Dec 8th, 2013 at 11:34 AM.

  3. #3
    gibra
    Guest

    Re: Print External PDF without opening it, via string entered in a textbox.

    You can use:

    Code:
    Dim pdffilename as String
    pdffilename = Textbox1.Text
    pdffilename = pdffilename + ".pdf"
    Of course, you must add a check if file exists.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2013
    Posts
    29

    Re: Print External PDF without opening it, via string entered in a textbox.

    Thank you for responding Gibra, can you please elaborate some more?

    How can i implement that piece of code to work with the print code?

    I need the print code to get the pdfilename and add the location (P:\) AND .pdf to be like psi.FileName = "P:\pdffilename.pdf"

    Could I replace the
    psi.FileName = "P:\pdffilename.pdf"
    with
    psi.FileName = "pdffilename"
    and it will add the P:\ AND .pdf to the code for automated printing?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Print External PDF without opening it, via string entered in a textbox.

    Quote Originally Posted by kingpain View Post
    The pdffilename is the text the user will enter and with the click of button1 it will store that as string and should add P:\ AND .pdf extension at the end of it and should replace that part of psi.filename = "P:\pdffilename.pdf"
    So, basically, you're asking how to concatenate strings, correct? That is one of the most elementary things you can do and would be covered by any beginner tutorial.
    Code:
    Dim filePath = "P:\" & myTextBox.Text & ".pdf"

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Dec 2013
    Posts
    29

    Re: Print External PDF without opening it, via string entered in a textbox.

    This post has been edited and delete most junk.

    Steve
    Last edited by kingpain; Dec 8th, 2013 at 11:34 AM.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2013
    Posts
    29

    Post Re: Print External PDF without opening it, via string entered in a textbox.

    Code:
    Imports System.IO
    Imports System.Drawing
    
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim pdffilename As String
            pdffilename = TextBox1.Text
            Dim filepath = "C:\temp\" & TextBox1.Text & ".pdf"
            Dim psi As New ProcessStartInfo
            psi.UseShellExecute = True
    
            psi.Verb = "print"
    
            psi.WindowStyle = ProcessWindowStyle.Hidden
    
            'psi.Arguments = PrintDialog1.PrinterSettings.PrinterName.ToString()
    
            psi.FileName = ("filepath") ' This works if i give the full location of my pdf file instead of the string "filepath"
            ' the messagebox will be removed this is just for testing.
            MessageBox.Show(filepath) ' to comfirm that I'm indeed finding the file at C:\temp\test01.pdf 
    
            Process.Start(psi) ' This is where the code fails. 
    
    
        End Sub
    
      
    
        Private Sub TextBox1_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    
        End Sub
    
    
    End Class
    What am i doing wrong?

    I've imported the printdocument1 and everything else.

    the messagebox test works fine, it shows me the file location with the named enter in the textbox.

    but the error happens on
    Code:
    Process.Start(psi)
    Any ideas?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Print External PDF without opening it, via string entered in a textbox.

    Firstly, a PrintDocument is of no use to you because that's for when you do your own printing but in this case you're relying on an external application to do the printing for you.

    As for the issue, you tell us that there's an error and you tell us where but you don't tell us what the error message is. Maybe we can guess what it is or maybe we can work out what it is from the code but should we have to do either? You have the error message, which the IDE gave to you as a diagnostic tool, so why not give it to us? We then know what we're looking for in the code.

    In this case though, a quick look at the code reveals the issue:
    Code:
    psi.FileName = ("filepath") ' This works if i give the full location of my pdf file instead of the string "filepath"
    Why would you use the String "filepath" in the first place? You need to assign a String that contains the file path, not a String that contains "filepath".

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2013
    Posts
    29

    Re: Print External PDF without opening it, via string entered in a textbox.

    The error I get is Win32Exception was unhandled "The system cannot find the file specified"

    And I thought i did assign the file path as a string.
    Dim filepath = "C:\temp\" & TextBox1.Text & ".pdf"

    it works with the message box, it shows me that it has the file path as the correct location C:\temp\test6.pdf

    Or maybe I'm not quite following you...

    Thanks for your reply once again, it means a lot to me.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Dec 2013
    Posts
    29

    Re: Print External PDF without opening it, via string entered in a textbox.

    This post has been edited and delete most junk.

    Steve
    Last edited by kingpain; Dec 8th, 2013 at 11:33 AM.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Print External PDF without opening it, via string entered in a textbox.

    Quote Originally Posted by kingpain View Post
    The error I get is Win32Exception was unhandled "The system cannot find the file specified"

    And I thought i did assign the file path as a string.


    it works with the message box, it shows me that it has the file path as the correct location C:\temp\test6.pdf

    Or maybe I'm not quite following you...

    Thanks for your reply once again, it means a lot to me.
    Well you didn't. Look at your code:
    Code:
    Imports System.IO
    Imports System.Drawing
    
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim pdffilename As String
            pdffilename = TextBox1.Text
            Dim filepath = "C:\temp\" & TextBox1.Text & ".pdf"
            Dim psi As New ProcessStartInfo
            psi.UseShellExecute = True
    
            psi.Verb = "print"
    
            psi.WindowStyle = ProcessWindowStyle.Hidden
    
            'psi.Arguments = PrintDialog1.PrinterSettings.PrinterName.ToString()
    
            psi.FileName = ("filepath") ' This works if i give the full location of my pdf file instead of the string "filepath"
            ' the messagebox will be removed this is just for testing.
            MessageBox.Show(filepath) ' to comfirm that I'm indeed finding the file at C:\temp\test01.pdf 
    
            Process.Start(psi) ' This is where the code fails. 
    
    
        End Sub
    
      
    
        Private Sub TextBox1_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    
        End Sub
    
    
    End Class
    You declare a 'psi' variable and you assign a ProcessStartInfo object to it and you have then used that variable thereafter. Now look at how you're trying to use the file path. You have declared a variable and assigned a value to it, i.e. a String that contains the file path. Have you then used that variable thereafter?

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Dec 2013
    Posts
    29

    Re: Print External PDF without opening it, via string entered in a textbox.

    For the likes of me I cannot understand what I'm doing wrong. this code works if i replace filepath with the actual file path. it will start the print process.

    I still don't understand why it fails to at
    Process.Start(psi)
    Please help me out. I have very limited knowledge of VB so far, i cannot fix this code by myself even if you give instructions, I can study and fix it if you give me the right suggestions or code fix.

    Thanks for responding.

    Steve

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Dec 2013
    Posts
    29

    Re: Print External PDF without opening it, via string entered in a textbox.

    Code:
    Imports System.IO
    Imports System.Drawing
    Imports System.Diagnostics
    
    
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim pdffilename As String
            pdffilename = TextBox1.Text
            Dim filepath = "C:\temp\" & TextBox1.Text & ".pdf"
            Dim psi As New System.Diagnostics.ProcessStartInfo()
            psi.UseShellExecute = True
    
            psi.Verb = "print"
    
            psi.WindowStyle = ProcessWindowStyle.Hidden
    
            'psi.Arguments = PrintDialog1.PrinterSettings.PrinterName.ToString()
    
            psi.FileName = ("filepath")
            Process.Start(psi) ' This is where the code fails. 
    
    
        End Sub
    
    
    
        Private Sub TextBox1_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    
        End Sub
    
    
    End Class
    Not sure what I'm doing wrong

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Print External PDF without opening it, via string entered in a textbox.

    Look at where you're using the psi variable. Do you have double quotes around it? Of course not, so why do you think it's good idea to put double quotes around filepath? You're not using the filepath variable; you're using a String literal contain the actual text "filepath".

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Dec 2013
    Posts
    29

    Re: Print External PDF without opening it, via string entered in a textbox.

    Sweet! I took out the double quotes and it worked, only problem is, it leaves a blank adobe reader app open, I suppose I can write a piece of coding to kill any open adobe reader window.

    Or is there a prefer method for closing the adobe window?

    Thank you very very much mate. you are the man .

    my I'm very intrigued, I will continue to learn VB to my full extent. so future projects wouldn't be a pain in the.....

  16. #16
    gibra
    Guest

    Re: Print External PDF without opening it, via string entered in a textbox.

    Sorry but I don't understand...

    Until now you say that starting path is always "P:\" :
    Quote Originally Posted by kingpain View Post
    I need the print code to get the pdfilename and add the location (P:\) AND .pdf to be like psi.FileName = "P:\pdffilename.pdf"

    Now you starting path is changed:
    Quote Originally Posted by kingpain View Post
    Code:
            Dim filepath = "C:\temp\" & TextBox1.Text & ".pdf"
    Why ???
    Last edited by gibra; Dec 8th, 2013 at 03:43 PM.

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Dec 2013
    Posts
    29

    Re: Print External PDF without opening it, via string entered in a textbox.

    Because P: is the drive path for my work. C:\temp is where I have my test pdf files for testing.

    Sorry for the confusion, I got the program to work like it should. only have to figure out how to close the Acrord32.exe after printing.

    I have more to code for this program. but I don't think i will be bothering anyone on the forums.

    my own lack of knowledge made me ask for help, and I know how people are these days when it comes to helping a random person.

    but so far you guys have been great. I've learn a lot.
    I will continue to learn more and educate myself. (I didn't have the privilege of going to a college to further my education).

  18. #18
    gibra
    Guest

    Re: Print External PDF without opening it, via string entered in a textbox.

    This code work for me:

    Code:
        Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
            Try
                Dim psi As New System.Diagnostics.ProcessStartInfo()
                Dim filepath = "D:\" & TextBox1.Text & ".pdf"
                psi.UseShellExecute = True
                psi.Verb = "print"
                psi.WindowStyle = ProcessWindowStyle.Hidden
                psi.FileName = filepath
    
                Dim myProcess As System.Diagnostics.Process
                myProcess = System.Diagnostics.Process.Start(psi)  
                myProcess.WaitForInputIdle()
    
                Dim localProces As Diagnostics.Process = Diagnostics.Process.GetProcessById(myProcess.Id)
                localProces.CloseMainWindow()
                localProces.Close()
    
            Catch ex As Exception
                MessageBox.Show(ex.Message())
            End Try
    
    
        End Sub

Tags for this Thread

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