PrintDocument control 'Save As' form.

OzMaz

New member
Joined
Jul 28, 2022
Messages
3
Programming Experience
10+
Hi Folks,

Just finishing off a project and tidying things up.

My last task is to work out the save process of the Print() function. By single stepping, I have determined that as soon as the print() function is executed a 'Save As' form is displayed (as I'm saving to PDF). The one thing I want to do is preset the file name in code. I have provided a file name in the DocumentName property but that doesn't seem to pass through At the moment the filename is blank.

Any ideas?
 
You are presumably selecting some specific PDF printer driver. We would have to know more about that. What you want may not be possible but, if it is, it is likely to be specific to that printer driver.
 
Print functions.:
 Private Sub RelocatableSourceToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RelocatableSourceToolStripMenuItem.Click
        Const LinesPerPage As Integer = 42
        Dim Line As Integer
        Dim Page As Integer
        Dim AssembledLine As String
        Dim temp As Integer
        Dim temp2 As String

        PrintBufferIndex = 0
        ReDim PrintBuffer2(PrintBufferIndex)

        For Line = 0 To rtObjectCode.Lines.Count - 1
            Page = Int(Line / LinesPerPage)
            If Page > PrintBufferIndex Then
                PrintBufferIndex += 1
                ReDim Preserve PrintBuffer2(PrintBufferIndex)
            End If

            AssembledLine = rtObjectCode.Lines(Line)

            'If the line starts with four hexadecimal digits
            If Regex.Matches(AssembledLine, "\A[0-9A-F]{4}").Count > 0 Then
                'Convert to decimal
                temp = Convert.ToInt32(AssembledLine.Substring(0, 4), 16) + My.Settings.Offset
                temp2 = temp.ToString("X4")
                PrintBuffer2(PrintBufferIndex) &= temp2 & "-" & AssembledLine & vbCrLf

            Else
                PrintBuffer2(PrintBufferIndex) &= "         " & AssembledLine & vbCrLf

            End If

        Next

        Dim Result As DialogResult


        If FileNameNAM.Length > 0 Then              'If a NAM directive was found.
            SaveFilename = FileNameNAM

        Else
            MsgBox("You need a NAM directive." & vbCrLf & vbCrLf & "Try again.",, "Missing filename for printing")
            Exit Sub
        End If

        With PrintDialog1
            .PrintToFile = True
            .PrinterSettings.PrintFileName = SaveFilename
        End With

        Result = PrintDialog1.ShowDialog
        PrintIndex = 0

        With PrintDocument2
            .PrinterSettings = PrintDialog1.PrinterSettings
            .DocumentName = SaveFilename
            .OriginAtMargins = False                                'Margins are not respected.
            .DefaultPageSettings.Landscape = True                   'Makes the first page and following portrait.

            .Print()    'Prints the page(s).

        End With

    End Sub

    Private Sub PrintDocument2_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument2.PrintPage
        Dim MyFont As Font = New Font("Courier New", 10)
        Dim MyFormat As StringFormat = New StringFormat()
        Dim MyTabs() As Single = {100, 30, 30, 30, 30, 30, 100}

        MyFormat.SetTabStops(0, MyTabs)                                                                 'Set tabs.
        e.PageSettings.Landscape = True

        e.Graphics.DrawString(PrintBuffer2(PrintIndex), MyFont, Brushes.Black, 50, 90, MyFormat)        'Code data.

        PrintIndex += 1

        If PrintIndex < PrintBuffer2.Length Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If

    End Sub

I'm using the Microsoft PDF driver.
 
One option could be to use the SaveFileDialog instead:
VB.NET:
Using dialog As New SaveFileDialog
    dialog.Filter = "pdf files|*.pdf"
    dialog.FileName = "suggested name"
    If dialog.ShowDialog = DialogResult.OK Then
        With doc.PrinterSettings
            .PrinterName = "Microsoft Print to PDF"
            .PrintFileName = dialog.FileName
            .PrintToFile = True
        End With
        doc.Print()
    End If
End Using
 
Hi JohnH,

That worked as expected. Much appreciated.

Sometimes you just have to try alternate solutions. :)

JohnM
 
Solution

Latest posts

Back
Top