0

I'm programming for a friend to help them print all files in a folder (after filtering them). I've gotten to the point where I am able to select a folder, filter the files according to extension, and list them in a ListBox. I'm thinking that I'm going to have to Loop 1) Get "File1 Name", 2) Print File1, and 3) Go to the next item, Until there are no more files. So I'm testing it out on just a regular textbox where I input the location of file "C:\Users\akapp\OneDrive\Documents\AAppleton Midterm.pdf." Is there a way to do GetFiles and Print after using a button?

This is the Print Dialogue I'm using:

    Private Sub BtnPrint_Click(sender As Object, e As RoutedEventArgs) Handles btnPrint.Click
        Dim printDoc As New PrintDocument()
        Dim printDlg As New PrintDialog With {
            .Document = printDoc,
            .PrinterSettings = printDoc.PrinterSettings,
            .AllowSomePages = True
        }

        If printDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            printDoc.PrinterSettings = printDlg.PrinterSettings
            printDoc.Print()
        End If
    End Sub

It works fine, but just prints a blank page.

I've tried printDoc.DocumentName = txtTest.Text but that does not work... ;-;

Any help is very appreciated. ^u^

Also I am a very new programmer. Like, started this past week with this project.

3
  • Is it specifically PDF files that you're printing, or are you trying to print any file at all? Commented Dec 31, 2022 at 3:05
  • If you use a PrintDocument then it's up to you to specify exactly what to print, which you're not doing. You have to handle the PrintPage event and use GDI+ to draw what you want printed. That's obviously not practical for many file formats, including PDF. For such files, you would generally execute them using Process.Start and specify Print as the verb, which will make their default application do the printing. You should research that specifically. Commented Dec 31, 2022 at 4:35
  • Thank you! Process.Start with .verb = "printto" works! Commented Jan 3, 2023 at 15:40

1 Answer 1

0

Thank you to @jmcilhinney to start me in the right direction of Process.Start!

My final code (for printing files from their names in my listbox) is this!

    Dim psI As Process Start Info
    Try
        Dim printDlg As PrintDialog
        printDlg = New PrintDialog()

        If printDlg.ShowDialog = True Then
            For i = 0 To lstbxFiles.Items.Count - 1
            'psI.Arguments = """" & cmbxPrinters.Text & """"
            psI = New ProcessStartInfo(lstbxFiles.Items(i).ToString()) With 
              {
              .Arguments = """" & printDlg.PrintQueue.FullName & """",
              .UseShellExecute = True,
              .Verb = "PrintTo",
              .CreateNoWindow = True,
              .WindowStyle = ProcessWindowStyle.Hidden
              }
            System.Threading.Thread.Sleep(3000)
            Process.Start(psI)
            Next
        End If

    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString())
    End Try

Hopefully this may help other people!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.