delphi pages per sheet

Hot to set pages per sheet programmatically em Delphi
mccoymadAsked:
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.

mccoymadAuthor Commented:
Hot to set pages per sheet programmatically em Delphi
F IgorDeveloperCommented:
There is not a "generic" options in the Printer component that configure that.
Generally it's a feature from the printer driver.
You need to implement a internal conversion to the printing procedure
to fit any page arrangement into one page.

Please post the part of the code that prints the current document.
Emmanuel PASQUIERFreelance Project ManagerCommented:
I have made an application that can calculate the layout of multiple pages in a single sheet, using the rendered image of each page.
The purpose was to print booklets just by cutting and stapple the sheets
That could be modified to suit your needs. If you want something like that, tell me what exactly you want to achieve and I'll see if I can find it and modify it for you
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

mccoymadAuthor Commented:
//My code for configution print
procedure TForm1.ApplySettings;
var
  DevMode: PDeviceMode;
  hDevMode: THandle;
  FDevice, FDriver, FPort: array[0..255] of Char;
begin
  Printer.PrinterIndex := 6;
  Printer.GetPrinter(FDevice, FDriver, FPort, hDevMode);
  WPViewPDF1.CommandStr(COMPDF_SelectPrinter, FDevice);
  WPViewPDF1.CommandEx(COMPDF_DONTSETDEVMODE,1);

  DevMode := GlobalLock(hDevMode);
  try
    // Tray selection
{    DevMode^.dmDefaultSource := fOutputBin;}
    DevMode^.dmFields := DevMode^.dmFields or DM_DEFAULTSOURCE;

    // Orientation
    DevMode^.dmOrientation := Ord(1) + 1;   // 1=Portrait, 2=Lansdscape
    DevMode^.dmFields := DevMode^.dmFields or DM_ORIENTATION;

    // Apply changes
    WPViewPDF1.CommandStrEx(COMPDF_PrintSetDEVMODE, '', Cardinal(DevMode));

  finally
    GlobalUnlock(hDevMode);
  end;
end;
mccoymadAuthor Commented:
//My code for configution print
procedure TForm1.ApplySettings;
var
  DevMode: PDeviceMode;
  hDevMode: THandle;
  FDevice, FDriver, FPort: array[0..255] of Char;
begin
  Printer.PrinterIndex := 6;
  Printer.GetPrinter(FDevice, FDriver, FPort, hDevMode);
  WPViewPDF1.CommandStr(COMPDF_SelectPrinter, FDevice);
  WPViewPDF1.CommandEx(COMPDF_DONTSETDEVMODE,1);

  DevMode := GlobalLock(hDevMode);
  try
    // Tray selection
{    DevMode^.dmDefaultSource := fOutputBin;}
    DevMode^.dmFields := DevMode^.dmFields or DM_DEFAULTSOURCE;

    // Orientation
    DevMode^.dmOrientation := Ord(1) + 1;   // 1=Portrait, 2=Lansdscape
    DevMode^.dmFields := DevMode^.dmFields or DM_ORIENTATION;

    // Apply changes
    WPViewPDF1.CommandStrEx(COMPDF_PrintSetDEVMODE, '', Cardinal(DevMode));

    // Two pages per sheet
    //?
    //?
    //?

  finally
    GlobalUnlock(hDevMode);
  end;
end;
Emmanuel PASQUIERFreelance Project ManagerCommented:
hum, you are printing from PDF object, so my solution will not be good for you.
It would require first that you render the PDF pages as images, and only then could you scale them and print them with 2^N pages per sheet. While this would work on all printers, that would be quite a mess to do.

as fraigor said, not all printers support that kind of functionality, so you have first to check that yours can do it.

Then, there is a solution here :
http://www.experts-exchange.com/Programming/System/Windows__Programming/A_2090-How-to-set-Pages-Per-Sheet-Programmatically-for-N-Up-Printing.html
it is a C++ solution, so it would require some work to translate to Delphi
I'll give it a try, if you can check in the meantime that you CAN print multiple pages by doing it manually.
See in the printer dialog, in your printer properties. For my Canon color laser, it is there, but it's a very recent model, so maybe you won't be so happy
Emmanuel PASQUIERFreelance Project ManagerCommented:
now that is curious :
DevMode^.dmOrientation := Ord(1) + 1;   // 1=Portrait, 2=Lansdscape
why not set := 2 directly ?? or use the constant DMORIENT_LANDSCAPE that should be declared somewhere (probably in Windows unit)

I think that your solution is this :

 pByteArray(DevMode)^[sizeof(PDeviceMode^)+24]:=1; // 1=> 2 pages per sheet

pByteArray is declared in SysUtils

(**... after a bit testing before posting ... **)
I tested the code below with my printer, and it would appear that it does not work. And after more testing, the reason is simple : no 2 printers use the extra memory zone the same way to store their private properties. That was to be expected.

I post this anyway, and I will post later some code that I made to explore that memory zone and detect how YOUR printer works. So the real answer to your question is that it is NOT possible to make a global solution, but with patience and the code I will provide, you COULD make a solution for a SPECIFIC printer/driver
procedure TForm1.ApplySettings;
var
  DevMode: PDeviceMode;
  hDevMode: THandle;
  FDevice, FDriver, FPort: array[0..255] of Char;
  DevModeExtra:pByteArray;
