Solved

Printing with different fonts through API

Posted on 2004-08-30
4
862 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You're welcomed ;)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now