Jump to content

A thin RTF Printer for RTF Files


AutoBert
 Share

Go to solution Solved by AutoBert,

Recommended Posts

As i didn't found a working solution fot RTF Files printing i created this

Spoiler

 

CUI program

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile_x64=RTF_SHellExPrint.exe
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

Global $sFile = "Test.rtf"
Global $sPara = ""
Global $sWorkDir = ""
Global $sVerb = 'Print'
Global $SW_Mode = @SW_HIDE
Global $iTimeOut = 100            ;Time in sec to wait for File

Global $sAllowed_Verbs = 'Open, Edit, Print'
If ($CmdLine[0] >= 1 And $CmdLine[1] <> '') Then $sFile = String($CmdLine[1])
If ($CmdLine[0] >= 2 And $CmdLine[2] <> '') Then $sPara = String($CmdLine[2])
If ($CmdLine[0] >= 3 And $CmdLine[3] <> '') Then $sWorkDir = String($CmdLine[3])
If ($CmdLine[0] >= 4 And $CmdLine[4] <> '') Then $sMode = String($CmdLine[4])
If ($CmdLine[0] >= 5 And $CmdLine[5] <> '') Then $SW_Mode = $CmdLine[5]
If ($CmdLine[0] >= 6 And $CmdLine[6] <> '') Then $iTimeOut = Int($CmdLine[6])

If Not StringInStr($sAllowed_Verbs, $sVerb) Then $sVerb = 'Open'
Global $hDtStart = TimerInit(), $iDtWait
While Not FileExists($sFile)
    Sleep(100)
    $iDtWait = TimerDiff($hDtStart) / 1000
    If $iDtWait > $iTimeOut Then Exit -1
WEnd


Global $iPid = ShellExecute($CmdLine[1], $sPara, $sWorkDir, $sVerb, $SW_Mode)
;MsgBox(64, 'PID ', $iPid, 5)
Exit Int($iPid)

  It print the file with run in the Caller Main...exe:

Func _PrintIt($sPath = $g_sSavePath)
    $g_Busy = True
    _ShowBusy()
    Local $sPrint = StringFormat('%s  "" "Print"  @SW_HIDE', $sPath)
    Local $iPid = Run('RTF_SHellExPrint.exe ' & $sPrint)
    ConsoleWrite('RTF_SHellExPrint.exe ' & $sPrint & ' Ret: ' & Int($iPid) & '|' & @error & @CRLF)
    :#cs  not realy needed  
    While ProcessExists($iPid)
            Sleep(100)
        WEnd
    #ce wait's for printjob end 
    $g_Busy = False
EndFunc   ;==>_

 

 

 

and now i extended it for use of the Microsoft Print to PDF (autofilling the name):

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=..\..\..\..\..\..\IconViewer\Icons\shell32_105.ico
#AutoIt3Wrapper_Outfile_x64=RTF_SHellExPrint.exe
#AutoIt3Wrapper_AU3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4
#AutoIt3Wrapper_Run_Tidy=y
#Au3Stripper_Parameters=/mo
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;#AutoIt3Wrapper_Change2CUI=y
#include <Array.au3>

Global $sFile = "Test.rtf"
Global $sPara = ""
Global $sMode
Global $sWorkDir = ""
Global $sVerb = 'Print'
Global $SW_Mode = @SW_HIDE
Global $iTimeOut = 100            ;Time in sec to wait for File

Global $sAllowed_Verbs = 'Open, Edit, Print'
If ($CmdLine[0] >= 1 And $CmdLine[1] <> '') Then $sFile = String($CmdLine[1])
If ($CmdLine[0] >= 2 And $CmdLine[2] <> '') Then $sPara = String($CmdLine[2])
If ($CmdLine[0] >= 3 And $CmdLine[3] <> '') Then $sWorkDir = String($CmdLine[3])
If ($CmdLine[0] >= 4 And $CmdLine[4] <> '') Then $sMode = String($CmdLine[4])
If ($CmdLine[0] >= 5 And $CmdLine[5] <> '') Then $SW_Mode = $CmdLine[5]
If ($CmdLine[0] >= 6 And $CmdLine[6] <> '') Then $iTimeOut = Int($CmdLine[6])

$sFile = StringReplace($sFile, '%20', ' ')
If Not StringInStr($sAllowed_Verbs, $sVerb) Then $sVerb = 'Open'
;_ArrayDisplay($CmdLine)

Global $hDtStart = TimerInit(), $iDtWait
While Not FileExists($sFile)
    Sleep(100)
    $iDtWait = TimerDiff($hDtStart) / 1000
    If $iDtWait > $iTimeOut Then Exit -1
WEnd


