2011-08-19 71 views

回答

3

您將需要創建一個打印機設備上下文,並使用該設備上下文作爲參考DC呈現您的頁面,同時跟蹤您已呈現的頁面數量。這將不得不在.NET打印基礎架構的範圍之外完成。

  1. 得到參考打印機DC
  2. 基於參考打印機DC
  3. 創建圖形對象上繪製位圖
  4. 渲染頁面使用的圖形對象以位圖上創建位圖(頁數進行計數這裏)
  5. 更多數據打印?轉到4

下面是在步驟1中了一槍,假設你的WinForms正在努力...

Private Function GetHighResolutionGraphics() As Graphics 

     Try 

      Dim HighestResolution As Printing.PrinterResolution = Nothing 
      Dim HighestResolutionPrinter As String = "" 
      Dim XResolution As Integer = Integer.MinValue 

      Using dlg As New PrintDialog 

       For Each Printer As String In Printing.PrinterSettings.InstalledPrinters 
        dlg.PrinterSettings.PrinterName = Printer 
        For Each Resolution As Printing.PrinterResolution In dlg.PrinterSettings.PrinterResolutions 
         Using gr As Graphics = dlg.PrinterSettings.CreateMeasurementGraphics() 
          If gr.DpiX > XResolution Then 
           HighestResolution = Resolution 
           HighestResolutionPrinter = Printer 
           XResolution = gr.DpiX 
          End If 
         End Using 
        Next 
       Next 

       dlg.PrinterSettings.PrinterName = HighestResolutionPrinter 
       dlg.PrinterSettings.DefaultPageSettings.PrinterResolution = HighestResolution 

       Return dlg.PrinterSettings.CreateMeasurementGraphics() 

      End Using 

     Catch ex As Exception 
      ' handle or ignore .NET AccessViolation for certain network printers that are turned off, etc... 
     End Try 

     Return Me.CreateGraphics() 

    End Function 

第二步是「簡單的」使用與已經實施PagePrint事件返回參考Graphics對象代碼將頁面渲染到適當的位圖,同時跟蹤您正在渲染的頁面數量。不要忘記將PagePrint事件重構爲一個單獨的例程,該例程接受一個Graphics對象,以便它可以用於打印,預覽和頁碼。不要忘記處理圖形對象和位圖

using gfxReference as Graphics = GetHighResolutionGraphics() 
    using bmpPage as new Bitmap(gfxReference.DpiX * 8.5, gfxReference.DpiY * 11) 
    using gfxRender as Graphics = Graphics.FromImage(bmpPage) 
     gfxRender.Clear(Color.White) 
     // Existing PagePrint event logic goes here (uses gfxRender) 
     // Track Number of pages printed 
    end using 
    end using 
end using