0

So I am working on a form which need to be printed. I want to end up with a PDF file using Foxit PDF printer. The problem is that I cant figure out how to get the selected path as file location so I keep getting an the Path cannot be null. error. Where in the code should I put my filelocation when using the Printform? In this code the foldername is the location where I want to print.

Private Sub BtnPrint_Click(sender As Object, e As EventArgs) Handles BtnPrint.Click

    Dim folderDlg As New FolderBrowserDialog
    Dim foldername As String
    folderDlg.ShowNewFolderButton = True
    If (folderDlg.ShowDialog() = DialogResult.OK) Then
        foldername = folderDlg.SelectedPath
        Dim root As Environment.SpecialFolder = folderDlg.RootFolder

    End If

    PrintForm1.Print()

End Sub

Edit:

Actually deleted part of the code and still getting the same error (first part wasnt doing anything to start with I know that). All I am using now is:

Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles BtnPrint.Click

    PrintForm1.Print()

End Sub

Also microsoft help database about Printform isnt helping since I have done exactly what it says and still getting the Path is Null error

Edit 2: So I am using this code now and it is working.

 Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles BtnPrint.Click

    PrintDialog1.PrinterSettings = PrintForm1.PrinterSettings
    PrintDialog1.AllowSomePages = True
    If PrintDialog1.ShowDialog = DialogResult.OK Then PrintForm1.PrinterSettings = PrintDialog1.PrinterSettings

    With Me.PrintForm1
        .PrintAction = Printing.PrintAction.PrintToPreview

        Dim MyMargins As New Margins

        With MyMargins
            .Left = 10
            .Right = 10
            .Top = 10
            .Bottom = 10
        End With

        .PrinterSettings.DefaultPageSettings.Margins = MyMargins

        .Print()

    End With

End Sub

but as soon as I try to set which area it should print I get the following error: "Printing is not a member of powerpacks". I tried using the following code according to microsoft this is the way it should work.. I have no clue where the error comes from

.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)  
Fyer-Zero
  • 123
  • 2
  • 11
  • I'm not sure how `.Print()` works - is `root` used by it? Are you sure that DialogResult is returning True? Have you debugged it? I know Foxit, but am unsure how it's being used here - is this an API/library? – Mahendran Nadesan Oct 16 '17 at 14:00
  • The Printform.print() works and I get the dialog to select the printer and in properties of the printer I can select the path, But as soon as I press print I get the error that there is no path selected. Foxit is just the actual printer I am using, Also when I select an actual physical printer I get the same error. – Fyer-Zero Oct 16 '17 at 14:09
  • Ok, but I don't understand how the printer would know that the path that you've set above is the path it's meant to use. Could you perhaps post more code? I really recommend placing breakpoints in your code, and getting at least the inner exception and/or stacktrace for the bit that fails. – Mahendran Nadesan Oct 16 '17 at 14:14
  • 1
    A "PDF printer" is not actually a printer. It is merely a printer driver that *acts* like a printer. Very convenient, that permits any app that can print to generate a PDF file. But you have to take care of the detail, it needs to know the path of the file. You must set PrinterSettings.PrintToFile to *true* and set the PrintFileName property. Now you know it. – Hans Passant Oct 16 '17 at 15:41

2 Answers2

0

You don't need a path to use printform. Printform just prints what you see on the screen to your default printer. You need to install "Visual Basic PowerPacks" to use this command. More explanation you may find here: https://learn.microsoft.com/en-us/dotnet/visual-basic/developing-apps/printing/how-to-print-a-form-by-using-the-printform-component

minimalist
  • 87
  • 4
  • thats what I dont get everyone says you dont need a path or filename preset and if you leave it empty it should prompt the user for a filename and path. this is what the microsoft help says: "Optionally, select the PrintFileName property and type the full path and file name for the destination file. If you skip this step, the user will be prompted for a file name at run time." but I keep getting this error: SYstem.argumentnullexception: Path Canoot be null. Parameter name: path. – Fyer-Zero Oct 17 '17 at 07:01
  • Apart from that, when I do select a path in the print properties I still get the same error. Plus I do have the powerpack installen – Fyer-Zero Oct 17 '17 at 07:04
  • When I double click on the printform in the toolbox it adds PrintForm1 to the project. So in printbutton I just need to add: PrintForm1.Print() and it prints what I see on the screen to pdf because I have setup cute pdf as my default printer. – minimalist Oct 17 '17 at 07:53
  • Tried changing my default printer still does not work. Edited my code a bit and nog managed to get it working. See the new code in the question. – Fyer-Zero Oct 17 '17 at 08:19
0

To preview your print you don't need to use printdialog and all this. You just click on printform1 in the designer,to get up the properties window of printform1. In printaction you choose PrintToPreview. Thats all its needed. These are all the lines I need:

Public Class Form1
Private Sub Exit_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Application.Exit()
End Sub

Private Sub Print_Click(sender As Object, e As EventArgs) Handles Button1.Click
    PrintForm1.Print()
End Sub

End Class

minimalist
  • 87
  • 4