Global $iPid, $hConv
Global $sPDF
Global $aPDF = StringSplit($sFile, '\', 1)

;_ArrayDisplay($aPDF)
For $i = 1 To $aPDF[0] - 1
    $sWorkDir &= $aPDF[$i]
    If $i < ($aPDF[0] - 1) Then $sWorkDir &= '\'
Next
;#cs
$iPid = ShellExecute($sFile, $sPara, $sWorkDir, $sVerb, $SW_Mode)
;Exit Int($iPid)

If $sPara <> 'pdf' Then

    Exit Int($iPid)
Else
    FileChangeDir($sWorkDir)
    $sPDF = StringReplace($aPDF[UBound($aPDF) - 1], '.rtf', '.pdf', -1)
    #cs
    ###################################################
    ### MsgBox(0, $sWorkDir, $sPDF)                 ###
    ### If FileExists($sPDF) Then FileDelete($sPDF) ###
    ###################################################
    #ce
    $hDtStart = TimerInit()
    Do
        ToolTip('Waiting PDF')
        $hConv = GetWinList()
        $iDtWait = TimerDiff($hDtStart) / 1000
        If $iDtWait > $iTimeOut Then Exit -2
    Until $hConv <> ''
    ;#cs
    WinWaitActive($hConv, '', '')
    Sleep(200)
    ControlSend($hConv, '', 1001, $sPDF)
    ControlSend($hConv, '', 1, '!s')
    WinWaitClose($hConv, '', 2)
    If WinWaitActive($hConv, 'Speichern unter bestätigen') Then ControlSend($hConv, '', $iTimeOut / 2, '!J')   ;overwrite = Yes
    ;jetzt kommt ein 'DRUCKT Fenster, z.B. WordPad evtl. warten bis beendet
    Exit Int($iPid)
EndIf

Func GetWinList()
    Local $aWinList = WinList('Druckausgabe speichern unter')
    ;Display($aWinList)
    ConsoleWrite('done' & @CRLF)
    If $aWinList[0][0] > 0 Then Return $aWinList[1][1]
EndFunc   ;==>GetWinList
;#ce

In the Caller only one Parameter must be changed:

Func _PrintIt($sPath = $g_sSavePath)
    ;$g_Busy = True
    ;_ShowBusy()
    Local $sPrint = StringFormat('%s  "PDF" "Print"  @SW_HIDE', $sPath)     ;inserted PDF as parameter
    Local $iPid = Run('RTF_SHellExPrint.exe ' & $sPrint)
    ConsoleWrite('RTF_SHellExPrint.exe ' & $sPrint & ' Ret: ' & Int($iPid) & '|' & @error & @CRLF)
    ;#cs  not realy needed  
    While ProcessExists($iPid)
            Sleep(100)
        WEnd
    ;#ce wait's for printjob end 
    ;$g_Busy = False
EndFunc   ;==>_

The Microsoft PDF Printer must be installed and selected as standard-printer. It must be compiled to a normal exe.  with prints to other printers are also possible . Just with "" as parameter.

Edited by AutoBert
Source edited
Link to comment
Share on other sites

@AutoBert 

Maybe this can help ?

#include-once
#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
#Tidy_Parameters=/sort_funcs /reel

#Region RTF PRINTER Include#

#include <WinAPIGdiDC.au3>
#include <GuiRichEdit.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <SendMessage.au3>
#include <Array.au3>
#include <StaticConstants.au3>

#EndRegion RTF PRINTER Include#

#Region RTF PRINTER HEADER#
; #INDEX# =======================================================================================================================
; Title .........: UDF for Printing RTF files
; AutoIt Version : 3.3.10.2++
; Language ......: English
; Description ...: UDF for Printing RTF files
; Author(s) .....: mLipok, RoyGlanfield
; Modified ......:
; ===============================================================================================================================
#cs
    Title:   UDF for Printing RTF files
    Filename:  RTF_Printer.au3
    Description: UDF for Printing RTF files
    Author:   RoyGlanfield, mLipok
    Modified:
    Last Update: 2014/06/03
    Requirements:
    AutoIt 3.3.10.2 or higher

    http://www.autoitscript.com/forum/topic/127580-printing-richedit/
    http://www.autoitscript.com/forum/topic/161831-rtf-printer-printing-richedit/


    Update History:
    ===================================================
    2014/06/04
    v0.1 First official version

    2014/06/04
    v0.2
    * Global Variable renaming by adding $__
    * extended $__API_RTF_Print
    * new function: _RTF_SetMargins($vMarginLeft = 1, $vMarginTop = 1, $vMarginRight = 1, $vMarginBottom = 1)
    * new function: _RTF_SetNumberOfCopies($iNumberOfCopies = 1)
    * added #Region RTF PRINTER initialization#
    * CleanUp
    * added #forceref
    * added #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7


    2014/06/12
    v0.3
    * Global Variable Renaming add 'RTF' to variable names and added $__ to be sure that no one else used the name
    * Global Variable renaming $aPageInfo to $__aRTFPageInfo to be sure that no one else used the name
    * variable renaming $hInput* to $iCtrlInput*
    * in _RTF_PrintFile() new parameter $fAddIndex
    * Setting RTF_Printer Window Title
    * fix problem with printing more than 16 RTF files >>  GUIDelete($__API_RTF_Print[$__hRTF_Gui])
    * new internal function __RTF_AddLeadingZeros
    * added some more comments
    *

#CE

#EndRegion RTF PRINTER HEADER#

#Region RTF PRINTER Constants#

; #CONSTANTS# ===================================================================================================================
; $__<UDF>_CONSTANT_<NAME>
Global Const $__PDtags = "align 1 ;DWORD lStructSize;" & _
        "HWND hwndOwner;" & _
        "handle hDevMode;" & _
        "handle hDevNames;" & _
        "handle hDC;" & _
        "DWORD Flags;" & _
        "WORD nFromPage;" & _
        "WORD nToPage;" & _
        "WORD nMinPage;" & _
        "WORD nMaxPage;" & _
        "WORD nCopies;" & _
        "handle hInstance;" & _
        "LPARAM lCustData;" & _
        "ptr lpfnPrintHook;" & _
        "ptr lpfnSetupHook;" & _
        "ptr lpPrintTemplateName;" & _
        "ptr lpSetupTemplateName;" & _
        "handle hPrintTemplate;" & _
        "handle hSetupTemplate"

Global Enum _
        $__hRTF_dcc, _
        $__hRTF_Gui, $__hRichEditE, $__hRTF_RichEditPre, _
        $__vRTFLaPgNw, $__vRTFLaPgTot, _
        $__vRTFMarginTop, $__vRTFMarginLeft, $__vRTFMarginRight, $__vRTFMarginBottom, _
        $__iRTFNumberOfCopies, _
        $__API_RTF_Print_Count
; , $tDefaultPrinter, _

#EndRegion RTF PRINTER Constants#

#Region RTF PRINTER Global Variables#

Global $__API_RTF_Print[$__API_RTF_Print_Count]
Global $__aRTFPageInfo[2]; $__aRTFPageInfo[0]= total num of pages   $__aRTFPageInfo[1.....] = 1st char number of each page

#EndRegion RTF PRINTER Global Variables#

#Region RTF PRINTER initialization#

_RTF_SetMargins()
_RTF_SetNumberOfCopies()

#EndRegion RTF PRINTER initialization#

#Region RTF PRINTER CURRENT#

; #FUNCTION# ====================================================================================================================
; Name ..........: _RTF_PrintFile
; Description ...: Print Selected File in the background or with custom window dialog
; Syntax ........: _RTF_PrintFile($sRTF_FileFullPath[, $sDocTitle = Default[, $fPrintNow = True[, $fAddIndex = False]]])
; Parameters ....: $sRTF_FileFullPath   - A string value. Full path to the RTF file to be printed
;                  $sDocTitle           - [optional] A string value. Default is Default. If Default then $sDocTitle = FileName
;                  $fPrintNow           - [optional] A boolean value. Default is True. If true then printing is running in the background.
;                  $fAddIndex           - [optional] A boolean value. Default is False. If true then add Indexing to Document Name in spooler
; Return values .: None ; TODO
; Author ........: mLipok, RoyGlanfield
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/topic/161831-rtf-printer-printing-richedit/
; Example .......: No
; ===============================================================================================================================
Func _RTF_PrintFile($sRTF_FileFullPath, $sDocTitle = Default, $fPrintNow = True, $fAddIndex = False)
    Static $__iRTF_Printer_Counter
    If $__iRTF_Printer_Counter = '' Then $__iRTF_Printer_Counter = 1
    If Not FileExists($sRTF_FileFullPath) Then Return -1

    ; setting DocTitle (document Name in spooler) - if Default then FileName
    If $sDocTitle = Default Then
        $sDocTitle = StringRegExp($sRTF_FileFullPath, '(?i).+\\(.+)', 3)[0]
    EndIf

    ; adding index (as prefix) to the DocTitle (document Name in spooler)
    If $fAddIndex Then
        $sDocTitle = __RTF_AddLeadingZeros(String($__iRTF_Printer_Counter), 5) & '_' & $sDocTitle
    EndIf

    Local $hPrintDc = ''
    #forceref $hPrintDc
    Local $__aRTFPageInfo[2]; $__aRTFPageInfo[0]= total num of pages   $__aRTFPageInfo[1.....] = 1st char number of each page
    Local $iPageInPreview = 1 ; the Page Now on show in the preview

    ; Printer dialog tags
    Local $tDefaultPrinter = __GetDefaultPrinter()

    ; Setting RTF_Printer Window Title
    Local $sRTF_Printer_Title = 'Print RTF ---- ' & $tDefaultPrinter
    $sRTF_Printer_Title = '['& String($__iRTF_Printer_Counter) & '] ' & $sRTF_Printer_Title

    $__API_RTF_Print[$__hRTF_Gui] = GUICreate($sRTF_Printer_Title, 430, 580, -1, -1)

    ; Control whose contents is t be printed--------MUST be RichEdit.
    ; any size will do---- has no effect on the printed copy

    $__API_RTF_Print[$__hRichEditE] = _GUICtrlRichEdit_Create($__API_RTF_Print[$__hRTF_Gui], "", 10, 50, 400, 124, $ES_MULTILINE + $WS_VSCROLL) ;+ $ES_AUTOVSCROLL)
    ; 21000 push the preview control off the page
    ; to be resized and positioned after paper size and orientation has been chosen [print dialog or default]
    $__API_RTF_Print[$__hRTF_RichEditPre] = _GUICtrlRichEdit_Create($__API_RTF_Print[$__hRTF_Gui], "", 21000, 10, 10, 10, $ES_MULTILINE) ;+ $ES_AUTOVSCROLL)
    $__API_RTF_Print[$__vRTFLaPgNw] = GUICtrlCreateLabel("0", 60, 180, 40, 20, $SS_RIGHT)
    GUICtrlCreateLabel(" of ", 100, 180, 20, 20, $SS_Center)
    $__API_RTF_Print[$__vRTFLaPgTot] = GUICtrlCreateLabel("0", 120, 180, 40, 20, $SS_Left)
    Local $hButtonBack = GUICtrlCreateButton("Back", 160, 180, 50, 20)
    Local $hButtonNext = GUICtrlCreateButton("Next", 210, 180, 50, 20)
    Local $hButtonPrintNow = GUICtrlCreateButton("PrintNow", 280, 180, 100, 20)

    Local $EndPage = ''
    #forceref $EndPage
    GUICtrlCreateLabel('Margins-->', 0, 3, 50, 20)
    GUICtrlCreateLabel(' cm--->', 0, 30, 40, 20)
    GUICtrlCreateLabel('Left', 50, 3, 30, 15)
    GUICtrlCreateLabel('Top', 80, 3, 40, 15)
    GUICtrlCreateLabel('Right', 120, 3, 40, 15)
    GUICtrlCreateLabel('Bottom', 160, 3, 40, 15)
    GUICtrlCreateLabel('Copies', 230, 3, 40, 15)
    Local $iCtrlInputMarginLeft = GUICtrlCreateInput($__API_RTF_Print[$__vRTFMarginLeft], 40, 18, 40, 30, $ES_READONLY)
    GUICtrlCreateUpdown(-1)
    GUICtrlSetLimit(-1, 4, 0)

    Local $iCtrlInputMarginTop = GUICtrlCreateInput($__API_RTF_Print[$__vRTFMarginTop], 80, 18, 40, 30, $ES_READONLY)
    GUICtrlCreateUpdown(-1)
    GUICtrlSetLimit(-1, 4, 0)

    Local $iCtrlInputMarginRight = GUICtrlCreateInput($__API_RTF_Print[$__vRTFMarginRight], 120, 18, 40, 30, $ES_READONLY)
    GUICtrlCreateUpdown(-1)
    GUICtrlSetLimit(-1, 4, 0)

    Local $iCtrlInputMarginBottom = GUICtrlCreateInput($__API_RTF_Print[$__vRTFMarginBottom], 160, 18, 40, 30, $ES_READONLY)
    GUICtrlCreateUpdown(-1)
    GUICtrlSetLimit(-1, 4, 0)

    Local $iCtrlInputNumberOfCopies = GUICtrlCreateInput($__API_RTF_Print[$__iRTFNumberOfCopies], 230, 25, 40, 20)
    Local $hButtonPrint = GUICtrlCreateButton("Print", 280, 5, 55, 20)
    Local $hButtonPrintGeneral = GUICtrlCreateButton("General", 280, 25, 60, 20)
    Local $hButtonPrintSetup = GUICtrlCreateButton("Print Setup", 335, 5, 75, 20)
    Local $hButtonNoDialog = GUICtrlCreateButton("No dialog", 340, 25, 70, 20)
    _GUICtrlRichEdit_StreamFromFile($__API_RTF_Print[$__hRichEditE], $sRTF_FileFullPath)
    ; TrayTip('_GUICtrlRichEdit_StreamFromFile',@error,4) ; for test only

    If $fPrintNow = False Then
        GUISetState()
    EndIf

    ; this does not work ---$r = _GUICtrlRichEdit_SetZoom($__API_RTF_Print[$__hRichEditE], 1/2)--------------
    ; Below works well---but does have to be reset after anything is streamed in------
    ; OK--------$r = _SendMessage($h_RichEdit, $EM_SETZOOM, $tDefaultPrinterominator, $denominator)
    ; $r = _SendMessage($__API_RTF_Print[$__hRichEditE], $EM_SETZOOM, 1000, 2333) ; must be integer -- "1000, 2333"  not "1, 2.333"
    ; $r = _SendMessage($__API_RTF_Print[$__hRichEditE], $EM_SETZOOM, 100, 200) ; eg 50%
    Local $r = _SendMessage($__API_RTF_Print[$__hRichEditE], $EM_SETZOOM, 100, 300) ; eg 33.3333333333%
    #forceref $r
    ; $r = _SendMessage($__API_RTF_Print[$__hRTF_RichEditPre], $EM_SETZOOM, 100, 500) ; eg 20%
    Local $z
    Local $iMsg
    If $fPrintNow Then

        ; NO DIALOG

        ; $__API_RTF_Print[$__iRTFNumberOfCopies] = GUICtrlRead($iCtrlInputNumberOfCopies) ; not needed as only prints 1copy
        ; "No dialog" ---No choices
        ; prints 1 copy using the default printer's settings
        $__API_RTF_Print[$__hRTF_dcc] = __GetDC_PrinterNoDialog()
        If IsPtr($__API_RTF_Print[$__hRTF_dcc]) = 1 Then
            $z = __RTF_Preview($__API_RTF_Print[$__hRTF_dcc], $__API_RTF_Print[$__hRichEditE], $__API_RTF_Print[$__vRTFMarginLeft], $__API_RTF_Print[$__vRTFMarginTop], $__API_RTF_Print[$__vRTFMarginRight], $__API_RTF_Print[$__vRTFMarginBottom])
        EndIf

        ; PRINT NOW

        ; "Print Setup" dialog ---choices
        ; number of copies
        ; portrait/landscape choice
        ; paper size
        If IsPtr($__API_RTF_Print[$__hRTF_dcc]) = 1 Then

            __RTF_Print($__API_RTF_Print[$__hRTF_dcc], $__API_RTF_Print[$__hRichEditE], $sDocTitle, $__API_RTF_Print[$__vRTFMarginLeft], $__API_RTF_Print[$__vRTFMarginTop], $__API_RTF_Print[$__vRTFMarginRight], $__API_RTF_Print[$__vRTFMarginBottom])
            $__iRTF_Printer_Counter += 1
        Else

        EndIf
        $hPrintDc = ''
        ; GUISetState() ; for test only
        ; Sleep(3000) ; for test only
    Else
        While 1
            Sleep(10)
            $__API_RTF_Print[$__vRTFMarginLeft] = GUICtrlRead($iCtrlInputMarginLeft)
            $__API_RTF_Print[$__vRTFMarginTop] = GUICtrlRead($iCtrlInputMarginTop)
            $__API_RTF_Print[$__vRTFMarginRight] = GUICtrlRead($iCtrlInputMarginRight)
            $__API_RTF_Print[$__vRTFMarginBottom] = GUICtrlRead($iCtrlInputMarginBottom)
            $__API_RTF_Print[$__iRTFNumberOfCopies] = GUICtrlRead($iCtrlInputNumberOfCopies)

            $iMsg = GUIGetMsg()
            Select

                Case $iMsg = $GUI_EVENT_CLOSE

                    GUIDelete()
                    Exit

                Case $iMsg = $hButtonNoDialog; "No dialog"

                    ; $__API_RTF_Print[$__iRTFNumberOfCopies] = GUICtrlRead($iCtrlInputNumberOfCopies) ; not needed as only prints 1copy
                    ; "No dialog" ---No choices
                    ; prints 1 copy
                    ; using the default printer's settings
                    $__API_RTF_Print[$__hRTF_dcc] = __GetDC_PrinterNoDialog()
                    If IsPtr($__API_RTF_Print[$__hRTF_dcc]) = 1 Then
                        $z = __RTF_Preview($__API_RTF_Print[$__hRTF_dcc], $__API_RTF_Print[$__hRichEditE], $__API_RTF_Print[$__vRTFMarginLeft], $__API_RTF_Print[$__vRTFMarginTop], $__API_RTF_Print[$__vRTFMarginRight], $__API_RTF_Print[$__vRTFMarginBottom])
                        MsgBox(4096, '', $z & ' pages sent to the preview.')
                    EndIf

                Case $iMsg = $hButtonPrintSetup; "PrintSetup"

                    ; "Print Setup" dialog ---choices
                    ; number of copies
                    ; portrait/landscape choice
                    ; paper size
                    $__API_RTF_Print[$__hRTF_dcc] = __GetDC_PrinterSetup($__API_RTF_Print[$__iRTFNumberOfCopies]) ; OK--
                    If IsPtr($__API_RTF_Print[$__hRTF_dcc]) = 1 Then
                        $z = __RTF_Preview($__API_RTF_Print[$__hRTF_dcc], $__API_RTF_Print[$__hRichEditE], $__API_RTF_Print[$__vRTFMarginLeft], $__API_RTF_Print[$__vRTFMarginTop], $__API_RTF_Print[$__vRTFMarginRight], $__API_RTF_Print[$__vRTFMarginBottom])
                        MsgBox(4096, '', $z & ' pages sent to the preview.')
                    EndIf

                Case $iMsg = $hButtonPrint; "Print"

                    ; "Print" ---choices--
                    ; number of copies
                    ; Page range---All, from, to, Selection
                    ; NO-- portrait/landscape choice
                    $__API_RTF_Print[$__hRTF_dcc] = __GetDC_Printer($__API_RTF_Print[$__iRTFNumberOfCopies]) ; OK--"Print" dialog
                    If IsPtr($__API_RTF_Print[$__hRTF_dcc]) = 1 Then
                        $z = __RTF_Preview($__API_RTF_Print[$__hRTF_dcc], $__API_RTF_Print[$__hRichEditE], $__API_RTF_Print[$__vRTFMarginLeft], $__API_RTF_Print[$__vRTFMarginTop], $__API_RTF_Print[$__vRTFMarginRight], $__API_RTF_Print[$__vRTFMarginBottom])
                        MsgBox(4096, '', $z & ' pages sent to the preview.')
                    EndIf

                Case $iMsg = $hButtonPrintGeneral; "General"

                    ; "General" ---choices--
                    ; number of copies
                    ; Page range---All,  Selection
                    ; NO-- portrait/landscape choice-----------
                    $__API_RTF_Print[$__hRTF_dcc] = __GetDC_PrinterGeneral($__API_RTF_Print[$__hRichEditE], $__API_RTF_Print[$__iRTFNumberOfCopies])
                    If IsPtr($__API_RTF_Print[$__hRTF_dcc]) = 1 Then
                        $z = __RTF_Preview($__API_RTF_Print[$__hRTF_dcc], $__API_RTF_Print[$__hRichEditE], $__API_RTF_Print[$__vRTFMarginLeft], $__API_RTF_Print[$__vRTFMarginTop], $__API_RTF_Print[$__vRTFMarginRight], $__API_RTF_Print[$__vRTFMarginBottom])
                        MsgBox(4096, '', $z & ' pages sent to the preview.')
                    EndIf

                Case $iMsg = $hButtonPrintNow;

                    ; "Print Setup" dialog ---choices
                    ; number of copies
                    ; portrait/landscape choice
                    ; paper size
                    If IsPtr($__API_RTF_Print[$__hRTF_dcc]) = 1 Then
                        MsgBox(4096, '', __RTF_Print($__API_RTF_Print[$__hRTF_dcc], $__API_RTF_Print[$__hRichEditE], $sDocTitle, $__API_RTF_Print[$__vRTFMarginLeft], $__API_RTF_Print[$__vRTFMarginTop], $__API_RTF_Print[$__vRTFMarginRight], $__API_RTF_Print[$__vRTFMarginBottom]))
                    Else

                    EndIf
                    $hPrintDc = ''

                Case $iMsg = $hButtonNext;

                    If $iPageInPreview < $__aRTFPageInfo[0] Then
                        $iPageInPreview += 1
                        __NextPage($iPageInPreview)
                    EndIf

                Case $iMsg = $hButtonBack;

                    If $iPageInPreview > 1 Then
                        $iPageInPreview -= 1
                        __NextPage($iPageInPreview)
                    EndIf

            EndSelect
        WEnd
    EndIf

    ; CleanUp
    If IsHWnd($__API_RTF_Print[$__hRTF_Gui]) Then GUIDelete($__API_RTF_Print[$__hRTF_Gui])
