WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Printing Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Printing direct from Winbatch


Here is some code to print direct from within Winbatch.

You will need to change the Printer variable to the description of the printer you want to send the job to. This is the text that you see when you look at your list of printers (Start/Settings/Printers).

If the script crashes after the StartDocA line then you will be left with a Print job spooling and you will have to restart the Print Spool service.

This example creates a string to print. To print something else simply replace the For..Next loop and assign text with what you want to print (leave the StrReplace there).

Iain

#DefineFunction rgb(red,green,blue)
  Return ((red mod 256)+(green mod 256)*256+(blue mod 256)*256*256)
#EndFunction

#DefineFunction Print_CreateFont(gdi32,hdc,name,height,width,weight,italic,underline,strikeout,angle)
   Return DllCall(gdi32,long:"CreateFontA",long:height,long:width,long:angle*10,long:angle*10,long:weight,long:italic,long:underline,long:strikeout,long:0,long:0,long:0,long:0,long:0,lpstr:name)
#EndFunction

#DefineFunction Print_TextOut(gdi32,hdc,text,x,y,font,colour,align)
   IntControl(73,1,0,0,0)
   DllCall(gdi32,long:"SetTextColor",long:hdc,long:colour)
   DllCall(gdi32,long:"SetTextAlign",long:hdc,long:align)
   DllCall(gdi32,long:"SelectObject",long:hdc,long:font)
   DllCall(gdi32,long:"TextOutA",long:hdc, long:x, long:y, lpstr:text,long:StrLen(text))
   Return
   :WBERRORHANDLER
   DllCall(gdi32,long:"AbortDoc", long:hdc)
   DllCall(gdi32,long:"DeleteDC", long:hdc)
Exit

#EndFunction

IntControl(73,1,0,0,0)

TopMargin = 100
LeftMargin = 0
LineLength = 80
LinesPerPage = 63
TextColour = RGB(0,0,0)

PRINTER = "HP DeskJet 970Cxi" ; CHANGE

; values for text alignment
TA_NOUPDATECP    = 0 ; The current position is NOT updated after each text output call (DEFAULT)
TA_UPDATECP      = 1 ; The current position IS updated after each text output call
TA_LEFT          = 0 ; The reference point is on the left edge of the bounding box (DEFAULT) 
TA_RIGHT         = 2 ; The reference point is on the right edge of the bounding box
TA_CENTER        = 6 ; The reference point is middle of the bounding box
TA_TOP           = 0 ; The reference point is on the top edge of the bounding box (DEFAULT) 
TA_BOTTOM        = 8 ; The reference point is on the bottom edge of the bounding box
TA_BASELINE      = 24 ; ?? The reference point is on the baseline of the text ??
TA_RTLREADING    = 256 ; Text is laid out right to left

gdi32=DllLoad(StrCat(DirWindows(1),"gdi32.dll"))
hdc=DllCall(gdi32,long:"CreateDCA", lpstr:"WINSPOOL", lpstr:PRINTER,lpnull,lpnull)

font_heading = Print_CreateFont(gdi32,hdc,"Arial",150,0,700,@FALSE,@FALSE,@FALSE,0)
font_90 = Print_CreateFont(gdi32,hdc,"Courier New",80,0,400,@FALSE,@FALSE,@FALSE,90)
font_45 = Print_CreateFont(gdi32,hdc,"Arial",80,0,400,@FALSE,@FALSE,@FALSE,45)
font_text = Print_CreateFont(gdi32,hdc,"Courier New",50,0,400,@FALSE,@FALSE,@FALSE,0)

DocInfo = BinaryAlloc(5*4)
BinaryPoke4(DocInfo,0,5*4)
BinaryPoke4(DocInfo,4,0)
BinaryPoke4(DocInfo,8,0)
BinaryPoke4(DocInfo,12,0)
BinaryPoke4(DocInfo,16,0)

printjob = DllCall(gdi32,long:"StartDocA",long:hdc, lpbinary:DocInfo)
DllCall(gdi32,long:"StartPage", long:hdc)

Print_TextOut(gdi32,hdc,"This is a heading",800,TopMargin+100,font_heading,RGB(0,0,0),TA_CENTER)

text = ""
For r = 1 To 10
   Text = StrCat("This is line ",r)
   TextColour = RGB(Random(255),Random(255),Random(255))
   Print_TextOut(gdi32,hdc,text,100+r*80,1000,font_90,TextColour,0)
Next r
For r = 11 To 20
   Text = StrCat("This is line ",r)
   TextColour = RGB(Random(255),Random(255),Random(255))
   Print_TextOut(gdi32,hdc,text,r*110-300,1000,font_45,TextColour,0)
Next r

Print_TextOut(gdi32,hdc,"Text ",100,1200,font_text,0,TA_UPDATECP)
For r = 1 To 10
   TextColour = RGB(Random(255),Random(255),Random(255))
   Print_TextOut(gdi32,hdc,r,0,0,font_text,TextColour,TA_UPDATECP)
Next r

text = FileGet(AskFilename("", "c:\", "Text files|*.txt","",1),"")
TextColour = RGB(0,0,0)
y = 0
text = StrReplace(text,@CRLF,@LF)
TopMargin = TopMargin + 1500
For a = 1 To ItemCount(text,@LF)
   line = ItemExtract(a,text,@LF)
   lines = (StrLen(line)/LineLength)+1
   For b = 1 To lines
      PrintText = StrSub(line,1,LineLength)
      line = StrSub(line,LineLength+1,-1)
      Print_TextOut(gdi32,hdc,PrintText,LeftMargin,TopMargin+y*50,font_text,TextColour,0)
      y = y + 1
      If y == LinesPerPage Then
         DllCall(gdi32,long:"EndPage", long:hdc)
         DllCall(gdi32,long:"StartPage", long:hdc)
         y = 0
      EndIf
   Next b
Next a
DllCall(gdi32,long:"EndPage", long:hdc)
DllCall(gdi32,long:"EndDoc", long:hdc)
DllCall(gdi32,long:"DeleteDC", long:hdc)

Exit

;:WBERRORHANDLER
DllCall(gdi32,long:"AbortDoc", long:hdc)
DllCall(gdi32,long:"DeleteDC", long:hdc)
Exit

Article ID:   W16170
File Created: 2004:03:30:15:43:04
Last Updated: 2004:03:30:15:43:04