RoyGlanfield Posted April 14, 2011 Posted April 14, 2011 I have been experimenting with printing RichEdit formatted text. I use AutoIt script to produce a preview and send it to the chosen printer. Everything has been tested on an XP computer with an Epson printer. expandcollapse popup;============================================================================================== ;--Name --------- _RTF_Print ;--Description --- Print from RichEdit control ;--Parameters -----$hPrintDc = Printer's device context ;-------------------$hwnd = handle of the RichEdit control ;-------------------$DocTitle = printer's job title ;-------------------$LeftMinMrgn = Minimum margin on the left ;-------------------$TopMinMrgn = Minimum margin on the Top ;-------------------$RightMinMrgn = Minimum margin on the Right ;-------------------$BottomMinMrgn = Minimum margin on the Bottom ;--Return values -- Success - 'Sent to the printer.' ;-------------------Failure - 'Printing aborted.' ;--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 ;--Author -------- RoyGlanfield ;============================================================================================== Func _RTF_Print($hPrintDc, $hwnd, $DocTitle, $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! $dotInchX = _WinAPI_GetDeviceCaps($hPrintDc, 88);-----Const LOGPIXELSX = 88 $dotInchY = _WinAPI_GetDeviceCaps($hPrintDc, 90);-----Const LOGPIXELSY = 90 ;----printer dots per inch ;--------get the printable area [Page] and paper area [Paper] $PageW = _WinAPI_GetDeviceCaps($hPrintDc, 8);-----Const HORZRES= 8 $PageH = _WinAPI_GetDeviceCaps($hPrintDc, 10);-----Const VERTRES = 10 $PaperW = _WinAPI_GetDeviceCaps($hPrintDc, 110) ;-----Const PHYSICALWIDTH = 110 $PaperH = _WinAPI_GetDeviceCaps($hPrintDc, 111) ;-----Const PHYSICALHEIGHT = 111 ;----none printable margins $OffSetX = _WinAPI_GetDeviceCaps($hPrintDc, 112) ;-----Const PHYSICALOFFSETX = 112 $OffSetY = _WinAPI_GetDeviceCaps($hPrintDc, 113);-----Const PHYSICALOFFSETY = 113 $RightMrgn = $PaperW - $PageW - $OffSetX $BottomMrgn = $PaperH - $PageH - $OffSetY ;----conversion factors to use later----------- $TwipsInchX = $dotInchX / 1440;-----convert dots to twips [per inch] $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 $Left1 = $LeftMinMrgn - $OffSetX If $Left1 < 0 Then $Left1 = 0;-----dont print before printable area starts $Top1 = $TopMinMrgn - $OffSetY If $Top1 < 0 Then $Top1 = 0;-----dont print before printable area starts $Right1 = $RightMinMrgn - $RightMrgn If $Right1 < 0 Then $Right1 = 0;-----dont print after printable area ends ;$Right1 = $PaperW - $Right1 - $OffSetX $Right1 = $PageW - $Right1 ;+$Left1;- $OffSetX $Bottom1 = $BottomMinMrgn - $BottomMrgn If $Bottom1 < 0 Then $Bottom1 = 0;-----dont print after printable area ends $Bottom1 = $PageH - $Bottom1;+$Top1 $z = _SendMessage($hwnd, $EM_SETTARGETDEVICE, 0);---0=wrap----anything else is 1 char per page!!!!! If $z = 0 Then Return 'Cant find RichEdit Control' If _GUICtrlRichEdit_GetTextLength($hwnd) < 1 Then Return 'Nothing to Print.' ;---------must have a selection on the richEdit control--------------- _SendMessage($hwnd, $EM_SETSEL, 0, -1);--ok----select all $pgTags = "int Left1;int Top1;int Right1;int Bottom1;int Left2;int Top2;int Right2;int Bottom2;" $rgTags = "LONG cpMin;LONG cpMax" $dcHTags = "HANDLE hdc;HANDLE hdcTarget;" ;------------create a structure for the printed page $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????????} $a = DllStructGetPtr($strcPg) + DllStructGetSize(DllStructCreate($dcHTags & $pgTags)) ;-----------use this pointer----------- _SendMessage($hwnd, $EM_EXGETSEL, 0, $a) ;-----------find the last char of the document to be printed $cpMax = DllStructGetData($strcPg, "cpMax") ;-----------set the 1st page start char----------- $cpMin = 0 $cpMin2 = -1 ;----------------------------------------------------------------------------------- ;--------create a Document structure for the print job title $strDocNm = DllStructCreate("char DocName[" & StringLen($DocTitle & Chr(0)) & "]") DllStructSetData($strDocNm, "DocName", $DocTitle & Chr(0)) $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 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($hwnd, $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($hwnd, $EM_FORMATRANGE, False, 0) If $EndPage[0] > 0 Then $r = 'Sent to the printer.' DllCall("Gdi32.dll", "int", "EndDoc", "HANDLE", $hPrintDc) Else $r = 'Printing aborted.' DllCall("Gdi32.dll", "int", "AbortDoc", "HANDLE", $hPrintDc) EndIf Return $r EndFunc ;==>_RTF_Print ;============================================================================================== Please see the zip file for a working example and a test .rtf file. I think there should be a better way of producing the preview. I would like your comments.ToAutoIT.zip mLipok 1
taietel Posted April 14, 2011 Posted April 14, 2011 (edited) Thanks for sharing, RoyGlanfield. I've tested on Windows7/x86 with no problems! taietel [edit] printers: FoxitPDF, Lexmark Z600 and 2600 series. [edit again] Tried PrintNow directly and the application crashes. If I set printer, pages etc. first, it's ok. [edit again] Solved: add this line: $dcc =_GetDC_PrinterNoDialog() just before line 170. Edited April 14, 2011 by taietel Things you should know first...In the beginning there was only ONE! And zero... Progs: Create PDF(TXT2PDF,IMG2PDF) 3D Bar Graph DeskGadget Menu INI Photo Mosaic 3D Text
mLipok Posted June 2, 2014 Posted June 2, 2014 (edited) Awesome. It works on AutoIt 3.3.10.2 EDIT: Look at my modification "RTF Printer" Edited May 26, 2017 by mLipok Link was broken Signature beginning:* Please remember: "AutoIt"..... * Wondering who uses AutoIt and what it can be used for ? * Forum Rules ** ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Code * for other useful stuff click the following button: Spoiler Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API * ErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 * My contribution to others projects or UDF based on others projects: * _sql.au3 UDF * POP3.au3 UDF * RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF * SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane * Useful links: * Forum Rules * Forum etiquette * Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * Wiki: * Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX IE Related: * How to use IE.au3 UDF with AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler * IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related: * How to get reference to PDF object embeded in IE * IE on Windows 11 * I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions * EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *I also encourage you to check awesome @trancexx code: * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuff * OnHungApp handler * Avoid "AutoIt Error" message box in unknown errors * HTML editor * winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/ "Homo sum; humani nil a me alienum puto" - Publius Terentius Afer"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming" , be and \\//_. Anticipating Errors : "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty." Signature last update: 2023-04-24
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now