EndFunc   ;==>_RTF_PrintFile

; #FUNCTION# ====================================================================================================================
; Name ..........: _RTF_SetMargins
; Description ...: Set Margins size in cm [centimeters]
; Syntax ........: _RTF_SetMargins([$vMarginLeft = 1[, $vMarginTop = 1[, $vMarginRight = 1[, $vMarginBottom = 1]]]])
; Parameters ....: $vMarginLeft         - [optional] A variant value. Default is 1.
;                  $vMarginTop          - [optional] A variant value. Default is 1.
;                  $vMarginRight        - [optional] A variant value. Default is 1.
;                  $vMarginBottom       - [optional] A variant value. Default is 1.
; Return values .: None
; Author ........: mLipok
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _RTF_SetMargins($vMarginLeft = 1, $vMarginTop = 1, $vMarginRight = 1, $vMarginBottom = 1)
    $__API_RTF_Print[$__vRTFMarginLeft] = $vMarginLeft ; initial minimum Left margin
    $__API_RTF_Print[$__vRTFMarginTop] = $vMarginTop ; initial minimum Top margin
    $__API_RTF_Print[$__vRTFMarginRight] = $vMarginRight ; initial minimum Right margin
    $__API_RTF_Print[$__vRTFMarginBottom] = $vMarginBottom ; initial minimum Bottom margin
