print api

Just starting out? Need help? Post your questions and find answers here.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: print api

Post by IdeasVacuum »

If you only want to print some text, the PB Print commands can handle that:
PB Printer
If you need something more ambitious, then the best solution (Windows OS only) is:
ABB Klaus Printer Lib
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: print api

Post by TI-994A »

Louise wrote:I want with a new settings to print a text...
...I want to a dialog with the predefined settings to be print.
Hello Louise. This example gets the default printer, configures some presets, and prints some text to it, using Windows API functions:

Code: Select all

If OpenLibrary(0, "winspool.drv")
  defaultPrinterName.s = Space(#MAX_PATH)
  If GetVersion_() & $FF0000   ;Windows 2000/NT/XP
    returnBuffer = #MAX_PATH
    ;for UNICODE use GetDefaultPrinterW
    CallFunction(0, "GetDefaultPrinterA", @defaultPrinterName, @returnBuffer)
  Else   ;Windows 95/98/ME
    GetPrivateProfileString_("WINDOWS", "DEVICE", "", @defaultPrinterName, #MAX_PATH, "Win.Ini")
    defaultPrinterName = StringField(defaultPrinterName, 1, ",")
  EndIf  
  Debug defaultPrinterName
  CloseLibrary(0)

  Define printDriver.s = "WINSPOOL", printerName.s = defaultPrinterName
  Define *sysDEVMODE, *userDEVMODE, *contextDEVMODE.DEVMODE, docInfo.DOCINFO
  
  If OpenPrinter_(printerName, @printerHandle, 0)
    returnBuffer = DocumentProperties_(0, printerHandle, printerName, 0, 0, 0)
    *sysDEVMODE = AllocateMemory(returnBuffer)
    *userDEVMODE = AllocateMemory(returnBuffer)
    DocumentProperties_(0, printerHandle, printerName, *sysDEVMODE, *userDEVMODE, #DM_OUT_BUFFER)
    *contextDEVMODE = *sysDEVMODE
    ClosePrinter_(printerHandle)
    FreeMemory(*sysDEVMODE)
    FreeMemory(*userDEVMODE)
    
    With *contextDEVMODE
      \dmOrientation = #DMORIENT_PORTRAIT
      \dmPaperSize = #DMPAPER_A4
      \dmPrintQuality = #DMRES_DRAFT
      \dmCollate = #DMCOLLATE_TRUE
      \dmCopies = 1     
    EndWith
   
    With docInfo
      \cbSize = SizeOf(DOCINFO)
      \lpszDocName = @"PureBasic Test Print"
      \lpszOutput = #Null
    EndWith  
    
    printerDC = CreateDC_(@printDriver, printerName, 0, *contextDEVMODE)
    jobId = StartDoc_(printerDC, @docInfo)
    StartPage_(printerDC)
      outString.s = "Prints on page 1..."
      TextOut_(printerDC, 200, 200, outString, Len(outString))
    EndPage_(printerDC)
    StartPage_(printerDC)
      outString = "Prints on page 2..."
      TextOut_(printerDC, 200, 200, outString, Len(outString))
    EndPage_(printerDC)
    EndDoc_(printerDC)
    DeleteDC_(printerDC) 
  EndIf    
EndIf
Hope you'll find it useful. :D
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: print api

Post by VB6_to_PBx »

TI-994A wrote:
Louise wrote:I want with a new settings to print a text...
...I want to a dialog with the predefined settings to be print.
Hello Louise. This example gets the default printer, configures some presets, and prints some text to it, using Windows API functions:

Code: Select all

If OpenLibrary(0, "winspool.drv")
  defaultPrinterName.s = Space(#MAX_PATH)
  If GetVersion_() & $FF0000   ;Windows 2000/NT/XP
    returnBuffer = #MAX_PATH
    ;for UNICODE use GetDefaultPrinterW
    CallFunction(0, "GetDefaultPrinterA", @defaultPrinterName, @returnBuffer)
  Else   ;Windows 95/98/ME
    GetPrivateProfileString_("WINDOWS", "DEVICE", "", @defaultPrinterName, #MAX_PATH, "Win.Ini")
    defaultPrinterName = StringField(defaultPrinterName, 1, ",")
  EndIf  
  Debug defaultPrinterName
  CloseLibrary(0)

  Define printDriver.s = "WINSPOOL", printerName.s = defaultPrinterName
  Define *sysDEVMODE, *userDEVMODE, *contextDEVMODE.DEVMODE, docInfo.DOCINFO
  
  If OpenPrinter_(printerName, @printerHandle, 0)
    returnBuffer = DocumentProperties_(0, printerHandle, printerName, 0, 0, 0)
    *sysDEVMODE = AllocateMemory(returnBuffer)
    *userDEVMODE = AllocateMemory(returnBuffer)
    DocumentProperties_(0, printerHandle, printerName, *sysDEVMODE, *userDEVMODE, #DM_OUT_BUFFER)
    *contextDEVMODE = *sysDEVMODE
    ClosePrinter_(printerHandle)
    FreeMemory(*sysDEVMODE)
    FreeMemory(*userDEVMODE)
    
    With *contextDEVMODE
      \dmOrientation = #DMORIENT_PORTRAIT
      \dmPaperSize = #DMPAPER_A4
      \dmPrintQuality = #DMRES_DRAFT
      \dmCollate = #DMCOLLATE_TRUE
      \dmCopies = 1     
    EndWith
   
    With docInfo
      \cbSize = SizeOf(DOCINFO)
      \lpszDocName = @"PureBasic Test Print"
      \lpszOutput = #Null
    EndWith  
    
    printerDC = CreateDC_(@printDriver, printerName, 0, *contextDEVMODE)
    jobId = StartDoc_(printerDC, @docInfo)
    StartPage_(printerDC)
      outString.s = "Prints on page 1..."
      TextOut_(printerDC, 200, 200, outString, Len(outString))
    EndPage_(printerDC)
    StartPage_(printerDC)
      outString = "Prints on page 2..."
      TextOut_(printerDC, 200, 200, outString, Len(outString))
    EndPage_(printerDC)
    EndDoc_(printerDC)
    DeleteDC_(printerDC) 
  EndIf    
EndIf
Hope you'll find it useful. :D

in PureBasic versions v5.44LTS and upwards
i get the ERROR = Structure field not found: dmOrientation.

how can i solve this error ?

Code: Select all

   With *contextDEVMODE
      \dmOrientation = #DMORIENT_PORTRAIT
      \dmPaperSize = #DMPAPER_A4
      \dmPrintQuality = #DMRES_DRAFT
      \dmCollate = #DMCOLLATE_TRUE
      \dmCopies = 1     
    EndWith
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: print api

Post by VB6_to_PBx »

 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: print api

Post by collectordave »

Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: print api

Post by TI-994A »

dige wrote:...Structure DEVMODE is now changed...

dm.DEVMODE\dmOrientation

change to:

dm.DEVMODE\Printer\dmOrientation
Thanks for sharing the solution. :D
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Post Reply