Printing Multiple Images (Delphi 6.0)

Take the following steps to add code that prints two images on one page. This code shows you how to control the size and position of images on the page.

1.

Start with the project that you created in Loading and Displaying an Image.

2.

Add a button control to your form and name it as follows:

 

Name

Caption

 

btnPrint

Print Twice on One Page

3.

On the Project pull-down menu, use the Import Type library… and select the LEAD Raster Process object library (14.5).

4.

Select LEAD Raster Process from the ActiveX tab and add it to your form.

5.

Add the following code to the btnPrint control's Click procedure. In online help, you can use the Edit pull-down menu to copy the block of code.

procedure TForm1.btnPrintClick(Sender: TObject); 
var
   //Declare the variable for printed text
   Msg: String; 
   //Declare the variables for pixel measurements
   TextHeightInPixels: Integer; 
   UsableWidth: Integer; 
   UsableHeight: Integer; 
   MaxImageHeight: Integer; 
   //Declare the variables for sizing and positioning the image
   PrintLeft: longint; 
   PrintTop: longint; 
   PrintHeight: longint; 
   PrintWidth: longint; 
   //Declare variables used for preserving aspect ratios
   WidthFactor: Integer; 
   HeightFactor: Integer; 
begin
   //Set the variables used for preserving the aspect ratio
   HeightFactor := Trunc (LEADRasterView1.Raster.BitmapHeight); 
   WidthFactor:= Trunc (LEADRasterView1.Raster.BitmapWidth); 

   //Set the pointer to an hourglass
   Screen.Cursor := crHourGlass; 
   //Get the page width and height in pixels (dots) 
   UsableWidth := Printer.PageWidth ; 
   UsableHeight := Printer.PageHeight ; 
   //Print a title at the top of the page
   Msg:= 'This is a print example using LEADTOOLS ActiveX.'; 
   Printer.BeginDoc ( ); 
   Printer.Canvas.TextOut ( Printer.Canvas.PenPos.x, Printer.Canvas.PenPos.y, Msg ) ; 
   //Get the maximum height of one image, 
   //assuming two equal-size images and space for 12 lines of text
   TextHeightInPixels := Printer.Canvas.TextHeight (Msg); 
   MaxImageHeight := Trunc ( (UsableHeight - (12 * TextHeightInPixels)) / 2); 
   //Size and position the first image, preserving the aspect ratio. 
   //Check to see if using the maximum width will make the image too tall. 
   //Set the dimensions based on the result. 
   if (((UsableWidth * HeightFactor) / WidthFactor) < MaxImageHeight) then
   begin
      PrintLeft := 1; 
      PrintTop := TextHeightInPixels + Printer.Canvas.PenPos.y; 
      PrintWidth := UsableWidth; 
      PrintHeight := Trunc ((PrintWidth * HeightFactor) / WidthFactor); 
   end
   else
   begin
      PrintLeft := 1; 
      PrintTop := TextHeightInPixels + Printer.Canvas.PenPos.y ; 
      PrintHeight := MaxImageHeight; 
      PrintWidth := Trunc ( (PrintHeight * WidthFactor) / HeightFactor); 
   end; 
   //Print the first image
   LEADRasterView1.Render ( Printer.Handle, PrintLeft, PrintTop, PrintWidth, PrintHeight) ; 
   //Update the current printing position
   Printer.Canvas.MoveTo ( 1, Printer.Canvas.PenPos.y + PrintHeight + TextHeightInPixels ); 
   //Print a blank line, then print a caption for the next picture
   Msg := ' ' ; 
   Printer.Canvas.TextOut ( Printer.Canvas.PenPos.x, Printer.Canvas.PenPos.y, Msg ) ; 
   Msg := 'This is the second picture, which is modified before printing:'; 
   Printer.Canvas.TextOut ( Printer.Canvas.PenPos.x, Printer.Canvas.PenPos.y + TextHeightInPixels, Msg ) ; 
   //Stretch the intensity of the bitmap for the second printing
   //to show the difference between the second picture and the first
   LEADRasterProcess1.StretchIntensity( LEADRasterView1.Raster ) ; 
   //Size and position the second image, preserving the aspect ratio. 
   //The coding is the same as for the first image. 
   if ((UsableWidth * HeightFactor) / WidthFactor) < MaxImageHeight Then
   begin
      PrintLeft := 1; //This is for flush left. Use 0 for centering. 
      PrintTop := TextHeightInPixels + Printer.Canvas.PenPos.y ; 
      PrintWidth := UsableWidth ; 
      PrintHeight := Trunc((PrintWidth * HeightFactor) / WidthFactor); 
   end
   else
   begin
      PrintLeft := 1 ;//This is for flush left. Use 0 for centering. 
      PrintTop := TextHeightInPixels + Printer.Canvas.PenPos.y ; 
      PrintHeight := MaxImageHeight; 
      PrintWidth := Trunc((PrintHeight * WidthFactor) / HeightFactor); 
   end; 
    //Print the second image
   LEADRasterView1.Render ( Printer.Handle, PrintLeft, PrintTop, PrintWidth, PrintHeight ) ; 
   //Finish the page and finish the print job
   Printer.NewPage ( ) ; 
   Printer.EndDoc ( ) ; 
   //Set the mouse pointer back to the default
   Screen.Cursor := crDefault; 
end; 

6.

Add "Printers" unit to the uses section.

7.

Run your program to test it.