跳至页脚内容
使用 IRONPRINT

使用 IronPrint 在不打开 Adobe 的情况下通过 VB.NET 打印 PDF

在VB.NET中打印PDF文档过去是复杂的,通常需要Adobe Reader依赖或棘手的解决方案。 IronPrint通过提供一个简单、无依赖的解决方案改变了这种情况。 它可以在Windows、macOS和移动平台上运行。 在VB.NET中,PDF打印变得像调用一个单一的方法那么简单。

本指南展示了如何静默打印、显示Windows打印对话框、自定义设置以及在VB.NET中管理多个打印机。

如何开始使用IronPrint?

通过NuGet包管理器安装IronPrint只需几秒钟。 在 Visual Studio 中打开包管理器控制台并运行:

Install-Package IronPrint
Install-Package IronPrint
SHELL

安装完成后,按照以下代码片段所示,导入命名空间并应用许可证密钥来配置您的项目:

Imports IronPrint
' Apply your license key (get a free trial key from Iron Software website)
License.LicenseKey = "YOUR-LICENSE-KEY"
Imports IronPrint
' Apply your license key (get a free trial key from Iron Software website)
License.LicenseKey = "YOUR-LICENSE-KEY"
VB .NET

IronPrint支持.NET Framework 4.6.2+ 以及所有现代.NET版本(5, 6, 7, 8+),确保与传统和最新VB.NET PDF打印项目的兼容性。 该库通过其统一的打印 API支持无缝处理 PDF、PNG、HTML、TIFF、GIF、JPEG 和 BMP 格式。

如何在VB.NET中静默打印PDF文档?

静默打印实现了自动打印PDF文档而无需用户交互,完美适用于自动化工作流程和批量处理。 根据微软关于.NET中打印的文档,传统的方法需要复杂的PrintDocument实现。 以下是使用IronPrint打印PDF文件的方法:

Imports IronPrint
Module PrintingExample
    Sub Main()
        Dim pdfPath As String = "invoice.pdf"
        ' Print PDF to default printer in VB.NET
        Printer.Print(pdfPath)
        ' Create a PrintSettings object
        Dim settings As New PrintSettings()
        settings.PrinterName = "Microsoft Print to PDF" ' exact printer name
        ' Print PDF to a specific printer programmatically
        Printer.Print(pdfPath, settings)
    End Sub
End Module
Imports IronPrint
Module PrintingExample
    Sub Main()
        Dim pdfPath As String = "invoice.pdf"
        ' Print PDF to default printer in VB.NET
        Printer.Print(pdfPath)
        ' Create a PrintSettings object
        Dim settings As New PrintSettings()
        settings.PrinterName = "Microsoft Print to PDF" ' exact printer name
        ' Print PDF to a specific printer programmatically
        Printer.Print(pdfPath, settings)
    End Sub
End Module
VB .NET

此代码展示了如何将加载的PDF文档直接发送到打印队列而不显示任何对话框。 该Print方法自动使用系统的默认打印机。 否则,在打印设置中设置特定打印机的名称并在打印PDF文件时使用它,可以让您精确指定使用哪台打印机。

IronPrint在内部处理所有复杂的PDF渲染,消除了对Adobe Acrobat Reader或其他外部依赖的需求。 对于更高级的场景,请查看打印设置文档

如何在打印前显示打印对话框?

有时用户需要在将文档发送到打印机前控制打印设置。 IronPrint的ShowPrintDialog方法显示熟悉的Windows打印对话框。 这允许您选择要用于您的打印工作的打印机。

Imports IronPrint
Module DialogPrinting
    Sub Main()
        ' Show print dialog for PDF printing in VB.NET
        Printer.ShowPrintDialog("report.pdf")
    End Sub
End Module
Imports IronPrint
Module DialogPrinting
    Sub Main()
        ' Show print dialog for PDF printing in VB.NET
        Printer.ShowPrintDialog("report.pdf")
    End Sub
End Module
VB .NET

这种方法通过标准的Windows界面为用户提供了对打印机选择、页范围、份数和其他设置的完全控制。 该方法在用户打印或取消对话框后返回,使其容易集成到现有的Windows Forms应用程序。 有关更多对话框选项,请参阅打印对话框示例

使用 IronPrint 在不打开 Adobe 的情况下通过 VB.NET 打印 PDF:图 1 - 打印对话框

如何自定义PDF打印设置?

IronPrint通过PrintSettings类提供了对打印设置的详细控制。 您可以通过编程方式为您的VB.NET PDF打印需求配置方向、DPI、份数等:

