You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET打印信封时无法显示Windows系统打印预览求助

解决Windows系统打印预览空白与VB.NET双重预览问题

核心问题原因

  1. 系统预览空白:仅设置PageSettings边距,未指定纸张尺寸(PaperSize)。Windows系统打印预览依赖打印机驱动的纸张定义,缺少有效尺寸参数时,驱动无法渲染预览内容,仅在实际打印时才触发PrintPage事件。
  2. 双重预览:使用VB.NET自带PrintPreviewDialog后,又调用PrintDialog.ShowDialog(),后者会触发系统级打印确认界面,导致重复预览。

解决方案

1. 为PageSettings添加明确的信封纸张尺寸

在每个信封尺寸分支中,补充设置对应PaperSize。标准信封用PaperKind枚举,自定义尺寸需创建PaperSize对象并标记为自定义类型。

修改后的完整代码:

Private Sub btn_PrintEnvelope_Click(sender As Object, e As EventArgs) Handles btn_PrintEnvelope.Click
    Dim PageSetup As New PageSettings

    If cb_EnvelopeSize.SelectedIndex <> -1 Then
        Select Case cb_EnvelopeSize.SelectedIndex
            Case 0  '~~ #10信封
                With PageSetup
                    .Margins.Left = 218
                    .Margins.Right = 253
                    .Margins.Top = 0
                    .Margins.Bottom = 100
                    .Landscape = False
                    ' 指定#10信封标准尺寸(单位:百分之一英寸)
                    .PaperSize = New PaperSize(PaperKind.Envelope10, 950, 412)
                End With
            Case 1  '~~ 6x9信封(自定义尺寸)
                Dim custom6x9 As New PaperSize("Custom 6x9", 900, 600)
                custom6x9.RawKind = CInt(PaperKind.Custom)
                With PageSetup
                    .Margins.Left = 125
                    .Margins.Right = 160
                    .Margins.Top = 0
                    .Margins.Bottom = 200
                    .Landscape = False
                    .PaperSize = custom6x9
                End With
            Case 2  '~~ A7信封
                With PageSetup
                    .Margins.Left = 163
                    .Margins.Right = 198
                    .Margins.Top = 5
                    .Margins.Bottom = 415
                    .Landscape = False
                    ' 指定A7信封标准尺寸
                    .PaperSize = New PaperSize(PaperKind.EnvelopeA7, 629, 445)
                End With
        End Select

        PrintDoc.DefaultPageSettings = PageSetup
        PrintDoc.DocumentName = "Client Envelope"

        PrintDialog.ShowHelp = True
        PrintDialog.Document = PrintDoc

        If PrintDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            PrintDoc.Print()
        End If
    End If
End Sub

2. 消除双重预览

若使用VB.NET的PrintPreviewDialog做预览,直接调用打印而非再弹系统对话框:

' 替换原PrintDialog逻辑
Dim previewDlg As New PrintPreviewDialog()
previewDlg.Document = PrintDoc
' 可设置预览窗口属性,比如大小
previewDlg.WindowState = FormWindowState.Maximized
If previewDlg.ShowDialog() = DialogResult.OK Then
    PrintDoc.Print()
End If

关键注意点

  • 纸张尺寸单位为百分之一英寸,需对应实际信封的英寸尺寸换算。
  • 自定义纸张必须设置RawKind = CInt(PaperKind.Custom),否则打印机驱动可能无法识别。
  • 确保打印机支持所选信封尺寸,部分打印机对自定义尺寸有额外限制。

内容的提问来源于stack exchange,提问作者Gary L Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 06:45:06