2007. január 28., vasárnap

Print an HTML file using TWebBrowser


Problem/Question/Abstract:

How to print an HTML file using TWebBrowser

Answer:

Solve  1:

var
  I, O: OleVariant;
begin
  I := 0;
  WebBrowser1.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, I, O);
end;


Solve  2:

uses
  ActiveX

procedure PrintWebBrowser(WB: TWebBrowser);
var
  OleCommandTarget: IOleCommandTarget;
  Command: TOleCmd;
  Success: HResult;
begin
  if not Assigned(WB) then
    Exit;
  if not Assigned(WB.Document) then
    Exit;
  {Get reference to IOleCommandTarget}
  WB.Document.QueryInterface(IOleCommandTarget, OleCommandTarget);
  {Check if printing is currently possible}
  Command.cmdID := OLECMDID_PRINT;
  if OleCommandTarget.QueryStatus(nil, 1, @Command, nil) <> S_OK then
  begin
    {Something went wrong ...}
    Exit;
  end;
  if (Command.cmdf and OLECMDF_ENABLED) <> 0 then
  begin
    {Print}
    Success := OleCommandTarget.Exec(nil, OLECMDID_PRINT,
      OLECMDEXECOPT_DONTPROMPTUSER, EmptyParam, EmptyParam);
    case Success of
      S_OK: ; {Everything's fine}
      OLECMDERR_E_CANCELED: ShowMessage('Aborted by user');
    else
      ShowMessage('Error');
    end;
  end
  else
  begin
    {Printing not possible}
  end;
end;


Solve 3:

After navigating to a page with TWebBrowser you may want to print it. Well, the Microsoft Internet Explorer control can do that, show a print preview dialog, and even a page setup dialog.

var
  vaIn, vaOut: OleVariant; // Needed in all examples
{ ...}
// Printing without the Printer dialog
WebBrowser1.ControlInterface.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,
  vaIn, vaOut);
// Print with the Printer dialog
WebBrowser1.ControlInterface.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER,
  vaIn, vaOut);
// Show the Print Preview dialog
WebBrowser1.ControlInterface.ExecWB(OLECMDID_PRINTPREVIEW,
  OLECMDEXECOPT_DONTPROMPTUSER, vaIn, vaOut);
// Show the Page Setup dialog
WebBrowser1.ControlInterface.ExecWB(OLECMDID_PAGESETUP, OLECMDEXECOPT_PROMPTUSER,
  vaIn, vaOut);

Nincsenek megjegyzések:

Megjegyzés küldése