6
在回答這個問題......如何預處理PrintDocument以計算打印前的總頁數?
接受的答案包括本聲明
您不必打印兩次,你只需要模擬第一次打印 。
那麼,如何在沒有任何輸出到打印機或屏幕的情況下先通過文檔一次?
在回答這個問題......如何預處理PrintDocument以計算打印前的總頁數?
接受的答案包括本聲明
您不必打印兩次,你只需要模擬第一次打印 。
那麼,如何在沒有任何輸出到打印機或屏幕的情況下先通過文檔一次?
您將需要創建一個打印機設備上下文,並使用該設備上下文作爲參考DC呈現您的頁面,同時跟蹤您已呈現的頁面數量。這將不得不在.NET打印基礎架構的範圍之外完成。
下面是在步驟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