?
Solved

Changing printer orientation

Posted on 2004-04-28
10
Medium Priority
?
1,132 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 75 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

719 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