跳至页脚内容
使用 IRONPRINT

如何使用 IronPrint 在 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

安装后,通过导入命名空间并按照我们在下面的代码片段中展示的,应用您的许可证密钥来配置您的项目。

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"
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

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
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

此代码展示了如何将加载的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
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

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

如何使用IronPrint在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
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

这些设置为您提供了对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
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

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

如何使用IronPrint在VB.NET中打印PDF:图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
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

如何使用IronPrint在VB.NET中打印PDF:图3 - 错误示例

有关额外的故障排查帮助,请参阅综合故障排查指南或联系Iron Software的技术支持

结论

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

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

常见问题解答

如何使用VB.NET打印PDF文档?

使用IronPrint,在VB.NET中打印PDF文档非常简单。您只需调用一个方法即可,无需担心Adobe Reader等依赖项。

IronPrint需要任何外部依赖吗?

不,IronPrint为PDF打印提供了一个无依赖方案,使其更容易集成到您的.NET应用程序中。

IronPrint是否兼容macOS的PDF打印?

是的,IronPrint可以无缝地在Windows、macOS和移动平台上运行,使您可以在不同操作系统中用VB.NET打印PDF。

我可以在VB.NET中使用IronPrint自定义打印设置吗?

是的,IronPrint允许您实现自定义打印设置,包括静默打印和对话框选项,为您的.NET应用程序提供灵活性。

IronPrint与其他PDF打印解决方案相比有什么独特之处?

IronPrint提供了一种简单高效的PDF打印解决方案,不需要Adobe Reader或复杂的解决方案,使使用VB.NET的开发人员更容易访问。

Curtis Chau
技术作家

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

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