EndFunc   ;==>_RTF_SetMargins

; #FUNCTION# ====================================================================================================================
; Name ..........: _RTF_SetNumberOfCopies
; Description ...: Set number of copies
; Syntax ........: _RTF_SetNumberOfCopies([$iNumberOfCopies = 1])
; Parameters ....: $iNumberOfCopies     - [optional] An integer value. Default is 1.
; Return values .: None
; Author ........: mLipok
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _RTF_SetNumberOfCopies($iNumberOfCopies = 1)
    $__API_RTF_Print[$__iRTFNumberOfCopies] = $iNumberOfCopies ; initial number of Copies to print
EndFunc   ;==>_RTF_SetNumberOfCopies
#EndRegion RTF PRINTER CURRENT#

#Region RTF PRINTER INTERNAL#


; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __GetDC_PrinterSetup
; Description ...: Get the device context of a "PrinterSetup"  Dialog box
; Syntax ........: __GetDC_PrinterSetup([$iCopies = 1])
; Parameters ....: $iCopies             - [optional] An integer value. Default is 1. Number of copies to print.
; Return values .: Success: Device context
;                  Failure: 0
; Author ........: RoyGlanfield
; Modified ......: mLipok
; Remarks .......: Has choices for -- portrait/landscape, printer, paper size
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/topic/127580-printing-richedit/
; Example .......: No
; ===============================================================================================================================
Func __GetDC_PrinterSetup($iCopies = 1)
    Local $strcPD = DllStructCreate($__PDtags)
    DllStructSetData($strcPD, "lStructSize", DllStructGetSize($strcPD))
    ; DllStructSetData($strcPD, "hwndOwner", $hwnd) ;
    ; Constants--$PD_RETURNDC = 0x100----$PD_PRINTSETUP = 0x40
    DllStructSetData($strcPD, "Flags", 0x100 + 0x40) ; different flags open different dialogue boxes
    DllStructSetData($strcPD, "nCopies", $iCopies) ; set the number of copies
    ;   DllStructSetData($strcPD,"nFromPage", 1) ; start from page #
    ; DllStructSetData($strcPD, "nToPage", 0xFFFF)
    ; DllStructSetData($strcPD, "nMinPage", 1)
    ; DllStructSetData($strcPD, "nMaxPage", 0xFFFF)
    Local $bRet = DllCall("Comdlg32.dll", "int", "PrintDlgW", "ptr", DllStructGetPtr($strcPD))
    Local $hDC = 0
    If $bRet[0] = True Then
        $hDC = DllStructGetData($strcPD, "hDC")
    EndIf
    $strcPD = ''
    Return $hDC
EndFunc   ;==>__GetDC_PrinterSetup

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __GetDC_Printer
; Description ...: Get the device context of a "Print" Dialog box
; Syntax ........: __GetDC_Printer([$iCopies = 1])
; Parameters ....: $iCopies             - [optional] An integer value. Default is 1. Number of copies to print.
; Return values .: Success: Device context
;                  Failure: 0
; Author ........: RoyGlanfield
; Modified ......: mLipok
; Remarks .......: Has choices for -- many, but no portrait/landscape
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/topic/127580-printing-richedit/
; Example .......: No
; ===============================================================================================================================
Func __GetDC_Printer($iCopies = 1)
    Local $strcPD = DllStructCreate($__PDtags)
    DllStructSetData($strcPD, "lStructSize", DllStructGetSize($strcPD))
    ; DllStructSetData($strcPD, "hwndOwner", $hwnd) ;
    ; Constant--$PD_RETURNDC = 0x100
    DllStructSetData($strcPD, "Flags", 0x100) ; different flags open different dialogue boxes
    DllStructSetData($strcPD, "nCopies", $iCopies) ; set the number of copies
    DllStructSetData($strcPD, "nFromPage", 1) ; start from page #
    DllStructSetData($strcPD, "nToPage", 0xFFFF)
    DllStructSetData($strcPD, "nMinPage", 1)
    DllStructSetData($strcPD, "nMaxPage", 0xFFFF)
    Local $bRet = DllCall("Comdlg32.dll", "int", "PrintDlgW", "ptr", DllStructGetPtr($strcPD))
    Local $hDC = 0
    If $bRet[0] = True Then
        $hDC = DllStructGetData($strcPD, "hDC")
    EndIf
    $strcPD = ''
    Return $hDC
