Solved

Printing with different fonts through API

Posted on 2004-08-30
4
875 Views
Last Modified: 2013-12-03
I need to use the API rather than the printer object because the printer I will be printing to may not exist on the local machine (though "Add Printers"), but will be accessable through the network.

I need to print a line on one font, then print a line in another font.

Someting like this:

This is Arial, 18 pt, Bold

This is New Times Roman, 12 pt, regular

What I have so far:

    Dim lhPrinter As Long
    Dim lReturn As Long
    Dim lpcWritten As Long
    Dim lDoc As Long
    Dim i
    Dim myPrinter As String
    Dim MyDocInfo As DOCINFO
    Dim sText As String
   
    myPrinter = "\\mynetwork\printer1"
    lReturn = OpenPrinter(myPrinter, lhPrinter, 0)
    If lReturn = 0 Then
        MsgBox "The Printer doesn't exist."
        Exit Sub
    End If
   
    MyDocInfo.pDocName = "Clif's Print Test"
    MyDocInfo.pOutputFile = vbNullString
    MyDocInfo.pDatatype = vbNullString
    lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo)

    Call StartPagePrinter(lhPrinter)

    sText = "This is Arial, 18 pt, Bold" & vbCrLf
    'Need to change font to Arial, 18 pt, Bold
    lReturn = WritePrinter(lhPrinter, ByVal sText, Len(sText), lpcWritten)
    sText = "This is New Times Roman, 12 pt, regular" & vbCrLf
    'Need to change font to New Times Roman, 12 pt, regular
    lReturn = WritePrinter(lhPrinter, ByVal sText, Len(sText), lpcWritten)

    lReturn = EndPagePrinter(lhPrinter)
    lReturn = EndDocPrinter(lhPrinter)
    lReturn = ClosePrinter(lhPrinter)
0
Comment
Question by:Clif
  • 2
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
mladenovicz earned 500 total points
ID: 11932620
Try to use CreateFontIndirect API. The CreateFontIndirect function creates a logical font that has the characteristics specified in the specified structure. The font can subsequently be selected as the current font for any device context.

http://cuinl.tripod.com/Tips/print9.htm
0
 
LVL 10

Author Comment

by:Clif
ID: 11933803
mladenovicz,

The code you provided the link to does work, however using the code for a basis in changing fonts doesn't seem to work (or I just don't know what I'm doing)

I have tried the following (modifying the code from the link you provided):
   
    lf.lfHeight = 18 * GetDeviceCaps(hPrintDc, LOGPIXELSY) \ 72
    lf.lfWidth = 0
    lf.lfEscapement = 0
    lf.lfOrientation = 0
    lf.lfWeight = 900
    lf.lfItalic = False
    lf.lfUnderline = False
    lf.lfStrikeOut = False
    lf.lfCharSet = DEFAULT_CHARSET
    lf.lfOutPrecision = OUT_DEFAULT_PRECIS
    lf.lfClipPrecision = CLIP_DEFAULT_PRECIS
    lf.lfQuality = DEFAULT_QUALITY
    lf.lfPitchAndFamily = DEFAULT_PITCH
    lf.lfFaceName = StrConv("Arial" & Chr$(0), vbFromUnicode)
    hFontArial = CreateFontIndirect(lf) 'Create the normal font
   
    lf.lfHeight = 12 * GetDeviceCaps(hPrintDc, LOGPIXELSY) \ 72
    lf.lfWidth = 0
    lf.lfEscapement = 0
    lf.lfOrientation = 0
    lf.lfWeight = 500
    lf.lfItalic = False
    lf.lfUnderline = False
    lf.lfStrikeOut = False
    lf.lfCharSet = DEFAULT_CHARSET
    lf.lfOutPrecision = OUT_DEFAULT_PRECIS
    lf.lfClipPrecision = CLIP_DEFAULT_PRECIS
    lf.lfQuality = DEFAULT_QUALITY
    lf.lfPitchAndFamily = DEFAULT_PITCH
    lf.lfFaceName = StrConv("Times New Roman" & Chr$(0), vbFromUnicode)
    hFontTimes = CreateFontIndirect(lf) 'Create the times new roman font


    'Select normal font structure and save previous font info
    hOldfont = SelectObject(hPrintDc, hFontArial)
    sText = "This is Arial, 18 pt, Bold"
    'Send text to printer, starting at location 100, 100
    result = TextOut(hPrintDc, 100, 100, sText, Len(sText))
    'Reset font back to original
    result = SelectObject(hPrintDc, hOldfont)

    'Select times font structure and save previous font info
    hOldfont = SelectObject(hPrintDc, hFontTimes)
    sText = "This is New Times Roman, 12 pt, regular"
    'Send text to printer, starting at location 100, 120
    result = TextOut(hPrintDc, 100, 120, sText, Len(sText))
    'Reset font back to original
    result = SelectObject(hPrintDc, hOldfont)

Nothing gets printed at all.  Just a blank sheet of paper emerges from the printer.
0
 
LVL 10

Author Comment

by:Clif
ID: 11935125
I'm an idiot.

The code sample I gave you that I said wasn't working wasn't exactly what I had in my VB 6 app.  In my app I had accidentally remmed out the sText = "...".  So it was printing, just empty strings.

It does work perfectly.

Thanks.
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11940228
You're welcomed ;)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question