2011-08-19 107 views
4

我正在編寫一個所見即所得的頁面設計器應用程序,它允許用戶將圖像和文本拖放到設計器面板上,然後將面板打印到PDF。如何獲取所有紙張尺寸名稱和相應的像素尺寸?

在我的應用程序的頁面設置選項中,用戶需要選擇一個頁面尺寸,然後根據選定的頁面尺寸,它將在屏幕上顯示一個根據尺寸確定尺寸的面板(例如,A4選擇= 8.5 x 11英寸和麪板的尺寸將根據這些像素尺寸而定)。

然後,當用戶單擊「打印」時,面板的內容將被繪製爲具有所選尺寸的PDF文件。

我正在使用wPDF組件集,特別是TWPPDFPrinter組件來創建PDF。

我的問題:

  1. 如何獲取所有紙張大小的列表名稱,然後如何讓自己的相應尺寸wPDFPrinter?

在此先感謝。

+0

請準確定義「所有紙張大小」 –

回答

2

要獲得在系統中定義的所有打印機表單列表:

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; 
+0

Thx的意思。但首先是TWPPDFPrinter是一個組件,那麼如何選擇這個作爲打印機索引? –

+0

@Steve - 我認爲這是一臺打印機...無論如何,您要麼獲得假脫機程序中定義的所有表單的列表,要麼獲取特定打印機支持的表單列表。對於前者的'nil'作爲'OpenPrinter'的第一個參數,對於後者,你應該傳遞一個打印機名稱作爲'OpenPrinter'的第一個參數。 (當然,實際上並不是完全必要選擇打印機,只需將「零」或有效的「打印機名稱」傳遞給OpenPrinter)。 –

+0

OTOH它似乎沒有區別。請參閱[在此頁面上]的註釋(http://www.berezniker.com/content/pages/visual-foxpro/enumerating-printer-forms),我可以在這裏驗證並且網絡上有線程數量相同。簡而言之,如果您想獲得特定打印機支持的列表,請勿使用'EnumForms',請使用'DeviceCapabilities'。 –

2

您可以使用EnumForms和查詢本地打印服務器來獲取紙張大小列表。看this question

1

你爲什麼不使用默認的打印機設置對話框?

鏈接如下處理到OnAccept事件TFilePrintSetup行動:

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

等瞧。