EndFunc   ;==>__GetDC_Printer

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __GetDC_PrinterNoDialog
; Description ...: Get the device context of  the default printer
; Syntax ........: __GetDC_PrinterNoDialog()
; Parameters ....:
; Return values .: Success: Device context
;                  Failure: 0
; Author ........: RoyGlanfield
; Modified ......: mLipok
; Remarks .......: Uses the default printers settings
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/topic/127580-printing-richedit/
; Example .......: No
; ===============================================================================================================================
Func __GetDC_PrinterNoDialog()
    Local $strcPD = DllStructCreate($__PDtags)
    DllStructSetData($strcPD, "lStructSize", DllStructGetSize($strcPD))
    ; DllStructSetData($strcPD, "hwndOwner", $hwnd) ;
    ; Const--RETURNDEFAULT = 0x400---RETURNDC = 0x100----PRINTSETUP = 0x40
    DllStructSetData($strcPD, "Flags", 0x100 + 0x40 + 0x400) ; different flags open different dialogue boxes
    ; DllStructSetData($strcPD, "nCopies", $iCopies) ; only set this if a dialog is shown
    ;   DllStructSetData($strcPD,"nFromPage", 1) ; start from page #
    ; DllStructSetData($strcPD, "nToPage", 0xFFFF)
    ; DllStructSetData($strcPD, "nMinPage", 1)
    ; DllStructSetData($strcPD, "nMaxPage", 0xFFFF)
    Local $bRet = DllCall("Comdlg32.dll", "int", "PrintDlgW", "ptr", DllStructGetPtr($strcPD))
    Local $hDC = 0
    If $bRet[0] = True Then
        $hDC = DllStructGetData($strcPD, "hDC")
    EndIf
    $strcPD = ''
    Return $hDC
EndFunc   ;==>__GetDC_PrinterNoDialog

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __GetDC_PrinterGeneral
; Description ...: Get the device context of a "PrinterSetup  General tab" Dialog box
; Syntax ........: __GetDC_PrinterGeneral($hRichEditCtrl[, $iCopies = 1])
; Parameters ....: $hRichEditCtrl                - A handle value. Handle of the RichEdit control.
;                  $iCopies             - [optional] An integer value. Default is 1. Number of copies to print.
; Return values .: Success: Device context
;                  Failure: 0
; Author ........: RoyGlanfield
; Modified ......: mLipok
; Remarks .......: Has choices for -- many, but no portrait/landscape
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/topic/127580-printing-richedit/
; Example .......: No
; ===============================================================================================================================
Func __GetDC_PrinterGeneral($hRichEditCtrl, $iCopies = 1) ; Printer Setup, general options---NO landscape!
    Local $strcPD = DllStructCreate($__PDtags)
    DllStructSetData($strcPD, "lStructSize", DllStructGetSize($strcPD))
    DllStructSetData($strcPD, "hwndOwner", $hRichEditCtrl) ;
    ; Constant--$PD_RETURNDC = 0x100
    DllStructSetData($strcPD, "Flags", 0x100) ; different flags open different dialogue boxes
    DllStructSetData($strcPD, "nCopies", $iCopies) ; set the number of copies
    ; DllStructSetData($strcPD,"nFromPage", 10) ; start from page #
    ; DllStructSetData($strcPD, "nToPage", 0xFFFF)
    ; DllStructSetData($strcPD, "nMinPage", 1)
    ; DllStructSetData($strcPD, "nMaxPage", 0xFFFF)
    Local $bRet = DllCall("Comdlg32.dll", "int", "PrintDlgW", "ptr", DllStructGetPtr($strcPD))
    Local $hDC = 0
    If $bRet[0] = True Then
        $hDC = DllStructGetData($strcPD, "hDC")
    EndIf
    $strcPD = ''
    Return $hDC
EndFunc   ;==>__GetDC_PrinterGeneral

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __GetDefaultPrinter
; Description ...: Get default printer dialog tags
; Syntax ........: __GetDefaultPrinter()
; Parameters ....:
; Return values .: Printer dialog tags
; Author ........: RoyGlanfield
; Modified ......: mLipok
; Remarks .......:
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/topic/127580-printing-richedit/
; Example .......: No
; ===============================================================================================================================
Func __GetDefaultPrinter()
    Local $tags1 = DllStructCreate("dword")
    DllCall("winspool.drv", "int", "GetDefaultPrinter", "str", '', "ptr", DllStructGetPtr($tags1))
    Local $tags2 = DllStructCreate("char[" & DllStructGetData($tags1, 1) & "]")
    DllCall("winspool.drv", "int", "GetDefaultPrinter", "ptr", DllStructGetPtr($tags2), "ptr", DllStructGetPtr($tags1))
    Return DllStructGetData($tags2, 1)
EndFunc   ;==>__GetDefaultPrinter

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __NextPage
; Description ...: Show selected page in preview
; Syntax ........: __NextPage($iPageNumber)
; Parameters ....: $iPageNumber         - An integer value.
; Return values .: None
; Author ........: RoyGlanfield
; Modified ......: mLipok
; Remarks .......:
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/topic/127580-printing-richedit/
; Example .......: No
; ===============================================================================================================================
Func __NextPage($iPageNumber)

    ; stream into preview all up to the end of the page to show
    ; _GUICtrlRichEdit_SetSel($__API_RTF_Print[$__hRichEditE], 0, $__aRTFPageInfo[$iPageNumber + 1] - 1) ; '-1' because $__aRTFPageInfo[$iPageNumber+1] is the 1st char of next page
    _GUICtrlRichEdit_SetSel($__API_RTF_Print[$__hRichEditE], 0, $__aRTFPageInfo[$iPageNumber + 1]) ; '-1' because $__aRTFPageInfo[$iPageNumber+1] is the 1st char of next page
    Local $stream = _GUICtrlRichEdit_StreamToVar($__API_RTF_Print[$__hRichEditE])
    _GUICtrlRichEdit_StreamFromVar($__API_RTF_Print[$__hRTF_RichEditPre], $stream)
    _GUICtrlRichEdit_HideSelection($__API_RTF_Print[$__hRTF_RichEditPre])

    ; now delete all before the page to show this way the font size etc. is continued, rather than reverting to the default.
    _GUICtrlRichEdit_SetSel($__API_RTF_Print[$__hRTF_RichEditPre], 0, $__aRTFPageInfo[$iPageNumber])
    _GUICtrlRichEdit_ReplaceText($__API_RTF_Print[$__hRTF_RichEditPre], '')
    Local $r = _SendMessage($__API_RTF_Print[$__hRTF_RichEditPre], $EM_SETZOOM, 100, 500) ; eg 20%
    #forceref $r
    GUICtrlSetData($__API_RTF_Print[$__vRTFLaPgNw], $iPageNumber)
EndFunc   ;==>__NextPage

Func __RTF_AddLeadingZeros($iDigitToExpand, $iNumberOfDigits)
    Return StringRight("00000000000000000000000000000000000" & String($iDigitToExpand), $iNumberOfDigits)
