Printing Multiple Images (C++ Builder 4)

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.

image\btncmd.gif Select a Button control; and add it to your main form. Put it at the top of the form to keep it away from the image.

3.

Change the Button control's Caption to Print Twice on One Page.

4.

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

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

LEADRasterProcess * pRasterProc;
//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;

CoCreateInstance(CLSID_LEADRasterProcess, NULL, CLSCTX_ALL, IID_ILEADRasterProcess, (void**)&pRasterProc);
//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
pRasterProc->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 ( ) ;
  pRasterProc->Release ();
//Set the mouse pointer back to the default
Screen->Cursor = crDefault;
}

5.

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

 

At the beginning of the Unit1.cpp file, include these files

#include <printers.hpp>
#include "LTRASTERPROCLib_TLB.h"

6.

Run your program to test it.