Solved

Changing printer orientation

Posted on 2004-04-28
10
1,016 Views
Last Modified: 2010-04-05
Hi !
I need to know how to change the printer orientation in an allready running printjob,
I will need to set a new orientation after "printer.newpage"

first page is landscape, second is portarait

if anyone can give me a working code i would be appreciatedd!
0
Comment
Question by:joepezt
  • 4
  • 4
  • 2
10 Comments
 
LVL 1

Expert Comment

by:AngelRider
ID: 10942931
The code below is taken directly from TPrinter..

procedure TPrinter.SetOrientation(Value: TPrinterOrientation);
const
  Orientations: array [TPrinterOrientation] of Integer = (
    DMORIENT_PORTRAIT, DMORIENT_LANDSCAPE);
begin
  CheckPrinting(False);
  GetPrinterIndex;
  if DeviceMode = 0 then RaiseError(SInvalidPrinterOp);
  SetState(psNoHandle);
  DevMode^.dmOrientation := Orientations[Value];
end;


thus all you need to do is something like

  Printer.SetOrientation(DMORIENT_PORTRAIT);

after you've called Printer.NewPage.

Don't forget that printer.canvas will change size as the x and y axes of the paper have swapped!

 
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10943013
Sorry, but wrong....

CheckPrinting(False) <-- will raise an exception if the job has already been started. The TPrinter's NewPage handles the end/start page, which is where this change would need to occur.

procedure TPrinter.NewPage;
begin
  CheckPrinting(True);
  EndPage(DC);

  // Would need to add the change in here

  StartPage(DC);
  Inc(FPageNumber);
  Canvas.Refresh;
end;

This is where the getting of the devmode, setting of the new orientation, and the call to ResetDC(...) would need to occur. You have 2 options

1. Calling the EndPage on the printer handle, perform the getting/setting of the devmode, then calling ResetDC, then finally calling StartPage. Problem with this is that it will throw your page count off.
2. Write your own printer class that allows for these changes between pages.

--------

Regards,
Russell
0
 
LVL 2

Author Comment

by:joepezt
ID: 10943277
procedure TPrinter.NewOrientationPage(MyOrientation : TPrinterOrientation = PoPortrait);
begin
  CheckPrinting(True);
  EndPage(DC);
  SetOrientation(MyOrientation);
  StartPage(DC);
  Inc(FPageNumber);
  Canvas.Refresh;
end;

I tried this, but I still get an error...
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10943319

Thats what I already stated....

SetOrientation makes the following check:
CheckPrinting(False) // Will raise exception if printing is true

Plus, you need to be able to call ResetDC(...) if the printer is already open and in the middle of a document when making changes to the devmode.


Russell

0
 
LVL 2

Author Comment

by:joepezt
ID: 10943426
it is in the middle of a document, infact, it's in the newpage part...

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Expert Comment

by:AngelRider
ID: 10943534
Oh yes, thanks for the reminder, Russell. I'd completely forgotten about CheckPrinting.. I hit this problem a few years ago, and now recall that I did exactly what you suggested, and broke the document into different jobs. Fortunately, I didn't have to print page-numbers :-)

0
 
LVL 2

Author Comment

by:joepezt
ID: 10943561
should I remove checkprinting =?
I cannot split it in two documents, because then the printer wont print on both sides of the document
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10944098
"should I remove checkprinting =?"

No.

Regarding document splitting...

You can still do it as one job, you just need to be able to do what is required between the EndPage and the call to StartPage. (Get the current devmode, set the new orientation, call ResetDC(...) )

Unfortunately, while you can decend a new class from TPrinter, you don't have much control in regards to overriding the existing functionality.  As I tried to already state, you could do all this outside the printer class (It exposes enough info though its properties), but by manually calling EndPage/StartPage, the private FPageNumber variable will not get updated. Because its a private var, even decending a class won't help in that aspect.

Now, if you have the code for TPrinter, and wanted to make the change in there, then that is also another possibility (I would recommend saving the original source).
 
If I have time, I will post an example of handling this via a function, and an example of a rewrite in the NewPage function. But in truth, I'm not sure if a 25pt question really warrants more time than has already been given to this question.

Russell


0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 25 total points
ID: 10944806
procedure NewPageChangeOrient(PrinterObj: TPrinter; Orient: TPrinterOrientation);
var  lpDevice,
     lpDriver,
     lpPort:        Array [0..1024] of Char;
     lpDevMode:     PDeviceMode;
     hDevMode:      THandle;
begin

  if PrinterObj.Printing then
  begin
     EndPage(PrinterObj.Handle);
     if (Printer.Orientation <> Orient) then
     begin
        PrinterObj.GetPrinter(@lpDevice, @lpDriver, @lpPort, hDevMode);
        if (hDevMode <> 0) then
        begin
           lpDevMode:=GlobalLock(hDevMode);
           if Assigned(lpDevMode) then
           begin
              lpDevMode^.dmFields:=DM_ORIENTATION;
              lpDevMode^.dmOrientation:=DMORIENT_LANDSCAPE;
              ResetDC(PrinterObj.Handle, lpDevMode^);
           end;
        end;
     end;
     StartPage(PrinterObj.Handle);
     PrinterObj.Canvas.Refresh;
  end;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin

  Printer.Orientation:=poPortrait;
  Printer.BeginDoc;
  Printer.Canvas.Font.Name:='Courier New';
  Printer.Canvas.Font.Size:=14;

  Printer.Canvas.TextOut(10, 10, 'Hello world');

  // Old call: Printer.NewPage;
  // Replace the NewPage call with a call to NewPageChangeOrient.
  // The one thing this won't do is update the page count correctly
  NewPageChangeOrient(Printer, poLandScape);

  Printer.Canvas.TextOut(10, 10, 'Hello world');
  Printer.EndDoc;

end;

I leave it up to you to either use the code as is, or try an fold it into printers.pas. The routine gives you what you asked for though.


Russell


0
 
LVL 2

Author Comment

by:joepezt
ID: 10958140
thanks :)

i made some modifications myself in the printer.pas file

procedure TPrinter.SetOrientation(Value: TPrinterOrientation);
const
  Orientations: array [TPrinterOrientation] of Integer = (
    DMORIENT_PORTRAIT, DMORIENT_LANDSCAPE);
begin
  GetPrinterIndex;
  if DeviceMode = 0 then RaiseError(SInvalidPrinterOp);
  DevMode^.dmOrientation := Orientations[Value];
  if not Printing then
    SetState(psNoHandle);
end;

procedure TPrinter.NewOrientationPage(MyOrientation : TPrinterOrientation = PoPortrait);
begin
  CheckPrinting(True);
  EndPage(DC);
  SetOrientation(MyOrientation);
  ResetDC(DC, DevMode^);
  StartPage(DC);
  Inc(FPageNumber);
  Canvas.Refresh;
end;

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now