4

I am writing a WYSIWYG page designer application, which will allow user to drop images and text onto a designer Panel and then print the panel to PDF.

In my application's Page Setup options, the user needs to select a page size, and then, based on selected page size it will show a panel on screen that is sized according to the dimensions (eg A4 selected = 8.5 x 11 inches and Panel will be sized to these pixel dimensions) .

Then when user clicks Print, the contents of the Panel will be drawn to a PDF file with the selected dimensions.

I am using the wPDF component set, and the TWPPDFPrinter component in particular to create the PDF.

My question:

  1. How to get list of all paper sizes names, and then how to get their corresponding dimensions for wPDFPrinter ?

Thanks in advance.

3 Answers3

2

You can get a list of paper sizes using EnumForms and Querying the local print server. see this question

Community
  • 1
  • 1
James
  • 9,774
  • 5
  • 34
  • 58
1

To get the list of all the printer forms defined in a system:

uses
  winspool, printers;

...


procedure TForm1.Button1Click(Sender: TObject);
var
  HPrinter: THandle;
  Forms: array of TFormInfo1;
  Count, Needed, Returned: DWORD;
  i: Integer;
begin
  Memo1.Clear;
  if OpenPrinter(nil, HPrinter, nil) then begin
    try
      if not EnumForms(HPrinter, 1, nil, 0, Needed, Returned) then begin

        // we should fail here since we didn't pass a buffer
        if GetLastError <> ERROR_INSUFFICIENT_BUFFER then
          RaiseLastOSError;

        Count := (Needed div SizeOf(TFormInfo1)) + 1;
        SetLength(Forms, Count);
        if EnumForms(HPrinter, 1, @Forms[0], SizeOf(TFormInfo1) * Count, Needed,
            Returned) then begin
          if Returned < Count then
            SetLength(Forms, Returned);
          for i := 0 to Returned - 1 do begin
             Memo1.Lines.Add(Format('Paper name: %s,   Paper size: %dmm x %dmm',
                            [Forms[i].pName,
                             Forms[i].Size.cx div 1000,
                             Forms[i].Size.cy div 1000]))
          end;
        end else
          RaiseLastOSError;
      end;
    finally
      ClosePrinter(HPrinter);
    end;
  end else
    RaiseLastOSError;
end;
Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
  • Thx for the answer. But the first thing is that TWPPDFPrinter is a component, so how do I select this as the printer index? –  Aug 20 '11 at 17:09
  • @Steve - I thought it was a printer... Anyway, you'd either get the list of all forms defined in the spooler, or get the list of forms supported by a specific printer. For the former pass 'nil' as the first parameter of `OpenPrinter`, for the latter you should pass a printer name as the first parameter of 'OpenPrinter'. (Of course it is in fact not exactly necessary to select the printer, just pass a 'nil' or a valid 'printer name' to OpenPrinter). – Sertac Akyuz Aug 21 '11 at 02:12
  • OTOH it doesn't seem to make a difference. See the note [on this page](http://www.berezniker.com/content/pages/visual-foxpro/enumerating-printer-forms), which I can verify here and there are threads on the net amounting to the same. In short, if you want to get the list that a specific printer supports do not use 'EnumForms', use 'DeviceCapabilities'. – Sertac Akyuz Aug 21 '11 at 02:30
0

Why don't you use the default printer setup dialog?

Link the following handler to the OnAccept event of a TFilePrintSetup action:

procedure TForm1.FilePrintSetup1Accept(Sender: TObject);
var
  Scale: Single;
begin
  Scale := Printer.PageHeight / Printer.PageWidth;
  DesignerPanel.Height := Round(DesignerPanel.Width * Scale);
end;

Ét voila.

NGLN
  • 43,011
  • 8
  • 105
  • 200