Blog pribadi untuk curhat, share tentang script, software, kuliah, project IT dan seputar IT.

FastReport get the current paper size from the printer and use it for the report

Tidak ada komentar

 

FastReport can be configured to get the current paper size from the printer and use it for the report. However, it requires some manual steps in your Delphi code to correctly read the printer's settings and then apply them to the FastReport report object.

Here's a breakdown of how it works and a Delphi script to accomplish this.

Understanding the Process

  1. FastReport's Default Behavior: By default, FastReport uses the TfrxReportPage.PaperSize property to determine the page dimensions. This is often set to a standard size like DMPAPER_A4 at design time.
  2. Getting Printer Information: The key is to use the Windows API and the Delphi Printers unit to access the TPrinter object. This object holds all the configuration for the currently selected printer, including its DM_PAPERSIZE, DM_PAPERWIDTH, and DM_PAPERLENGTH properties.
  3. Applying to FastReport: Once you have the custom dimensions, you can programmatically set the TfrxReportPage.PaperWidth and TfrxReportPage.PaperHeight properties. It's important to set the PaperSize property to DMPAPER_USER (which has a value of 256) to signal that you are using custom dimensions.

Delphi Script to Dynamically Set Paper Size

This script demonstrates a function that can be called before a report is prepared or printed. It gets the current printer's paper size and applies it to a FastReport page.

http://googleusercontent.com/immersive_entry_chip/0

 

unit frxUtils;

 

interface

 

uses

  Windows, Messages, SysUtils, Classes, frxClass, Printers, frxPrinters, frxCustomDB,

  frxDSys, frxDesign, frxGDIplus;

 

// This procedure gets the current printer's paper dimensions and applies them

// to the specified FastReport page.

procedure SetReportPaperSizeFromPrinter(aReport: TfrxReport);

 

implementation

 

procedure SetReportPaperSizeFromPrinter(aReport: TfrxReport);

var

  Device, Driver, Port: array[0..255] of Char;

  hDeviceMode: THandle;

  pDeviceMode: PDeviceMode;

  frxPage: TfrxReportPage;

  PaperWidth, PaperHeight: Integer;

begin

  // Ensure the printer is initialized and there is a report to work with

  if (aReport = nil) or (aReport.Pages[0] = nil) then

    Exit;

 

  // Get the current printer's device mode information

  Printer.GetPrinter(Device, Driver, Port, hDeviceMode);

  pDeviceMode := GlobalLock(hDeviceMode);

 

  try

    // Check if the DeviceMode is valid

    if (pDeviceMode <> nil) and (pDeviceMode.dmSize >= SizeOf(TDeviceMode)) then

    begin

      // Read the paper dimensions from the device mode.

      // dmPaperWidth and dmPaperLength are in 0.1mm units.

      PaperWidth := pDeviceMode.dmPaperWidth;

      PaperHeight := pDeviceMode.dmPaperLength;

 

      // Find the first report page

      frxPage := aReport.Pages[0] as TfrxReportPage;

 

      // Apply the custom paper size to the FastReport page

      // It's crucial to set PaperSize to DMPAPER_USER to enable custom dimensions

      frxPage.PaperSize := DMPAPER_USER;

     

      // FastReport's PaperWidth and PaperHeight properties are in millimeters.

      // So, we divide the DM values (in 0.1mm) by 10.

      frxPage.PaperWidth := PaperWidth / 10;

      frxPage.PaperHeight := PaperHeight / 10;

    end;

  finally

    // Unlock and free the handle

    if hDeviceMode <> 0 then

    begin

      GlobalUnlock(hDeviceMode);

      GlobalFree(hDeviceMode);

    end;

  end;

end;

 

end.

 

 

 

 

### How to Use the Script

 

1.  **Save the Unit:** Save the code above as `frxUtils.pas` and add it to your project.

2.  **Call the Procedure:** Before showing the report preview or printing, call the `SetReportPaperSizeFromPrinter` procedure. This ensures the report is sized correctly for the currently selected printer.

 

 

Here's an example of how you might call it from a button click event:

 

```delphi

procedure TForm1.Button1Click(Sender: TObject);

begin

  // Set the printer before preparing the report

  // This is an optional step if you want to use a specific printer instead of the default

  frxReport1.SelectPrinter;

 

  // Get the printer's custom size and apply it to the report page

  SetReportPaperSizeFromPrinter(frxReport1);

 

  // Prepare and show the report

  frxReport1.ShowPreparedReport;

end;

Tidak ada komentar :

Posting Komentar