EndFunc   ;==>__RTF_AddLeadingZeros

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __RTF_Preview
; Description ...: Show a "Print Preview" from RichEdit control
; Syntax ........: __RTF_Preview($hPrintDc, $hRichEditCtrl[, $LeftMinMrgn = 1[, $TopMinMrgn = 1[, $RightMinMrgn = 1[, $BottomMinMrgn = 1]]]])
; Parameters ....: $hPrintDc            - A handle value. Printer's device context.
;                  $hRichEditCtrl                - A handle value. Handle of the RichEdit control.
;                  $LeftMinMrgn         - [optional] An unknown value. Default is 1. Minimum margin on the left.
;                  $TopMinMrgn          - [optional] An unknown value. Default is 1. Minimum margin on the top.
;                  $RightMinMrgn        - [optional] An unknown value. Default is 1. Minimum margin on the Right.
;                  $BottomMinMrgn       - [optional] An unknown value. Default is 1. Minimum margin on the Bottom.
; Return values .: None
; Author ........: RoyGlanfield
; Modified ......: mLipok
; Remarks .......: Orientation, paper size, number of copies, and which printer are set when the Printer's device context is generated.
;                  Unexpected results may be caused by single line/paragaph orphin tags in the rtf.
;                  The Preview RichEdit Control is resized and zoomed 20%
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/topic/127580-printing-richedit/
; Example .......: No
; ===============================================================================================================================
Func __RTF_Preview($hPrintDc, $hRichEditCtrl, $LeftMinMrgn = 1, $TopMinMrgn = 1, $RightMinMrgn = 1, $BottomMinMrgn = 1)

    ; convert the margins 0.1 inches to twips
    ; $TopMinMarg = $TopMinMarg * 144; eg 10*144 = 1440 which 1 inch
    ; $LeftMinMarg = $LeftMinMarg * 144; eg 10*144 = 1440 which 1 inch
    ; $PageMinMarg = $PageMinMarg * 144; eg 10*144 = 1440 which 1 inch
    ; $BottomMinMarg = $BottomMinMarg * 144; eg 10*144 = 1440 which 1 inch

    ; convert the margins 1 cm to twips
    $TopMinMrgn = $TopMinMrgn * 567; eg 2.539cm * 567= ~1440 which 1 inch
    $LeftMinMrgn = $LeftMinMrgn * 567; eg 2.539cm * 567 = ~1440 which 1 inch
    $RightMinMrgn = $RightMinMrgn * 567; eg 2.539cm * 567 = ~1440 which 1 inch
    $BottomMinMrgn = $BottomMinMrgn * 567; eg 2.539cm * 567 = ~1440 which 1 inch

    ; $hPrintDc ; Divice Context handle-----------
    ; dots per inch depends on the printer quality setting-------X and Y can be different!
    Local $dotInchX = _WinAPI_GetDeviceCaps($hPrintDc, 88) ; Const LOGPIXELSX = 88
    Local $dotInchY = _WinAPI_GetDeviceCaps($hPrintDc, 90) ; Const LOGPIXELSY = 90

    ; printer dots per inch
    ; get the printable area  [Page] and paper area [Paper]
    Local $PageW = _WinAPI_GetDeviceCaps($hPrintDc, 8) ; Const HORZRES= 8
    Local $PageH = _WinAPI_GetDeviceCaps($hPrintDc, 10) ; Const VERTRES = 10
    Local $PaperW = _WinAPI_GetDeviceCaps($hPrintDc, 110) ; Const PHYSICALWIDTH = 110
    Local $PaperH = _WinAPI_GetDeviceCaps($hPrintDc, 111) ; Const PHYSICALHEIGHT = 111

    ; none printable margins
    Local $OffSetX = _WinAPI_GetDeviceCaps($hPrintDc, 112) ; Const PHYSICALOFFSETX = 112
    Local $OffSetY = _WinAPI_GetDeviceCaps($hPrintDc, 113) ; Const PHYSICALOFFSETY = 113
    Local $RightMrgn = $PaperW - $PageW - $OffSetX
    Local $BottomMrgn = $PaperH - $PageH - $OffSetY

    ; conversion factors to use later-----------
    Local $TwipsInchX = $dotInchX / 1440 ; convert dots to twips [per inch]
    Local $TwipsInchY = $dotInchY / 1440 ; convert dots to twips [per inch]

    ; convert all measurments to twips
    $OffSetX = $OffSetX / $TwipsInchX ; convert Left dots to twips
    $OffSetY = $OffSetY / $TwipsInchY ; convert Left dots to twips
    $PageW = $PageW / $TwipsInchX ; convert Right dots to twips
    $PageH = $PageH / $TwipsInchY ; convert Right dots to twips
    $PaperW = $PaperW / $TwipsInchX ; convert Paper Width dots to twips
    $PaperH = $PaperH / $TwipsInchY ; convert Paper Width dots to twips

    ; Set the margins and keep everything in the printable area
    Local $Left1 = $LeftMinMrgn - $OffSetX
    If $Left1 < 0 Then $Left1 = 0 ; dont print before printable area starts

    Local $Top1 = $TopMinMrgn - $OffSetY
    If $Top1 < 0 Then $Top1 = 0 ; dont print before printable area starts

    Local $Right1 = $RightMinMrgn - $RightMrgn
    If $Right1 < 0 Then $Right1 = 0 ; dont print after printable area ends
    ; $Right1 = $PaperW - $Right1 - $OffSetX
    $Right1 = $PageW - $Right1 ; $OffSetX+$Left1-$RightMrgn

    Local $Bottom1 = $BottomMinMrgn - $BottomMrgn
    If $Bottom1 < 0 Then $Bottom1 = 0 ; dont print after printable area ends
    $Bottom1 = $PageH - $Bottom1

    Local $z = _SendMessage($__API_RTF_Print[$__hRTF_RichEditPre], $EM_SETTARGETDEVICE, $hPrintDc, $Right1 - $Left1) ;

    If $z = 0 Then Return 'Cant find RichEdit Control'

    If _GUICtrlRichEdit_GetTextLength($hRichEditCtrl) < 1 Then Return 'Nothing to Print.'

    ; must have a selection on the richEdit control---------------
    _SendMessage($hRichEditCtrl, $EM_SETSEL, 0, -1) ; ok----select all

    Local $pgTags = "int Left1 ;int Top1 ;int Right1 ;int Bottom1 ;int Left2;int Top2;int Right2;int Bottom2;"
    Local $rgTags = "LONG cpMin;LONG cpMax"
    Local $dcHTags = "HANDLE hdc;HANDLE hdcTarget;"

    ; create a structure for the printed page
    Local $strcPg = DllStructCreate($dcHTags & $pgTags & $rgTags)
    DllStructSetData($strcPg, "hdc", $hPrintDc) ; printer
    DllStructSetData($strcPg, "Left1", $Left1) ; twip--------printer
    DllStructSetData($strcPg, "Right1", $Right1) ; twip--------printer
    DllStructSetData($strcPg, "Top1", $Top1) ; twip--------printer
    DllStructSetData($strcPg, "Bottom1", $Bottom1) ; twip--------printer

    ; next 7 lines seem to have, no effect or crash printer jobs queue---why???
    ; "HANDLE hdc;" is the printer------- before conecting printer to rtf???
    ; "HANDLE hdcTarget;" is the target RichEdit control that has the rtf ????
    ; DllStructSetData($strcPg,"hdcTarget",$hPrintDc) ; richEdit scource???
    ; DllStructSetData($strcPg,"Left2",$Left1/$dotInchX*96/3) ; richEdit scource???
    ; DllStructSetData($strcPg,"Top2",$Top1/$dotInchX*96/3) ; richEdit scource???
    ; DllStructSetData($strcPg,"Right2",$Page1/$dotInchX*96/3) ; twip------richEdit scource???
    ; DllStructSetData($strcPg,"Bottom2",$Bottom1/$dotInchX*96/3) ; twip-----richEdit scource???

    ; get the pointer to all that will be printed??? {I think????????}
    Local $a = DllStructGetPtr($strcPg) + DllStructGetSize(DllStructCreate($dcHTags & $pgTags))

    ; use this pointer-----------
    _SendMessage($hRichEditCtrl, $EM_EXGETSEL, 0, $a)
    ReDim $__aRTFPageInfo[2]
    $__aRTFPageInfo[1] = 1
    $__aRTFPageInfo[0] = 0 ; the total number of pages
    Local $iPageInPreview = 1 ; the page number of the preview now showing
    #forceref $iPageInPreview

    ; find the last char of the document to be printed
    Local $cpMax = DllStructGetData($strcPg, "cpMax")

    ; set the 1st page start char-----------
    Local $cpMin = 0
    Local $cpMin2 = -1

    ; -----------------------------------------------------------
    ; make a loop to format each printed page and exit when
    ; $cpMin reaches the $cpMax
    ; $cpMin is less than 0---{I have seen -1 but I forget when}---
    ; ALSO-- ExitLoop if $cpMin = $cpMin 2---if it stops finding the start of next page
    While $cpMax > $cpMin And $cpMin > -1

        ; get the 1st char of the next page, {but how does it work ???}
        $cpMin2 = $cpMin
        $cpMin = _SendMessage($hRichEditCtrl, $EM_FORMATRANGE, True, DllStructGetPtr($strcPg))
        If $cpMin2 = $cpMin Then ExitLoop; get out of loop before more pages are added

        ; set the next page start char-----------
        DllStructSetData($strcPg, "cpMin", $cpMin)
        $__aRTFPageInfo[0] += 1 ; increment page the total page count

        ; update array --- for what to show in the preview
        _ArrayAdd($__aRTFPageInfo, $cpMin) ; start char-number of each page

        ; end the page and loop again for the next page until the end of document
    WEnd
    _SendMessage($hRichEditCtrl, $EM_FORMATRANGE, False, 0)

    ; adjust the preview shape/size
    WinMove($__API_RTF_Print[$__hRTF_RichEditPre], '', 210 - $PaperW / 1440 * 96 / 5 / 2, 200, $PaperW / 1440 * 96 / 5 + 15, $PaperH / 1440 * 96 / 5 + 15)

    ; the printer starts printing at offsetX and offsetY but the preview windiw starts at 1 and 1
    ; so add offsetX and offsetY to the page margins to the printer would use
    _GUICtrlRichEdit_SetRECT($__API_RTF_Print[$__hRTF_RichEditPre], ($Left1 + $OffSetX) / $dotInchX * 96 / 5, ($Top1 + $OffSetY) / $dotInchY * 96 / 5, 460, 500)
    __NextPage(1)
    GUICtrlSetData($__API_RTF_Print[$__vRTFLaPgTot], $__aRTFPageInfo[0])
    Return $__aRTFPageInfo[0]
