如何preprocess的PrintDocument打印之前计算的总页数? [英] How to preprocess PrintDocument to calculate the total number of pages before printing?

查看:249
本文介绍了如何preprocess的PrintDocument打印之前计算的总页数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答这个问题...

In an answer to this question...

打印页X在.net

接受的答案包括本声明

您不必两次打印,你只需要模拟印刷   第一次了。

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

那么,你怎么能一次通过文件首先,没有任何输出要将打印机或屏幕?

So, how can you pass through the document one time first without any output going to the printer or screen?

推荐答案

您需要创建一个打印机设备上下文和使用该设备上下文作为参考DC,同时跟踪你呈现的页数渲染你的页面。这将必须在.NET印刷基础结构的范围之外进行。

You will need to create a printer device context and render your pages using that device context as a reference DC while keeping track of the number of pages you have rendered. This will have to be done outside the scope of the .NET Printing infrastructure.

  1. 在获取参考打印机DC
  2. 创建基于参考打印机DC位图
  3. 创建图形对象上绘制位图
  4. 在渲染页面使用图形对象为位图(计数网页这里)
  5. 更多的数据打印?转到4

下面是一张在步骤1,假设您正在使用的WinForms ...

Here's a shot at step 1, assumes you're working in 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事件code来呈现网页到适当的位图,同时跟踪你渲染页面的数量。不要忘了重构PagePrint事件给接受一个Graphics对象,因此它可以用于印刷,previewing和页码编排一个独立的程序。不要忘记处理图形对象和位图

Step 2 is "simply" using the returned Reference Graphics Object with your already implemented PagePrint event code to render the pages to an appropriate bitmap while keeping track of the number of pages you're rendering. Don't forget to refactor your PagePrint Event to a separate routine that accepts a Graphics object so it can be used for Printing, Previewing and Page Numbering. Don't forget to dispose of the Graphics object and the 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

这篇关于如何preprocess的PrintDocument打印之前计算的总页数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