TAdvStringGrid

Example 9 : Using bitmaps in the header when printing

vcl grid printing

Adding a company logo or any other graphic to every page header (this applies to footers as well) is a fairly easy task. This example shows how to use a bitmap in the header to print reports. The most important issue is to control the size of the bitmap on the output device, since the resolution is device dependent. This will be discussed further. To print any additional information to the data in the grid, you can use the PrintPage event. This event is called for every page printed. Additional properties are available to help placement of images and text with respect to the columns that will be printed automatically by the TAdvStringGrid. These properties are:

PrintPageWidth:integer;
Width in pixels of the paper

PrintColWidth[aCol]:integer;
Width in pixels of column aCol

PrintColOffset[aCol]:integer
Start position in pixels of column aCol

PrintColStart:integer;
Position from left in pixels of the first column left border

PrintColEnd:integer;
Position from left in pixels of the last column right border

With these things in mind, the following code is used to print the bitmap and text in the header and draw a line above the printed columns as well:

procedure TForm1.AdvStringGrid1PrintPage(Sender: TObject; Canvas: TCanvas;
  pagenr, pagexsize, pageysize: Integer);
var
  bmp: TBitmap;
  r: TRect;
  ratio: double;
begin
  bmp := TBitmap.create;
  bmp.LoadFromFile('athena.bmp');

  ratio := bmp.width/bmp.height;

  r.Left := AdvStringGrid1.printcoloffset[1];
  r.top := -0;
  r.right := r.left+round(advstringgrid1.printsettings.headersize*ratio);
  r.bottom := r.top-advstringgrid1.printsettings.headersize;

  Canvas.StretchDraw(r,bmp);
  bmp.free;

  r.left := r.right;
  r.top := 0;
  Canvas.Textout(r.left,r.top,'Printed with TAdvStringGrid');
  r.top := r.top-canvas.textheight('gh');
  Canvas.Textout(r.left,r.top,'showing how to add a bitmap in the header');

  r.left := advstringgrid1.printcoloffset[1];
  r.right := advstringgrid1.printcoloffset[8];
  r.top := -advstringgrid1.printsettings.HeaderSize+2;
  Canvas.MoveTo(r.left,r.top);
  Canvas.LineTo(r.right,r.top);
end;


First of all, the bitmap is created and loaded from a file. You can use a resource bitmap here as well (Use the LoadFromResourceName or LoadFromResourceID method instead of LoadFromFile). To print the bitmap within the header, the bitmap will be resized (keeping the aspect ratio!) to fit its height into the headersize. This height is set in the printsettings.headersize property. Notice that the printing is in MM_LOMETRIC mode, meaning a unit is 0.1mm and the vertical coordinates start at 0 and go down negative! The bitmap is drawn to the full headersize with the StretchDraw method and the width is resized with property aspect ratio. The left position of the bitmap is aligned to the first normal grid cell by using the PrintColOffset[1] property. As a last additional feature, a line is drawn here above the grid cells that contain data. Therefore, the start positions of these columns are used : printcoloffset[1] and printcoloffset[8]. Given this information, it should be easy to fully customize your headers and footers for printing with text as well as graphics.

Delphi project & source files for downloading included in the main demos distribution for Delphi.