Recent

Author Topic: Printing - new papersize creation  (Read 11370 times)

drschwarcz

  • New Member
  • *
  • Posts: 17
Printing - new papersize creation
« on: September 05, 2014, 09:33:57 pm »
Hi!

I would need to create size A5 if does not exist in the system, which is half of size A4.

I have no any solution for now.

If someone has experience in printing, then I would really appreciate it.

Thanks in advance.

Op. system: Windows 8.1, or Windows 7
« Last Edit: September 06, 2014, 08:22:01 am by drschwarcz »
Lazarus 1.6.4 @ Windows 10 x64

Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe.

Einstein, Albert :) ;)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Printing - new papersize creation
« Reply #1 on: September 05, 2014, 11:45:57 pm »
All modern printers support A5 as a paper size (even American ones which default to inches rather than metric and have Letter as default size).
Drop a TPageSetupDialog (rightmost icon on Dialogs tab) on your form, and invoke it say in a button OnClick handler via
Code: [Select]
  PageSetupDialog1.Execute;
at runtime.
In the "Paper" groupbox of the dialog there is a "Size" dropdown which will reveal A5 along with various other settings for your default printer. See image below.

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Printing - new papersize creation
« Reply #2 on: September 06, 2014, 12:29:38 am »
Drop a TPageSetupDialog (rightmost icon on Dialogs tab) on your form, and invoke it say in a button OnClick handler via...
Or if you don't want the user of your program to do this manually you can do it in code:
Code: [Select]
Printer.PaperSize.PaperName:='A5';

drschwarcz

  • New Member
  • *
  • Posts: 17
Re: Printing - new papersize creation
« Reply #3 on: September 06, 2014, 12:32:03 am »
It's clear for me. I do not want to use Dialog, I would like to handle from my application.

...and unfortunately, size A5 is not known by each printer, for instance my printer does not have this size in defaults.

RVK: Yes, I can set the name if it exists.
« Last Edit: September 06, 2014, 08:20:26 am by drschwarcz »
Lazarus 1.6.4 @ Windows 10 x64

Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe.

Einstein, Albert :) ;)

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Printing - new papersize creation
« Reply #4 on: September 06, 2014, 12:59:15 am »
...and unfortunately, size A5 is not known by each printer, for instance my printer does not have this size in defaults.
There is a topic here where there is some code to add a custom papersize (if A5 does not exists) but it's for Delphi (and no answer for FPC).
It should get you started. (I can't test this myself at the moment).

I take it you get an exception if you try to set A5 and A5 is not defined. You can catch that exception and try to add the custom size and try to set it again. If it fails again your routine should end and not print (because the custom size failed too).

B.T.W. Did you try adding A5 manually as custom papersize in the printer configuration? And did that work when you choose that papername from Lazarus? (Just to make sure that A5 is supported on the printer itself because there might be a reason it's not in the default papersizes.)

drschwarcz

  • New Member
  • *
  • Posts: 17
Re: Printing - new papersize creation
« Reply #5 on: September 06, 2014, 01:12:47 am »
Physically, the printer is able to print in A5 format if I add it to list and I can also choose from. :)

THX for your tips. I will check that during the day.
Lazarus 1.6.4 @ Windows 10 x64

Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe.

Einstein, Albert :) ;)

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Printing - new papersize creation
« Reply #6 on: September 06, 2014, 01:33:12 pm »
I'm not sure if you've already got around to it...
But here is an attempt to set the custom papersize to A5.

(i can't test this for myself at this moment but i'm sure you can)
(You need to decide if it's 21x14,85 or 14,85x21)

I'm sure i did some things wrong (i.e. not setting a GlobalLock and Unlock on devmode etc.) but this should work for now. We can worry about cleaning it up when it works. (preferably as a separate function to set custom-paper size.)

Code: [Select]
uses Printers, OSPrinters, Windows, WinUtilPrn;

// ...

const
  DMPAPER_USER = 0; // Why is this not defined in defines.inc ??????

procedure TForm1.Button1Click(Sender: TObject);
var
  PDev: TPrinterDevice;
  dmW: PDeviceModeW;
  dmA: PDeviceModeA;
begin
  PDev := TPrinterDevice(Printer.Printers.Objects[Printer.PrinterIndex]);
  if UseUnicode then
  begin
    dmW := PDev.DevModeW;
    dmW^.dmFields := DM_PAPERLENGTH or DM_PAPERWIDTH or DM_PAPERSIZE or DM_SCALE;
    dmW^.dmPaperLength := round(21 * 100); // 1/10 of mm
    dmW^.dmPaperWidth := round(14.85 * 100); // 1/10 of mm
    dmW^.dmPaperSize := DMPAPER_USER; // Custom Size, DMPAPER_A5
    dmW^.dmScale := 100;
    ResetDCW(Printer.Canvas.Handle, dmW);
  end
  else
  begin
    dmA := PDev.DevModeA;
    dmA^.dmFields := DM_PAPERLENGTH or DM_PAPERWIDTH or DM_PAPERSIZE or DM_SCALE;
    dmA^.dmPaperLength := round(21 * 100); // 1/10 of mm
    dmA^.dmPaperWidth := round(14.85 * 100); // 1/10 of mm
    dmA^.dmPaperSize := DMPAPER_USER; // Custom Size, DMPAPER_A5
    dmA^.dmScale := 100;
    ResetDCA(Printer.Canvas.Handle, dmA);
  end;
  Printer.BeginDoc;
  // ... do you thing
  Printer.EndDoc;
end;

drschwarcz

  • New Member
  • *
  • Posts: 17
Re: Printing - new papersize creation
« Reply #7 on: September 06, 2014, 06:34:19 pm »
THX, it looks great. I will try your code soon.
Lazarus 1.6.4 @ Windows 10 x64

Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe.

Einstein, Albert :) ;)

drschwarcz

  • New Member
  • *
  • Posts: 17
Re: Printing - new papersize creation
« Reply #8 on: October 01, 2014, 09:28:29 pm »
Hi RVK,

Code is pasted to my program. The print preview is fine, but the real papersize is still A4.

I currently testing with Abode Acrobat (I have no printer at home), but I going to test it in a real printer tomorrow and I will come back with the result.  :)

Good night!
Lazarus 1.6.4 @ Windows 10 x64

Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe.

Einstein, Albert :) ;)

 

TinyPortal © 2005-2018