Page 1 of 1

Re: print api

Posted: Fri May 02, 2014 5:13 pm
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

Re: print api

Posted: Fri May 02, 2014 9:05 pm
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

Re: print api

Posted: Tue Feb 07, 2017 7:17 am
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

Re: print api

Posted: Tue Feb 07, 2017 7:38 am
by VB6_to_PBx

Re: print api

Posted: Tue Feb 07, 2017 10:51 am
by collectordave

Re: print api

Posted: Tue Feb 07, 2017 1:28 pm
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