Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1470

Print to specific PDF file name

$
0
0
Assumes Windows 10 with the optional Microsoft Print to PDF driver installed. This is normally the default unless the user has removed it via Windows Features.

Bare bones example. Note that for the most part printing must be done via API calls to avoid raising the driver's file picker dialog:

Code:

Option Explicit

Private Declare Function EndDoc Lib "gdi32" (ByVal hDC As Long) As Long

Private Declare Function EndPage Lib "gdi32" (ByVal hDC As Long) As Long

Private Type DOCINFO
    cbSize As Long
    DocName As String
    Output As String
    Datatype As String
    fwType As Long
End Type

Private Declare Function StartDoc Lib "gdi32" Alias "StartDocA" ( _
    ByVal hDC As Long, _
    ByRef DOCINFO As DOCINFO) As Long

Private Declare Function StartPage Lib "gdi32" (ByVal hDC As Long) As Long

Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" ( _
    ByVal hDC As Long, _
    ByVal X As Long, _
    ByVal Y As Long, _
    ByVal Text As String, _
    ByVal nCount As Long) As Long

Private Sub Main()
    Const LMARGIN As Long = 300
    Const TMARGIN As Long = 500
    Dim P As Printer
    Dim DOCINFO As DOCINFO
    Dim LineHeight As Long
    Dim Y As Long
    Dim I As Long
    Dim Text As String

    For Each P In Printers
        If P.DeviceName = "Microsoft Print to PDF" Then
            Set Printer = P
            GoTo FoundPDF
        End If
    Next
    MsgBox "No ""Microsoft Print to PDF"" driver!", vbExclamation
    Exit Sub

FoundPDF:
    'We can't use Printer.EndPage, Printer.EndDoc, Printer.Print, Printer.PaintPicture,
    'etc. because we need StartDoc() which VB6 will try to do for us "automagically,"
    'thus preventing us from designating the output file programmatically.  I.e. the
    'user will always see the driver's dialog to pick an output file.
    With Printer
        On Error Resume Next
        Kill App.Path & "\specific.pdf"
        On Error GoTo 0
        With DOCINFO
            .cbSize = LenB(DOCINFO)
            .DocName = "To specific file"
            .Output = App.Path & "\specific.pdf"
        End With
        StartDoc .hDC, DOCINFO
        With .Font
            .Name = "Arial"
            .Size = 12
        End With
        LineHeight = Int(.ScaleY(.TextHeight("X"), .ScaleMode, vbPixels))
        StartPage .hDC
        Y = TMARGIN
        For I = 1 To 10
            Text = "Hello"
            TextOut .hDC, LMARGIN, Y, Text, Len(Text)
            Y = Y + LineHeight
            Text = "world!"
            TextOut .hDC, LMARGIN + 250, Y, Text, Len(Text)
            Y = Y + LineHeight * 2 'Double-space here.
        Next
        EndPage .hDC
        EndDoc .hDC
    End With
    MsgBox "Complete"
End Sub

DrawText() normally makes more sense than TextOut(). You can use StdPicture.Render() but not Printer.PaintPicture(), or use BitBlt(), StretchBlt(), etc. for images.

Viewing all articles
Browse latest Browse all 1470

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>