Page 1 of 1

TPrintPreview: set custom orientation

PostPosted: May 19th, 2009, 10:27 am
by leprince
Hello all and many thanks to Kambiz for a fine component!
My question ist: how do i ...

Code: Select all
if (today is Monday) then
  PrintPreview.Orientation := poPortrait
else
  PrintPreview.Orientation := poLandscape;
PrintPreview.BeginDoc;
  ... do some fancy printing ...
PrintPreview.EndDoc;


Sadly, when showing the form, the result is ALWAYS in poPortrait, no matter what.

Any idea, what i am doing wrong?
TIA and best wishes, WP

Re: TPrintPreview: set custom orientation

PostPosted: May 19th, 2009, 10:58 am
by Kambiz
I use similar code in my programs and it works.

Do you use the latest version of the component?

Re: TPrintPreview: set custom orientation

PostPosted: May 19th, 2009, 11:43 am
by leprince
Kambiz,
yes, i do. Just try the following in the demo:

Code: Select all
procedure TMainForm.GeneratePages;
begin
  with PrintPreview do
  begin
    Orientation := poLandscape;                  //        <==
    {$IFDEF COMPILER7_UP}
    PageBoundsAfterMargin := GetPageSetupParameters(PageSetupDialog);
    {$ELSE}
    PageBoundsAfterMargin := PageBounds;
    with PointFrom(mmLoMetric, 100, 100) do
      InflateRect(PageBoundsAfterMargin, -X, -Y);
    {$ENDIF}
    BeginDoc;
    try
      DrawImageTextPage;
      NewPage;
      DrawImageOnlyPage;
      NewPage;
      DrawRichTextPage;
    finally
      EndDoc;
    end;
  end;
end;


The result will always be portrait. See?
best wishes, WP

Re: TPrintPreview: set custom orientation

PostPosted: May 19th, 2009, 12:38 pm
by Kambiz
Consider that GetPageSetupParameters alters paper size as well as paper orientation.

Re: TPrintPreview: set custom orientation

PostPosted: May 19th, 2009, 1:40 pm
by leprince
Thanks for pointing this out. I looked at the code and considered ;-)
My problem was that although the orientation was poLandscape, the width and height did not swap correctly.

After changing the procedure SetPageSetupParameters, my goal is reached and everything works as expected. What do you think of it?

Code: Select all
procedure TPrintPreview.SetPageSetupParameters(PageSetupDialog: TPageSetupDialog);
var
  OutUnit: TUnits;
begin
  case PageSetupDialog.Units of
    pmMillimeters: OutUnit := mmHiMetric;
    pmInches: OutUnit := mmHiEnglish;
  else
    OutUnit := UserDefaultUnits;
  end;
  //new begin
  Printer.Orientation := Orientation;
  case Orientation of
    poLandscape:
      begin
        PaperHeight := min(PaperHeight, PaperWidth);
        PaperWidth  := max(PaperHeight, PaperWidth);
      end;
    poPortrait:
      begin
        PaperHeight := max(PaperHeight, PaperWidth);
        PaperWidth  := min(PaperHeight, PaperWidth);
      end;
  end;
  PageSetupDialog.PageHeight := ConvertY(PaperHeight, FUnits, OutUnit);
  PageSetupDialog.PageWidth  := ConvertX(PaperWidth, FUnits, OutUnit);
  // new end
end;


best wishes, WP

Re: TPrintPreview: set custom orientation

PostPosted: May 19th, 2009, 2:56 pm
by Kambiz
Well, there is another consideration: paper width can be larger than paper height while the orientation is still portrait. For example, ledger paper.

Because of the above, your code doesn't work for all sort of papers.

I checked my code (v5.14) again; it works as expected.

Re: TPrintPreview: set custom orientation

PostPosted: May 20th, 2009, 10:00 am
by leprince
Kambiz,
i was not aware of such considerations, you are certainly right.
After digging deeper, i think i am on the right track now...

Although you do not like to speak about the demos, it was my first reference of course. Sorry for that!

There you have a TRect called PageBoundsAfterMargin which is supposed to be the papersize minus the margins from the setupdialog.

After changing the orientation, the PageBoundsAfterMargin is not correct (still has the portrait-values), therefore it has to be rectified. This is done via GetPageSetupParameters. Before you can call GetPageSetupParameters you have to change the settings of the PageSetupDialog. This is done via SetPageSetupParameters - which unintendedly swaps the PageWidth and PageHeight back to portrait-values instead of setting them to PaperHeight and PaperWidth which are already set correctly.

Meaning:
Code: Select all
    PrintPreview.Orientation   := poLandscape;
    PrintPreview.SetPageSetupParameters(PageSetupDialog);
    PageBoundsAfterMargin      := PrintPreview.GetPageSetupParameters(PageSetupDialog);
    PrintPreview.BeginDoc;   


I hope i explained it understandably.

Anyway, after seeing what's going on, i have programmed my solution - without changing your code ;-)
Code: Select all
    PrintPreview.Clear;
    PrintPreview.Printer.Orientation := poLandscape;
    PrintPreview.Orientation   := poLandscape;
    PageSetupDialog.PageWidth  := PrintPreview.ConvertX(PrintPreview.PaperWidth , PrintPreview.Units, OutUnit);
    PageSetupDialog.PageHeight := PrintPreview.ConvertY(PrintPreview.PaperHeight, PrintPreview.Units, OutUnit);
    PageBoundsAfterMargin      := PrintPreview.GetPageSetupParameters(PageSetupDialog);
    PrintPreview.BeginDoc;


Thanks and best wishes, WP

Re: TPrintPreview: set custom orientation

PostPosted: May 20th, 2009, 1:49 pm
by Kambiz
Sorry, I think I am lost.

In the general demo, when I change paper, orientation, or margins the generated pages are as they suppose to be. Also, the information shown on the status bar correctly reflects the applied changes. Because of that, I don't know what you are talking about, sorry.

Which version of Delphi you use? Maybe PrintPreview has bug in your version of Delphi. I have tested the latest version of component only on Delphi 7 and 2009.

Re: TPrintPreview: set custom orientation

PostPosted: May 20th, 2009, 2:02 pm
by leprince
Kambiz,
i use D7 on WinXP Prof SP2.
Anyhow, please do not put too much effort into it - my *solution* works and that's all i need.

Maybe you could post a quick codesample of your way of changing the orientation via code at runtime? It may help clarifying things.

All the best and despite all issues one might have - thanks for this component!
WP