Solved

TWebBrowser and Print - Page Setup

Posted on 2002-05-21
5
2,253 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi TcxGrid group footer summary 3 316
Newbie Thread Programming 1 161
JAudiorecorder record freezing the app 29 74
Convert MS Word document to a PDF file 9 90
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 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