How can I set a printer's papersize?

Hello,
  My program use many report,and it's papersize isn't same,How can I let the user Preview the report use the correspond papersize,for example:
  Set the Printer's papersize to A3;
  Report_PaperSize_A3.Preview;
  Set the Printer's PaperSize to A4;
  Report_PaperSize_A4.Preview;
  How can I relize it?
  thank u.
  zhijun_chen
 
zhijun_chenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rwilson032697Commented:
You can use the TQuickRep.PaperSize property:

eg:
QuickRep1.PaperSize := A4;

Set is to default to use the paper size currently loaded into the printer.

Cheers,

Raymond.
0
intheCommented:
you can change teh paper size and bin of printer like so:

unit Unit1;

interface

uses
  Windows, Messages,Printers, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
Device : array[0..255] of char;
Driver : array[0..255] of char;
Port   : array[0..255] of char;
hDMode : THandle;
PDMode : PDEVMODE;
begin
  Printer.GetPrinter(Device, Driver, Port, hDMode);
  if hDMode <> 0 then begin
    pDMode := GlobalLock(hDMode);
    if pDMode <> nil then begin

     {Set to legal}
      pDMode^.dmFields := pDMode^.dmFields or dm_PaperSize;
      pDMode^.dmPaperSize := DMPAPER_LEGAL;

     {Set to custom size}
      pDMode^.dmFields := pDMode^.dmFields or
                          DM_PAPERSIZE or
                          DM_PAPERWIDTH or
                          DM_PAPERLENGTH;
      pDMode^.dmPaperSize := DMPAPER_USER;
      pDMode^.dmPaperWidth := 100 {SomeValueInTenthsOfAMillimeter};
      pDMode^.dmPaperLength := 100 {SomeValueInTenthsOfAMillimeter};

     {Set the bin to use}
      pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
      pDMode^.dmDefaultSource := DMBIN_MANUAL;

      GlobalUnlock(hDMode);
   end;
  end;
  Printer.PrinterIndex := Printer.PrinterIndex;  Printer.BeginDoc;
Printer.Canvas.Font.Name := 'Courier New';
Printer.Canvas.Font.Size := 10;
Printer.Canvas.Font.Color:=ClRed;
Printer.Canvas.TextOut(100,100, 'Test 1');
  Printer.EndDoc;
end;
end.




Regards Barry
0
zhijun_chenAuthor Commented:
Thank u,all
  I want it work with QuickRep, How can do it?
  To Inthe:
  u perhas miss a word:
  Printer.SetPrinter(.....), it doesn't work.
  zhijun_chen
 
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

rwilson032697Commented:
You will need to include printers in your uses clause.

Did you try my suggestion? This allows you to preview the report with any required paper size or the existing default paper size in the printer.

Cheers,

Raymond.
0
EpsylonCommented:
You can set the papersize after you dropped a QuickReport on the form by right-clicking on it and selecting 'Report Settings'
0
zhijun_chenAuthor Commented:
Thank u,rwilson
  I have try your suggestion,But it doesn't work, the Quickreport's preview
window still is the default settings,for example, if I set the printer's papersize A4 as My printer's default(In  windows), the Quickreport's preview window still be A4.

  To Epsylon,
  I want to create the report at runtime.
  thank u,anyway.
  zhijun_chen
0
intheCommented:
did you try changing the printers papersize by code first then calling preview?
0
zhijun_chenAuthor Commented:
Hello inthe,
  I have try, but it doesn't work.
  I think the only solution is to change the windows's default printer settings at runtime, but how can do it?
  thank u.
0
intheCommented:
maybe you can use the quickreports settings sometyhing like:

//add to uses QRPRNTR,printers;
 {the orientation needs printers unit}


var
a: TPrinterSettings;
begin
a.PaperSize := A3;
a.Copies := 1;
a.Orientation := PoPortrait;
a.ApplySettings;

then call preview..
0
intheCommented:
did you try?
0
rwilson032697Commented:
This is based on I found in my PAQ collection, it may be of use:


What you need to do is to add a public property of your preview form of type TQuickRep like the following:

type
  TfrmPreview = class(TForm)
    QRPreview: TQRPreview;
    .....
  public
    { Public declarations }
    CurrentReport : TQuickRep;
  end;

In the OnPreview event of the report, you would use something like:

procedure TfrmMyReport.QuickRep1Preview(Sender: TObject);
begin
  with frmPreview do
  begin
    CurrentReport := QuickRep1;
    QRPreview.QRPrinter := TQRPrinter(Sender);
    Show;
  end
end;

That lets you reference the calling report through the preview's CurrentReport variable.  Then to change the papersize you would do something like the following:

  with CurrentReport.PrinterSettings do
  begin
{ Set the paper size }
    PaperSize := A3;
    ApplySettings; // Needed?
  end;
  QRPreview.qrprinter.Print;

Cheers,

Raymond.
0
intheCommented:
with regards to:

I think the only solution is to change the windows's default printer settings at runtime, but how can do it?


maybe use the PrintSetupDialog component
(i cant remember if it is standard component or i downloaded it)
0
rwilson032697Commented:
TPrinterSetupDialog is a standard component

Raymond.
0
mscatenaCommented:
The following code can change the paper size:

procedure ChangePaperSize(PaperSize, Comp, Alt: integer);
var
  ADevice, ADriver, APort: array[0..255] of char;
  DeviceMode: THandle;
  M: PDevMode;
  s: string;
begin
  // Força use of Printer due to VCL bug. Do not remove
  S := Printer.Printers[Printer.PrinterIndex];
  // Get current data
  Printer.GetPrinter(ADevice, ADriver, APort, DeviceMode);
  M := GlobalLock(DeviceMode);
  try
    if M <> nil then
    begin
      // Change paper size
      M^.dmFields := DM_PAPERSIZE;
      if PaperSize = DMPAPER_USER then
        M^.dmFields := M^.dmFields or DM_PAPERLENGTH or DM_PAPERWIDTH;
      M^.dmPaperLength := Alt;
      M^.dmPaperWidth := Comp;
      M^.dmPaperSize := PaperSize;//
      // Update
      Printer.SetPrinter(ADevice, ADriver, APort, DeviceMode);
    end;
  finally
    GlobalUnlock(DeviceMode);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ChangePaperSize(DMPAPER_LETTER, 0, 0);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ChangePaperSize(DMPAPER_USER, StrToInt(EdComp.Text), StrToInt(EdAlt.Text));
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
intheCommented:
mscatena ,
please leave comments only,do you not see i already posted that before ?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.