Introduction

ATPrintPreview component allows to quickly implement "Print preview" dialog for image viewing applications. You can call preview form for a TBitmap/TImage with a single function call.

On this form user can change some options and start the actual printing. Note that the preview page on the right is adapted to match the active printer's page sizes.

Functions implement the following options:

  • Fit mode (normal, best fit, stretch...)
  • Margins
  • Footer text
  • Image position
  • Image gamma correction


Structs

Helper records:
  TFloatRect = record
    Left, Top, Right, Bottom: Extended;
  end;

  TFloatSize = record
    X, Y: Extended;
  end;

  TATPrintFooter = record
    Enabled: Boolean;
    EnableLine: Boolean;
    Caption: WideString;
    FontName: TFontName;
    FontSize: Integer;
    FontStyle: TFontStyles;
    FontColor: TColor;
    FontCharset: TFontCharset;
  end;
Printing options are specified in a record:
  TATPrintOptions = record
    Copies: Integer;
    OptFit: TATPrintFitMode;
    OptFitSize: TFloatSize;
    OptPosition: TATPrintPosition;
    OptMargins: TFloatRect;
    OptUnit: TATPrintUnit;
    OptGamma: Extended;
    OptFooter: TATPrintFooter;
    PixelsPerInch: Integer;
    JobCaption: string;
    FailOnErrors: Boolean;
  end;
Fields are:
  • Copies: Number of print copies.
  • OptFit: State of the option "Fit mode". Possible values:
    • pFitNormal: fit to page only large images
    • pFitBest: fit also small images
    • pFitStretch: stretch image to page
    • pFitSize: set custom image size
  • OptFitSize: Custom size for pFitSize fit mode.
  • OptPosition: State of the option "Position". Possible values:
    • pPosTopLeft
    • pPosTop
    • pPosTopRight
    • pPosLeft
    • pPosCenter
    • pPosRight
    • pPosBottomLeft
    • pPosBottom
    • pPosBottomRight
  • OptMargins: State of the "Margins" option (Left, top, right, bottom).
  • OptUnit: Units for margins and custom size:
    • pUnitMm: millimeters
    • pUnitCm: centimetres
    • pUnitIn: inches
  • OptGamma: Gamma correction value. Should be in 0.1...7.0, 1.0 means no gamma correction.
  • OptFooter: Footer to print below image.
  • PixelsPerInch: "Pixels per inch" value for the screen. Pass here the Screen.PixelsPerInch value.
  • JobCaption: Caption of the job in the Print Manager.
  • FailOnErrors: If set to True, then on preview errors the function will fail. If set to False, then on preview errors the form will not be shown but the printing will start without questions.


Functions

  • Units
  • Printing a bitmap
  • Printing an image

Units

First, use the following two units in your application (note that both units must be used):

  • ATPrintPreview
  • ATxPrintProc

Printing a bitmap

To call the preview form for a bitmap, use the BitmapPrint function:

function BitmapPrint(
  ABitmap: TBitmap;
  var AOptions: TATPrintOptions): Boolean;

Parameters:
ABitmap: Bitmap to print.
AOptions: Printing options.

Result:
True if printing was completed, False if printing was cancelled or some errors occured.

Printing an image

To call the preview form for an image (TImage), use the ImagePrint function:
function ImagePrint(
  AImage: TImage;
  var AOptions: TATPrintOptions): Boolean;

Parameters:
AImage: Image object to print. All image types can be printed: bitmaps, metafiles, icons etc.
AOptions: Printing options.

Result:
The same as for the BitmapPrint function.


Localizing

To localize the preview form captions, use the following variables, declared in ATPrintPreview unit. They are self-descriptive:
var
  //Captions:
  MsgPreviewDlgCaption: string = 'Print preview';
  MsgPreviewDlgPrintSetup: string = '&Print setup...';
  MsgPreviewDlgPrint: string = 'Print';
  MsgPreviewDlgCancel: string = 'Cancel';
  MsgPreviewDlgOptions: string = ' Options ';
  MsgPreviewDlgPosition: string = ' Position ';
  MsgPreviewDlgPreview: string = ' Preview ';
  MsgPreviewDlgMargins: string = ' Margins ';
  MsgPreviewDlgGamma: string = ' Gamma ';

  MsgPreviewDlgMarginLeft: string = '&Left:';
  MsgPreviewDlgMarginTop: string = '&Top:';
  MsgPreviewDlgMarginRight: string = '&Right:';
  MsgPreviewDlgMarginBottom: string = '&Bottom:';
  MsgPreviewDlgOptFitMode: string = '&Fit mode:';
  MsgPreviewDlgOptFitSizeX: string = '&Width:';
  MsgPreviewDlgOptFitSizeY: string = '&Height:';
  MsgPreviewDlgOptFitSizeProp: string = 'Proportio&nal';
  MsgPreviewDlgOptFooter: string = 'F&ooter text';
  MsgPreviewDlgOptFooterFont: string = 'Font...';
  MsgPreviewDlgOptGamma: string = '&Gamma value:';

  MsgPreviewDlgUnit: array[TATPrintUnit] of string =
    ('mm', 'cm', 'inches');
  MsgPreviewDlgFitModes: array[TATPrintFitMode] of string =
    ('Normal', 'Best fit to page', 'Stretch to page', 'Custom size');
  MsgPreviewDlgPos: array[TATPrintPosition] of string =
    ('Top-Left', 'Top', 'Top-Right',
     'Left', 'Center', 'Right',
     'Bottom-Left', 'Bottom', 'Bottom-Right');

  //Error messages:
  MsgPreviewDlgCannotAllocateMemory: string = 'Cannot allocate temporary bitmap, not enougth memory.';

  //Font:
  MsgPreviewDlgFontName: string = 'Tahoma';
  MsgPreviewDlgFontSize: Integer = 8;


History

1.3.6a (jan 2013)
  • Licensed under MPL 1.1 and put on SourceForge.
1.3.6 (oct 2008)
  • Added: Delphi 2009 compat.
1.3.5 (Mar 2008)
  • Added: Cm unit.
  • Added: Footer font color/style, footer enabling.
  • Added: Unicode footer caption.
  • Fixes.
1.3.0 (Feb 2008)
  • Added: "Custom size" fit mode.
  • Added: "Gamma" option.
  • Added: "Font" option.
  • Added: Up/down edits.
  • Added: Inches unit.
  • Changed: Functions parameters.
  • Fixed: Image flickering.
1.2.2 (Feb 2008)
  • Added: "Margins" option (currently in mm).
  • Added: "Fit mode" option.
  • Added: Footer centered.
  • Changed: Help file changed to .chm.
1.0.0 (Jul 2007)
  • Initial release.


License

This package, unless otherwise stated in the particular source code file, is distributed under Mozilla Public License Version 1.1 (the "License"). You may obtain a copy of the License at http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License.

Copyright © Alexey Torgashin.


Contacts

If you have questions, you may ask author:

Alexey Torgashin
www.uvviewsoft.com