EndFunc   ;==>__RTF_Preview

; #FUNCTION# ====================================================================================================================
; Name ..........: __RTF_Print
; Description ...: Print from RichEdit control
; Syntax ........: __RTF_Print($hPrintDc, $hRichEditCtrl, $sDocTitle[, $LeftMinMrgn = 1[, $TopMinMrgn = 1[, $RightMinMrgn = 1[,
;                  $BottomMinMrgn = 1]]]])
; Parameters ....: $hPrintDc            - A handle value. Printer's device context.
;                  $hRichEditCtrl                - A handle value. Handle of the RichEdit control.
;                  $sDocTitle            - An unknown value. Printer's job title.
;                  $LeftMinMrgn         - [optional] An unknown value. Default is 1. Minimum margin on the left
;                  $TopMinMrgn          - [optional] An unknown value. Default is 1. Minimum margin on the Top
;                  $RightMinMrgn        - [optional] An unknown value. Default is 1. Minimum margin on the Right
;                  $BottomMinMrgn       - [optional] An unknown value. Default is 1. Minimum margin on the Bottom
; Return values .: Success -  'Sent to the printer.'
;                  Failure - 'Printing aborted.'
; Author ........: RoyGlanfield
; Modified ......: mLipok
; Remarks .......: Orientation, paper size, number of copies, and which printer are set when the Printer's device context is generated.
;                  Unexpected results may be caused by single line/paragaph orphin tags in the rtf.
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/topic/127580-printing-richedit/
; Example .......: No
; ===============================================================================================================================
Func __RTF_Print($hPrintDc, $hRichEditCtrl, $sDocTitle, $LeftMinMrgn = 1, $TopMinMrgn = 1, $RightMinMrgn = 1, $BottomMinMrgn = 1)

    ; convert the margins 0.1 inches to twips
    ; $TopMinMarg = $TopMinMarg * 144; eg 10*144 = 1440 which 1 inch
    ; $LeftMinMarg = $LeftMinMarg * 144; eg 10*144 = 1440 which 1 inch
    ; $PageMinMarg = $PageMinMarg * 144; eg 10*144 = 1440 which 1 inch
    ; $BottomMinMarg = $BottomMinMarg * 144; eg 10*144 = 1440 which 1 inch

    ; convert the margins 1 cm to twips
    $TopMinMrgn = $TopMinMrgn * 567; eg 2.539cm * 567= ~1440 which 1 inch
    $LeftMinMrgn = $LeftMinMrgn * 567; eg 2.539cm * 567 = ~1440 which 1 inch
    $RightMinMrgn = $RightMinMrgn * 567; eg 2.539cm * 567 = ~1440 which 1 inch
    $BottomMinMrgn = $BottomMinMrgn * 567; eg 2.539cm * 567 = ~1440 which 1 inch

    ; $hPrintDc ; Divice Context handle-----------
    ; dots per inch depends on the printer quality setting-------X and Y can be different!
    Local $dotInchX = _WinAPI_GetDeviceCaps($hPrintDc, 88) ; Const LOGPIXELSX = 88
    Local $dotInchY = _WinAPI_GetDeviceCaps($hPrintDc, 90) ; Const LOGPIXELSY = 90

    ; printer dots per inch
    ; get the printable area  [Page] and paper area [Paper]
    Local $PageW = _WinAPI_GetDeviceCaps($hPrintDc, 8) ; Const HORZRES= 8
    Local $PageH = _WinAPI_GetDeviceCaps($hPrintDc, 10) ; Const VERTRES = 10
    Local $PaperW = _WinAPI_GetDeviceCaps($hPrintDc, 110) ; Const PHYSICALWIDTH = 110
    Local $PaperH = _WinAPI_GetDeviceCaps($hPrintDc, 111) ; Const PHYSICALHEIGHT = 111

    ; none printable margins
    Local $OffSetX = _WinAPI_GetDeviceCaps($hPrintDc, 112) ; Const PHYSICALOFFSETX = 112
    Local $OffSetY = _WinAPI_GetDeviceCaps($hPrintDc, 113) ; Const PHYSICALOFFSETY = 113
    Local $RightMrgn = $PaperW - $PageW - $OffSetX
    Local $BottomMrgn = $PaperH - $PageH - $OffSetY

    ; conversion factors to use later-----------
    Local $TwipsInchX = $dotInchX / 1440 ; convert dots to twips [per inch]
    Local $TwipsInchY = $dotInchY / 1440 ; convert dots to twips [per inch]

    ; convert all measurments to twips
    $OffSetX = $OffSetX / $TwipsInchX ; convert Left dots to twips
    $OffSetY = $OffSetY / $TwipsInchY ; convert Left dots to twips
    $PageW = $PageW / $TwipsInchX ; convert Right dots to twips
    $PageH = $PageH / $TwipsInchY ; convert Right dots to twips
    $PaperW = $PaperW / $TwipsInchX ; convert Paper Width dots to twips
    $PaperH = $PaperH / $TwipsInchY ; convert Paper Width dots to twips

    ; Set the margins and keep everything in the printable area
    Local $Left1 = $LeftMinMrgn - $OffSetX
    If $Left1 < 0 Then $Left1 = 0 ; dont print before printable area starts

    Local $Top1 = $TopMinMrgn - $OffSetY
    If $Top1 < 0 Then $Top1 = 0 ; dont print before printable area starts

    Local $Right1 = $RightMinMrgn - $RightMrgn
    If $Right1 < 0 Then $Right1 = 0 ; dont print after printable area ends

    ; $Right1 = $PaperW - $Right1 - $OffSetX
    $Right1 = $PageW - $Right1 ;+$Left1 ; $OffSetX

    Local $Bottom1 = $BottomMinMrgn - $BottomMrgn
    If $Bottom1 < 0 Then $Bottom1 = 0 ; dont print after printable area ends
    $Bottom1 = $PageH - $Bottom1 ;+$Top1

    Local $z = _SendMessage($hRichEditCtrl, $EM_SETTARGETDEVICE, 0) ; 0=wrap----anything else is 1 char per page!!!!!
    If $z = 0 Then Return 'Cant find RichEdit Control'

    If _GUICtrlRichEdit_GetTextLength($hRichEditCtrl) < 1 Then Return 'Nothing to Print.'

    ; must have a selection on the richEdit control---------------
    _SendMessage($hRichEditCtrl, $EM_SETSEL, 0, -1) ; ok----select all

    Local $pgTags = "int Left1 ;int Top1 ;int Right1 ;int Bottom1 ;int Left2;int Top2;int Right2;int Bottom2;"
    Local $rgTags = "LONG cpMin;LONG cpMax"
    Local $dcHTags = "HANDLE hdc;HANDLE hdcTarget;"

    ; create a structure for the printed page
    Local $strcPg = DllStructCreate($dcHTags & $pgTags & $rgTags)
    DllStructSetData($strcPg, "hdc", $hPrintDc) ; printer
    DllStructSetData($strcPg, "Left1", $Left1) ; twip--------printer
    DllStructSetData($strcPg, "Right1", $Right1) ; twip--------printer
    DllStructSetData($strcPg, "Top1", $Top1) ; twip--------printer
    DllStructSetData($strcPg, "Bottom1", $Bottom1) ; twip--------printer

    ; next 7 lines seem to have, no effect or crash printer jobs queue---why???
    ; "HANDLE hdc;" is the printer------- before conecting printer to rtf???
    ; "HANDLE hdcTarget;" is the target RichEdit control that has the rtf ????
    ; DllStructSetData($strcPg,"hdcTarget",$hPrintDc) ; richEdit scource???
    ; DllStructSetData($strcPg,"Left2",$Left1/$dotInchX*96/3) ; richEdit scource???
    ; DllStructSetData($strcPg,"Top2",$Top1/$dotInchX*96/3) ; richEdit scource???
    ; DllStructSetData($strcPg,"Right2",$Page1/$dotInchX*96/3) ; twip------richEdit scource???
    ; DllStructSetData($strcPg,"Bottom2",$Bottom1/$dotInchX*96/3) ; twip-----richEdit scource???

    ; get the pointer to all that will be printed??? {I think????????}
    Local $a = DllStructGetPtr($strcPg) + DllStructGetSize(DllStructCreate($dcHTags & $pgTags))

    ; use this pointer-----------
    _SendMessage($hRichEditCtrl, $EM_EXGETSEL, 0, $a)

    ; find the last char of the document to be printed
    Local $cpMax = DllStructGetData($strcPg, "cpMax")

    ; set the 1st page start char-----------
    Local $cpMin = 0
    Local $cpMin2 = -1

    ; -----------------------------------------------------------
    ; create a Document structure for the print job title
    Local $strDocNm = DllStructCreate("char DocName[" & StringLen($sDocTitle & Chr(0)) & "]")
    DllStructSetData($strDocNm, "DocName", $sDocTitle & Chr(0))
    Local $strDoc = DllStructCreate("int Size;ptr DocName;ptr Output;ptr Datatype;dword Type")
    DllStructSetData($strDoc, "Size", DllStructGetSize($strDoc))

    ; insert the document name structure into the document structure
    DllStructSetData($strDoc, "DocName", DllStructGetPtr($strDocNm))
    DllCall("gdi32.dll", "long", "StartDoc", "hwnd", $hPrintDc, "ptr", DllStructGetPtr($strDoc))

    ; -----------------------------------------------------------
    ; make a loop to format each printed page and exit when
    ; $cpMin reaches the $cpMax
    ; $cpMin is less than 0---{I have seen -1 but I forget when}---
    ; ALSO-- ExitLoop if $cpMin = $cpMin 2---if it stops finding the start of next page
    Local $StartPage, $EndPage
    #forceref $StartPage
    While $cpMax > $cpMin And $cpMin > -1

        ; start a new page-----------
        $StartPage = DllCall("Gdi32.dll", "int", "StartPage", "HANDLE", $hPrintDc)

        ; increment page the count-----------
        ; if not done now it will exit the loop before counting the last page
        ; get the 1st char of the next page, {but how does it work ???}
        $cpMin2 = $cpMin
        $cpMin = _SendMessage($hRichEditCtrl, $EM_FORMATRANGE, True, DllStructGetPtr($strcPg))

        ; ExitLoop when $cpMin = $cpMin 2---just in case it stops finding the start of next page
        If $cpMin2 = $cpMin Then ExitLoop; get out of loop before more pages are added

        ; set the next page start char-----------
        DllStructSetData($strcPg, "cpMin", $cpMin)

        ; this sends it to the printer
        $EndPage = DllCall("Gdi32.dll", "int", "EndPage", "HANDLE", $hPrintDc)

        ; end the page and loop again for the next page until the end of document
    WEnd
    _SendMessage($hRichEditCtrl, $EM_FORMATRANGE, False, 0)
    If $EndPage[0] > 0 Then
        DllCall("Gdi32.dll", "int", "EndDoc", "HANDLE", $hPrintDc)
        Return 'Sent to the printer.'
    Else
        DllCall("Gdi32.dll", "int", "AbortDoc", "HANDLE", $hPrintDc)
        Return 'Printing aborted.'
    EndIf
