gpt4 book ai didi

.net - 如何在打印前预处理PrintDocument以计算总页数?

转载 作者:行者123 更新时间:2023-12-02 20:40:40 26 4
gpt4 key购买 nike

回答这个问题...

Printing page x of y in .Net

已接受的答案包括此声明...

You don't have to print it twice, you just have to simulate printing the first time.

那么,如何才能先一次性浏览文档而不将任何输出发送到打印机或屏幕?

最佳答案

您将需要创建打印机设备上下文并使用该设备上下文作为引用 DC 来渲染页面,同时跟踪已渲染的页面数。这必须在 .NET 打印基础设施的范围之外完成。

  1. 获取引用打印机 DC
  2. 根据引用打印机 DC 创建位图
  3. 创建 Graphics 对象以在位图上绘制
  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

第 2 步“简单地”使用返回的引用图形对象和已实现的 PagePrint 事件代码将页面渲染为适当的位图,同时跟踪正在渲染的页面数。不要忘记将 PagePrint 事件重构为接受 Graphics 对象的单独例程,以便它可用于打印、预览和页码编号。不要忘记处理 Graphics 对象和 Bitmap

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

关于.net - 如何在打印前预处理PrintDocument以计算总页数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7116424/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com