Solved

TWebBrowser and Print - Page Setup

Posted on 2002-05-21
5
2,192 Views
Last Modified: 2007-11-27
Could someone please tell me how to set the page settings - Header, Footer, Orientation and margins when I'm printing from TWebBrowser.
I'm printing using .. ControlInterface.ExecWB, and I beleive I've seen this used for PageSetup also.

DR
0
Comment
Question by:DelRunner
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:ginsonic
ID: 7026154
listening
0
 
LVL 17

Accepted Solution

by:
inthe earned 200 total points
ID: 7026246
hi,
you can using embeddedwb as its interface is setup to do it,i never seen code for normal twebbrowser to do it.:
http://www.euromind.com/IEDELPHI/embeddedwb.htm
(way superior version of TWebbrowser)

below is taken from tips /faq on embededwb:
Regards Barry

4. Set Print-Options programmatically. (Version 1)

The following sample shows an easy way to programmatically set options for print (number of copies, paper orientation, header, footer, margins etc.). The program opens the Pagesetup and Print dialogs, makes the modifications and close them again before they become visible. You can use the functions EnumChildWindows and GetCtrlId to get the ControlID for other options you want to set programmatically.

 

Add to private section in your main form:
procedure WMActivate(var Msg: TWMActivate); message WM_ACTIVATE;


type

TPrintOptions = record
  Copies: Integer;    //Number of copies
  Portrait: Boolean;  //Paper orientation
  Left: string;         //Left Margin
  Top: string;         //Top Margin
  Right: string;       //Right Margin
  Bottom: string;    //Bottom Margin
  Header: string;    //Header string
  Footer: string;    //Footer string
end;

var
PrintOptions: TPrintOptions;
PrintWithOptions: Boolean;

procedure TForm1.WMActivate(var Msg: TWMActivate);
var
  S: string;
  h, wnd: HWND;
  Item, I: Integer;
begin
if PrintWithOptions and (msg.active = 0) then begin
  PrintWithOptions := false;
  wnd := Msg.ActiveWindow;
  I := GetWindowTextLength(wnd);
  SetLength(S, I + 1);
  GetWindowText(Wnd, PChar(S), I + 1);
  if S = 'Print'#0 then begin
     SetDlgItemInt(wnd, 1154, PrintOptions.Copies, FALSE);
    SendDlgItemMessage(Wnd, 1, BM_CLICK, 0, 0);
    PrintWithOptions := true;
    end else
  if S = 'Page Setup'#0 then begin
    if PrintOptions.Portrait then
    SendDlgItemMessage(Wnd, 1056, BM_CLICK, 0, 0)
    else
    SendDlgItemMessage(Wnd, 1057, BM_CLICK, 0, 0);
    SetDlgItemText(wnd, 8147, PChar(PrintOptions.Header));
    SetDlgItemText(wnd, 8149, PChar(PrintOptions.Footer));
    SetDlgItemText(wnd, 1155, PChar(PrintOptions.Left));
    SetDlgItemText(wnd, 1156, PChar(PrintOptions.Top));
    SetDlgItemText(wnd, 1157, PChar(PrintOptions.Right));
    SetDlgItemText(wnd, 1158, PChar(PrintOptions.Bottom));
    SendDlgItemMessage(Wnd, 1, BM_CLICK, 0, 0);
    PrintWithOptions := true;
  end;
end;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
if Assigned(EmbeddedWB1.Document) then begin
  with PrintOptions do begin
     Copies := 2;
    Portrait := FALSE;
    Footer := 'This is my Footer';
    Header := 'This is my Header';
    Left := '25,0';
    Top := '20,0';
    Right := '22,0';
    Bottom := '19,7';
  end;
PrintWithOptions := True;
Embeddedwb1.pagesetup;
embeddedwb1.printsetup;
PrintWithOptions := False;
end else showmessage('No Document to print!');
end;


 

 

5. Set Print-Options programmatically. (Version 2)



You can specify extended printing information by passing in the SAFEARRAY structure through the VARIANT argument vaIn, when you use ExecWB and OLECMDID_PRINT. This SAFEARRAY data type takes a maximum of five items:
A string (BSTR) that contains a custom header.

A string (BSTR) that contains a custom footer.

An IStream object that contains an HTML file that serves as an "optional header." This is the e-mail header that you see in Microsoft Outlook and Microsoft Outlook Express e-mail messages when you print them. This IStream must point to a full, valid HTML document, not HTML fragments, or it will print incorrectly.

An alternative URL to use for the document. This is only relevant to Outlook and Outlook Express.

A set of printing flags (dwFlags) to configure the printer. This is only relevant to Outlook and Outlook Express.

All versions of Internet Explorer, from version 4.0 onward, support the use of the IWebBrowser2::ExecWB method to customize headers and footers:

procedure TForm1.PrintHeaderFooter(Header, Footer, HTMLHeader: string);
var
  CmdTarget: IOleCommandTarget;
  Stream: IStream;
  v, v1: OleVariant;
  Dummy: Int64;
  Psa: PSafeArray;
begin
  CreateStreamOnHGlobal(0, TRUE, Stream);
  Stream.Write(Pchar(HTMLHeader), length(HTMLHeader), @Dummy);
  Stream.Seek(0, STREAM_SEEK_SET, Dummy);
  SafeArrayCopy(PSafeArray(TVarData(VarArrayOf([Header, Footer, Stream as IUnknown])).VArray), psa);
  TVarData(V1).VType := varArray or varByRef;
  SafeArrayCopy(psa, PSafeArray(TVarData(V1).VArray));
  if Webbrowser1.Document <> nil then
  try
    Webbrowser1.Document.QueryInterface(IOleCommandTarget, CmdTarget);
    if CmdTarget <> nil then
    try
      CmdTarget.Exec(nil, OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, v1, v);
    finally
      CmdTarget := nil;
    end;
  except end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  HTMLHeader: string;
begin
  HTMLHeader := '<HTML><BODY><FONT SIZE=4><B><CENTER>IE & DELPHI</CENTER></B></FONT><HR></BODY></HTML>';
  PrintHeaderFooter('My Header', 'My Footer', HTMLHeader);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  webbrowser1.navigate('about:blank');
end;

     
 

More Info: HOWTO: Print Custom Headers and Footers for a WebBrowser Control
http://support.microsoft.com/support/kb/articles/Q267/2/40.ASP
0
 
LVL 1

Author Comment

by:DelRunner
ID: 7035560
I've tried the suggestions, but without luck.
I'm getting errors when I try to compile the embeddedwb, complaints in the MSHTML_tlb.
I was hoping more on using the ExecWB.
Here I'm getting errors on the variable definitions:
IStreab, PSafeArray..

Any suggestions ?
I increase a qualified answer with further 100 p.

DR
0
 
LVL 1

Author Comment

by:DelRunner
ID: 7039008
Hello.....!
Nearly there. I've got the EmbeddedWB control working, but I forgot to mention that this should be for both NT4 and Win2000.
Do you know if the code is compatible for both ?

DR.
0
 
LVL 17

Expert Comment

by:inthe
ID: 7054824
>>EmbeddedWB control working

glad to hear ,it will save you alot of headaches believe me :)

well i know it works fine on win2000 , i really cant honestly say for nt4 but i dont see any problems ,well i can say its more dependant on ie than OS.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now