EndFunc   ;==>__RTF_Print
#EndRegion RTF PRINTER INTERNAL#

Example Print RTF

#include-once
#include <RTF_Printer.au3>

Example()

Func Example()
    Local $sRTF_FileFullPath = @ScriptDir & "\Test.rtf"
    Local $sScriptContent = FileRead(@ScriptFullPath)
    For $i = 1 To 3
        ; Create RTF Test File

        RTFExampleFile($sRTF_FileFullPath, 'Testing RTF_Printer.au3.' & @CRLF & 'Test #' & $i & @CRLF& @CRLF & $sScriptContent)

        ; Print RTF Test File
        _RTF_PrintFile($sRTF_FileFullPath, Default, True, True)

    Next

    ; CleanUp
    FileDelete($sRTF_FileFullPath)

EndFunc   ;==>Example

Func RTFExampleFile($sRTF_FileFullPath, $sTextToRTF)

    Local $hGui, $hRichEdit, $iMsg

    ; make a temp Gui
    $hGui = GUICreate("Example (" & StringTrimRight(@ScriptName, 4) & ")", 320, 350, -1, -1)

    ; create a RTF control
    $hRichEdit = _GUICtrlRichEdit_Create($hGui, "This is a test.", 10, 10, 300, 220, _
            BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))

    ; add text to RTF control
    _GUICtrlRichEdit_AppendText($hRichEdit, $sTextToRTF)

    ; Save to File
    FileDelete($sRTF_FileFullPath)
    _GUICtrlRichEdit_StreamToFile($hRichEdit, $sRTF_FileFullPath)

    ; CleanUp
    _GUICtrlRichEdit_Destroy($hRichEdit) ; needed unless script crashes
    GUIDelete($hGui)

EndFunc   ;==>RTFExampleFile

 

Also you can ask @argumentum to update the PrintPreview UDF to the latest version of AutoIT ?

 

 

I modified it for the version 3.3.14 ...

Enjoy !

 

 

PrintPreview for v3.3.14.zip

Edited by ptrex
Link to comment
Share on other sites

1 hour ago, ptrex said:

Also you can ask @argumentum to update the PrintPreview UDF to the latest version of AutoIT ?

hmmm, me work ?. Hmmm. 

Anyways, I just made available the file from https://www.autoitscript.com/forum/topic/113582-print-preview/ 
I did not write a single line of code and actually never even got to use it. 

PS: got to run it and needs some rearranging of constants. @ptrex, since you brought it up, you are aware of it's state. If you have in hand an updated working copy I'll add the files to it ?  [yes][no][maybe] :) 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

10 hours ago, ptrex said:

Best test it ...

The x64 on print would not show the print gui
Neither x64 nor x86 have the print preview
Since the original "shrink to gui" did not work or had x64 support.
Test printed and failed. Printed nothing. Not the updated nor the original for v3.3.14.2 . The 3.3.8.1 version did print as expected.

These are my findings in "Win10 / 2004".

I've never used this before other than hording files and post them back when needed.

Good luck Jim ?, I mean, it needs to be upgraded from 3.3.8.x ( that works ), and not 3.3.14.x .
 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...