Imports IronPrint
Module CustomPrintSettings
    Sub Main()
        ' Create custom print settings for PDF printing in VB.NET
        Dim settings As New PrintSettings() With {
            .Dpi = 300,
            .NumberOfCopies = 2,
            .PaperOrientation = PaperOrientation.Landscape,
            .PaperSize = PaperSize.A4,
            .PrinterName = "Office Printer"
        }
        ' Apply settings when printing PDF programmatically
        Printer.Print("document.pdf", settings)
    End Sub
End Module
Imports IronPrint
Module CustomPrintSettings
    Sub Main()
        ' Create custom print settings for PDF printing in VB.NET
        Dim settings As New PrintSettings() With {
            .Dpi = 300,
            .NumberOfCopies = 2,
            .PaperOrientation = PaperOrientation.Landscape,
            .PaperSize = PaperSize.A4,
            .PrinterName = "Office Printer"
        }
        ' Apply settings when printing PDF programmatically
        Printer.Print("document.pdf", settings)
    End Sub
End Module
VB .NET

这些设置为您提供了对PDF打印过程的完全控制。 Dpi属性确保专业文档的高质量输出,而NumberOfCopies消除了手动循环的需要。 设置PaperOrientation和PaperSize可确保PDF文件无论其原始格式如何,均能正确打印。 了解更多高级打印定制信息

如何选择和管理打印机?

IronPrint通过GetPrinterNames方法使打印机发现和选择变得简单:

Imports IronPrint
Module PrinterManagement
    Sub Main()
        ' Get all available printers for VB.NET PDF printing
        Dim printers As List(Of String) = Printer.GetPrinterNames()
        ' Display available printers
        For Each printerName As String In printers
            Console.WriteLine($"Found printer: {printerName}")
        Next
        ' Print PDF to first available printer
        If printers.Count > 0 Then
            Printer.PrintToPrinter("document.pdf", printers(0))
        End If
    End Sub
End Module
Imports IronPrint
Module PrinterManagement
    Sub Main()
        ' Get all available printers for VB.NET PDF printing
        Dim printers As List(Of String) = Printer.GetPrinterNames()
        ' Display available printers
        For Each printerName As String In printers
            Console.WriteLine($"Found printer: {printerName}")
        Next
        ' Print PDF to first available printer
        If printers.Count > 0 Then
            Printer.PrintToPrinter("document.pdf", printers(0))
        End If
    End Sub
End Module
VB .NET

此功能在多打印机环境中支持动态打印机选择,使应用程序能够根据可用性或文档类型智能路由文档。 了解更多打印机信息功能

使用 IronPrint 无需打开 Adobe 即可在 VB.NET 中打印 PDF:图 2 - 与使用 IronPrint 无需打开 Adobe 即可在 VB.NET 中打印 PDF相关的 3 张图片中的第 2 张

常见问题和解决方案是什么?

在VB.NET中实现PDF打印时,开发人员经常遇到这些常见问题:

问题:"找不到打印机"错误

使用GetPrinterNames()验证打印机名称是否完全匹配。 打印机名称区分大小写,必须精确匹配Windows注册表项。

问题:PDF打印为空白页

确保PDF文件路径正确且文件未损坏。 使用绝对路径以提高可靠性,并在打印前验证文件可访问性。

问题:打印质量差

PrintSettings中的 DPI 设置提高到 300 或更高,以获得专业品质的输出。 默认设置可能会使用较低分辨率以加快处理速度。

问题:访问被拒绝错误

用适当权限运行您的应用程序,并确保用户帐户对目标打印机有打印权限。

' Robust error handling for VB.NET PDF printing
Try
    If System.IO.File.Exists("document.pdf") Then
        Printer.Print("document.pdf")
    Else
        MessageBox.Show("PDF file not found")
    End If
Catch ex As Exception
    MessageBox.Show($"Printing failed: {ex.Message}")
End Try
' Robust error handling for VB.NET PDF printing
Try
    If System.IO.File.Exists("document.pdf") Then
        Printer.Print("document.pdf")
    Else
        MessageBox.Show("PDF file not found")
    End If
Catch ex As Exception
    MessageBox.Show($"Printing failed: {ex.Message}")
End Try
VB .NET

使用 IronPrint 在不打开 Adobe 的情况下通过 VB.NET 打印 PDF:图 3 - 示例错误

如需更多故障排除帮助,请参阅综合故障排除指南或联系Iron Software 的技术支持

结论

IronPrint 简化了VB.NET中的 PDF 打印,将复杂的挑战转化为简单的任务。 只需几行代码,您就可以实现静默打印、显示打印对话框、自定义设置和管理多个打印机。 为了完整的文档工作流程,IronPrint无缝集成IronPDF,一个强大的PDF库,允许您以编程方式生成PDF并立即打印它们,全部在同一个应用程序中。

准备简化您VB.NET应用程序中的PDF打印? 立即开始您的免费试用,体验具有全面技术支持的专业级打印功能。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。