begin
  Printer.PrinterIndex := 6;
  Printer.GetPrinter(FDevice, FDriver, FPort, hDevMode);
  WPViewPDF1.CommandStr(COMPDF_SelectPrinter, FDevice);
  WPViewPDF1.CommandEx(COMPDF_DONTSETDEVMODE,1);
  DevMode := GlobalLock(hDevMode);
  try
    // Tray selection
    DevMode^.dmFields := DevMode^.dmFields or DM_DEFAULTSOURCE;
    DevMode^.dmDefaultSource := fOutputBin;

    // Orientation
    DevMode^.dmOrientation := 2;   // 1=Portrait, 2=Lansdscape
    DevMode^.dmFields := DevMode^.dmFields or DM_ORIENTATION;

    // Two pages per sheet // 1=> 2 pages per sheet
    if DevMode^.dmNup=1 Then
     begin // N_Up printing supported
      DevModeExtra:=@pByteArray(DevMode)^[sizeof(PDeviceMode^)];
      DevModeExtra^[24]:=1; 
      if ( DevMode^.dmDriverExtra > 564 ) 
       Then  
        DevModeExtra^[564]= 0;  // 1= no border lines; 0= draw borders
     end;
    
    // Apply changes
    WPViewPDF1.CommandStrEx(COMPDF_PrintSetDEVMODE, '', Cardinal(DevMode));
  finally
    GlobalUnlock(hDevMode);
  end;
end;

Open in new window

Emmanuel PASQUIERFreelance Project ManagerCommented:
In one form, put : one memo, one printerdialog, one button

principle : each time you click the button, the printer dialog appears and you can select a printer, go in its properties and change whatever you want. It then compares the private memory zone of the printer with the last saved state of this memory zone. Of course it will not the first time you do that.

By playing a bit, and changing one parameter at a time, you can then reverse-engineer the memory zone structure.

The best you could do then, to create a "global" solution, is to let the user parametrize the printer with the correct settings, store the entire memory zone in a file associated with that printer, and after that initial config, each time you print you copy the whole settings back in the device driver memory.

Uses Classes, Windows, Form;

type
  TForm1 = class(TForm)
    btn1: TBitBtn;
    mmo1: TMemo;
    dlgPntSet: TPrinterSetupDialog;
    procedure btn1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Déclarations privées }
    _Extra:Boolean;
    _DevExtra:Array[0..6000] Of Byte;
  public
    { Déclarations publiques }
    procedure ComparePrinterSettings;
  end;

implementation

Uses Printer;

procedure TForm1.FormCreate(Sender: TObject);
begin
 _Extra:=False;
end;

procedure TForm1.CompareSettings;
var
  DevMode: PDeviceMode;
  hDevMode: THandle;
  FDevice, FDriver, FPort: array[0..255] of Char;
  DevModeExtra:pByteArray;
  i:integer;
begin
  if Not dlgPntSet.Execute Then Exit;
  Printer.GetPrinter(FDevice, FDriver, FPort, hDevMode);
  DevMode := GlobalLock(hDevMode);
  DevModeExtra:=@pByteArray(DevMode)^[sizeof(TDeviceMode)];
  mmo1.Lines.Add(Format('%d %d %d %d',[Integer(DevMode),sizeof(TDeviceMode),Integer(DevModeExtra),DevMode^.dmDriverExtra]));
  if _Extra then
   begin
    for i := 0 to DevMode^.dmDriverExtra - 1 do if DevModeExtra^[i]<>_DevExtra[i] then
     mmo1.Lines.Add(Format('Dif @%d : %x %x',[i,DevModeExtra^[i],_DevExtra[i]]));
   end;
  _Extra:=True;
  Move(DevModeExtra^,_DevExtra[0],DevMode^.dmDriverExtra);
end;

procedure TForm1.btn1Click(Sender: TObject);
begin
 CompareSettings;
end;

end.

Open in new window

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
mccoymadAuthor Commented:
Very strange!
Because the routine below prints two copies BUT do not print with Lansdscape orientation?

procedure TForm1.ApplySettings;
var
  DevMode: PDeviceMode;
  hDevMode: THandle;
  FDevice, FDriver, FPort: array[0..255] of Char;
begin
  Printer.PrinterIndex := 6;
  Printer.GetPrinter(FDevice, FDriver, FPort, hDevMode);
  WPViewPDF1.CommandStr(COMPDF_SelectPrinter, FDevice);
  WPViewPDF1.CommandEx(COMPDF_DONTSETDEVMODE,1);

  DevMode := GlobalLock(hDevMode);
  try
    // Tray selection
{    DevMode^.dmDefaultSource := fOutputBin;}
    DevMode^.dmFields := DevMode^.dmFields or DM_DEFAULTSOURCE;

    // Orientation. It NOT functioned
    DevMode^.dmOrientation := DMORIENT_LANDSCAPE;  
    DevMode^.dmFields := DevMode^.dmFields or DM_ORIENTATION;
   
   //Number of Copies. it FUNCTIONED
    DevMode^.dmCopies := 2;    

    // Apply changes
    WPViewPDF1.CommandStrEx(COMPDF_PrintSetDEVMODE, '', Cardinal(DevMode));

  finally
    GlobalUnlock(hDevMode);
  end;
end;
Emmanuel PASQUIERFreelance Project ManagerCommented:
>    // Orientation. It NOT functioned
>    DevMode^.dmOrientation := DMORIENT_LANDSCAPE;  
>    DevMode^.dmFields := DevMode^.dmFields or DM_ORIENTATION;

Well, it should have worked. Maybe the PDF component overrides the orientation of the page because of the document layout. That would make some sense, because otherwise a portrait page would not fit in a landscape sheet
mccoymadAuthor Commented:
This solves my problem
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
Editors IDEs

From novice to tech pro — start learning today.