Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Set printer properties for a TWebBrowser


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 1 - Last Post: Feb 14, 2017 4:11 AM Last Post By: Mikael Lenfors Threads: [ Previous | Next ]
Mikael Lenfors

Posts: 99
Registered: 3/6/01
Set printer properties for a TWebBrowser  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 9, 2017 6:41 AM
Hello!

I use a TWebBrowser for displaying PDF documents in my application.
I also have a setup in my program where the user can select a "Application" printer, tray orientation and papersize.
These settings work for all my reports where I use QuickReport, but how can I "inject" these settings in the TWebBrowser.

I want the TWebBrowser to be preset with printer, tray, orientation and papersize so that when the user prints from e.g. Acrobat reader inside the TWebBrower these settings are used as default.

Best regards, Mikael
Mikael Lenfors

Posts: 99
Registered: 3/6/01
Re: Set printer properties for a TWebBrowser  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 14, 2017 4:11 AM   in response to: Mikael Lenfors in response to: Mikael Lenfors
I found a solution!
I found the code below somewhere and modified it a Little.
If I call this procedure in the OnCreate event of my form containg the TWebBrowser then the AcrobatRead embedded in the browser will use the settings I supply here.
Only negative effect, you have to reset the windows settings with a new call to this procedure after use, otherwise your windows default printer settings are changed.

Hope this helps someone...

Best regards, Mikael

Uses WinApi.WinSpool, Printers

Procedure SetBrowserPrinter(PrinterIndex, Orientation, SourceTray, Duplex, PrinterSize : SmallInt);
Var hPrinter: THandle;
Device : array[0..255] of char;
Driver : array[0..255] of char;
Port : array[0..255] of char;
hDeviceMode: THandle;
bytesNeeded: Cardinal;
pPI: PPrinterInfo2;
retVal: Boolean;
Begin
Printer.PrinterIndex := PrinterIndex;
// Gets the printer assigned to the printer object
Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
// Open the Printer object's printer as Windows printer
If WinAPI.WinSpool.OpenPrinter(Device, hPrinter, nil) Then
Begin
Try
retVal := WinAPI.WinSpool.GetPrinter(hPrinter, 2, nil, 0, @bytesNeeded);
If Not retval Then
Begin
GetMem(pPI, bytesNeeded);
Try
retval := WinAPI.WinSpool.GetPrinter(hPrinter, 2, pPI, bytesNeeded, @bytesNeeded);
If retval Then
Begin
// Sets the changes from the system printer to default printer
With pPI^.pDevMode^ Do
Begin
dmFields := dmFields;
dmOrientation := Orientation;
// dmCopies :=
// dmCollate :=
// dmColor :=
dmDefaultSource := SourceTray;
// dmDitherType :=
dmDuplex := Duplex;
// dmICMIntent :=
// dmICMMethod :=
// dmMediaType :=
// dmPaperLength :=
dmPaperSize := PrinterSize;
// dmPaperWidth :=
// dmPrintQuality :=
// dmScale :=
// dmTTOption :=
// dmYResolution :=
End;
// Set the changes to the Windows printer
If WinAPI.WinSpool.SetPrinter(hPrinter, 2, pPI, 0) Then
Begin
ShowMessage('Error! (' + SysErrormessage(GetLastError) + ')');
End;
End
Else
Begin
ShowMessage('Error! (' + SysErrormessage(GetLastError) + ')');
End;
Finally
FreeMem(pPI);
End;
End
Else
Begin
ShowMessage('Error! (' + SysErrormessage(GetLastError) + ')');
End;
Finally
WinAPI.WinSpool.ClosePrinter(hPrinter);
End;
End
Else
Begin
ShowMessage('Error! (' + SysErrormessage(GetLastError) + ')');
End;
End;

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02