VB.NET中如何在PrintDialog对话框内实现打印预览?
VB.NET PrintDialog内置预览失效问题
我在VB.NET里想让PrintDialog对话框自带打印预览功能,但预览区域一直显示**"This app doesn't support print preview"**(如下图)。目前打印功能能正常运行,但搜到的解决方案全是让用PrintPreviewDialog,这不是我想要的实现方式。

我的相关代码如下:
打印触发方法
Private Sub PrintReplacementCostReport(Optional ByVal Title As String = "Replacement Cost Report") Dim Caller As String = GetCurrentMethod.DeclaringType.Name & "." & GetCurrentMethod.Name 'ClsDebug.LogProcEnter(New StackFrame(0, True).GetFileLineNumber, Caller) ClearErrorMessage() m_PageTitle = "Replacement Cost Report" ModGlobals.ReportRichTextBox = Me.rtfReport If Me.rtfReport.Lines.Count > 0 Then _currentRecord = 1 '-- Skip the heading SetPrintDocumentCommonSettings(m_PageTitle) SetPrintDialogCommonSettings() If PrintDialog1.ShowDialog = DialogResult.OK Then ClearErrorMessage() PrintDocument1.Print() End If ClearErrorMessage() Else If m_PageTitle = "Replacement Cost Report" Then Report("You Must Run a [Replacement Cost] Report Before Printing") End If End If 'ClsDebug.LogProcExit(New StackFrame(0, True).GetFileLineNumber, Caller) End Sub
PrintDialog配置方法
Private Sub SetPrintDialogCommonSettings(Optional ByVal ToFile As Boolean = False) With PrintDialog1 .Document = PrintDocument1 .PrinterSettings = PrintDocument1.PrinterSettings .AllowSomePages = True .AllowSelection = True .AllowPrintToFile = True .AllowCurrentPage = True If ToFile Then .PrinterSettings.PrinterName = "Microsoft Print to PDF" .PrintToFile = True .PrinterSettings.PrintFileName = "PrintDialogToFile.pdf" End If End With End Sub
PrintDocument配置方法
Private Sub SetPrintDocumentCommonSettings(ByVal Title As String, Optional ByVal ToFile As Boolean = False, Optional ByVal ToFileName As String = "") With PrintDocument1 .DocumentName = Title .PrinterSettings.Copies = 1 .PrinterSettings.PrintToFile = True .DefaultPageSettings.Margins.Right = 50 .DefaultPageSettings.Margins.Left = 50 .DefaultPageSettings.Margins.Top = 50 .DefaultPageSettings.Margins.Bottom = 50 If ToFile Then .DefaultPageSettings.PrinterSettings.PrintToFile = True .DefaultPageSettings.PrinterSettings.PrinterName = "Microsoft Print to PDF" .DefaultPageSettings.PrinterSettings.PrintFileName = "Roosevelt Dimes.pdf" End If .PrinterSettings.DefaultPageSettings.Margins.Left = 30 '50 .PrinterSettings.DefaultPageSettings.Margins.Right = 30 '50 .PrinterSettings.DefaultPageSettings.Margins.Top = 50 .PrinterSettings.DefaultPageSettings.Margins.Bottom = 50 yPos = .PrinterSettings.DefaultPageSettings.Margins.Top End With m_RememberSubTitle = String.Empty m_RememberTitle = String.Empty ModGlobals.WhiteSpace = False m_Landscape = False _PageChanged = True _currentPage = 0 End Sub
解决方案
首先明确:.NET WinForms原生的PrintDialog本身并不自带预览功能,你看到的预览区域是系统级打印对话框的一部分,它需要应用程序提供完整的打印渲染逻辑,且依赖打印机驱动支持才能显示预览。
具体修复步骤:
- 完善PrintDocument的PrintPage事件实现
系统预览完全依赖这个事件的渲染逻辑生成预览内容,你的代码里没贴这部分,这大概率是预览失效的核心原因。示例实现(适配你的RichTextBox内容):
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage ' 从RichTextBox获取内容并渲染到打印画布 Dim content As String = ModGlobals.ReportRichTextBox.Text Dim printFont As New Font("Arial", 10) Dim lineHeight As Single = printFont.GetHeight(e.Graphics) Dim yPosition As Single = e.MarginBounds.Top ' 逐行绘制内容 For Each line As String In content.Split(vbCrLf) If yPosition + lineHeight > e.MarginBounds.Bottom Then e.HasMorePages = True Return End If e.Graphics.DrawString(line, printFont, Brushes.Black, e.MarginBounds.Left, yPosition) yPosition += lineHeight Next e.HasMorePages = False End Sub
- 启用系统增强型打印对话框
修改SetPrintDialogCommonSettings方法,开启UseEXDialog属性,让PrintDialog调用系统增强版对话框,提升预览兼容性:
Private Sub SetPrintDialogCommonSettings(Optional ByVal ToFile As Boolean = False) With PrintDialog1 .Document = PrintDocument1 .PrinterSettings = PrintDocument1.PrinterSettings .AllowSomePages = True .AllowSelection = True .AllowPrintToFile = True .AllowCurrentPage = True .UseEXDialog = True ' 启用系统增强对话框,优化预览支持 If ToFile Then .PrinterSettings.PrinterName = "Microsoft Print to PDF" .PrintToFile = True .PrinterSettings.PrintFileName = "PrintDialogToFile.pdf" End If End With End Sub
- 额外注意点
- 部分打印机驱动本身不支持系统级预览,即使代码正确也可能显示不支持提示,这种情况只能换用
PrintPreviewDialog。 - 如果上述方法仍无效,.NET WinForms目前没有官方方案让
PrintDialog强制显示预览,只能考虑自定义一个包含预览和打印选项的复合对话框。
内容的提问来源于stack exchange,提问作者E Bell
相关产品推荐
相关产品推荐

