Printing Multiple Images (C++ Builder 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 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 install the LEAD Raster Process object library (14.5). Press OK.

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.

void __fastcall TForm1::btnPrintClick(TObject *Sender) 
{
   TPrinter * Prntr = Printer();

   //Declare the variable for printed text
   AnsiString Msg;
   //Declare the variables for pixel measurements
   int TextHeightInPixels;
   int UsableWidth;
   int UsableHeight;
   int MaxImageHeight;
   //Declare the variables for sizing and positioning the image
   long int PrintLeft;
   long int PrintTop;
   long int PrintHeight;
   long int PrintWidth;
   //Declare variables used for preserving aspect ratios
   int WidthFactor;
   int HeightFactor;

   //Set the variables used for preserving the aspect ratio
   HeightFactor = LEADRasterView1->Raster->BitmapHeight;
   WidthFactor= LEADRasterView1->Raster->BitmapWidth;

   //Set the pointer to an hourglass
   Screen->Cursor = crHourGlass;
   //Get the page width and height in pixels (dots)
   UsableWidth = Prntr->PageWidth ;
   UsableHeight = Prntr->PageHeight ;
   //Print a title at the top of the page
   Msg= "This is a print example using LEADTOOLS ActiveX->";
   Prntr->BeginDoc ( );
   Prntr->Canvas->TextOut ( Prntr->Canvas->PenPos.x, Prntr->Canvas->PenPos.y, Msg);
   //Get the maximum height of one image,
   //assuming two equal-size images and space for 12 lines of text
   TextHeightInPixels = Prntr->Canvas->TextHeight (Msg);
   MaxImageHeight = (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)
   {
      PrintLeft = 1;
      PrintTop = TextHeightInPixels + Prntr->Canvas->PenPos.y;
      PrintWidth = UsableWidth;
      PrintHeight = PrintWidth * HeightFactor / WidthFactor;
   }
   else
   {
      PrintLeft = 1;
      PrintTop = TextHeightInPixels + Prntr->Canvas->PenPos.y ;
      PrintHeight = MaxImageHeight;
      PrintWidth = (PrintHeight * WidthFactor) / HeightFactor;
   }
   //Print the first image
   LEADRasterView1->Render ((long)Prntr->Handle, PrintLeft, PrintTop, PrintWidth, PrintHeight);
   //Update the current printing position
   Prntr->Canvas->MoveTo ( 1, Prntr->Canvas->PenPos.y + PrintHeight + TextHeightInPixels );
   //Print a blank line, then print a caption for the next picture
   Msg = " " ;
   Prntr->Canvas->TextOut ( Prntr->Canvas->PenPos.x, Prntr->Canvas->PenPos.y, Msg ) ;
   Msg = "This is the second picture, which is modified before printing:";
   Prntr->Canvas->TextOut ( Prntr->Canvas->PenPos.x, Prntr->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)
   {
      PrintLeft = 1; //This is for flush left-> Use 0 for centering
      PrintTop = TextHeightInPixels + Prntr->Canvas->PenPos.y ;
      PrintWidth = UsableWidth ;
      PrintHeight = (PrintWidth * HeightFactor) / WidthFactor;
   }
   else
   {
      PrintLeft = 1 ;//This is for flush left-> Use 0 for centering
      PrintTop = TextHeightInPixels + Prntr->Canvas->PenPos.y ;
      PrintHeight = MaxImageHeight;
      PrintWidth = (PrintHeight * WidthFactor) / HeightFactor;
   }
    //Print the second image
   LEADRasterView1->Render ((long)Prntr->Handle, PrintLeft, PrintTop, PrintWidth, PrintHeight);
   //Finish the page and finish the print job
   Prntr->NewPage ( ) ;
   Prntr->EndDoc ( ) ;
   //Set the mouse pointer back to the default
   Screen->Cursor = crDefault;
}

6.

Run your program to test it.