using IronPrint 在不開啟 Adobe 的情況下透過 VB.NET 列印 PDF
在VB.NET中列印PDF文件過去比較複雜,通常需要依賴Adobe Reader或使用麻煩的解決方案。 IronPrint 透過提供一個簡單、不需依賴其他軟體的解決方案來改變這一點。 它可以在Windows、macOS和移動平台上運行。 在VB.NET中列印PDF變得如同呼叫一個方法一樣簡單。
本指南將展示如何靜默列印、顯示Windows列印對話框、自定義設定及在VB.NET中管理多台列印機。
如何開始使用IronPrint?
通過NuGet Package Manager安裝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"
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文件而不需要使用者交互,非常適合自動化工作流程和批量處理。 根據Microsoft對於.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
此程式碼展示了如何直接將載入的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
這種方法讓使用者能夠通過標準Windows介面,對列印機選擇、頁面範圍、拷貝數量和其他設置進行全面控制。 此方法在使用者列印或取消對話框後返回,便於整合到現有的Windows Forms應用程式中。 有關更多對話框選項,請參見列印對話框範例。

如何自定義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
這些設置讓您完全控制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中實現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 Software 的技術支援。
結論
IronPrint 簡化了 VB.NET 中的 PDF 列印,將複雜的挑戰轉變為簡單的任務。 只需幾行程式碼,您就可以實現靜默列印,顯示列印對話框,自定義設置和管理多台列印機。 為了完整的文件工作流程,IronPrint與IronPDF無縫整合,這是一個功能強大的PDF程式庫,允許您以程式化方式生成PDF並立即列印,都在同一應用中的進行。
準備好在您的VB.NET應用程式中簡化PDF列印了嗎? 立即開始免費試用,體驗專業級列印能力,並享有全方位技術支援。
常見問題
如何在VB.NET中列印PDF而不使用Adobe Reader?
使用IronPrint,您可以直接在VB.NET中列印PDF,而無需Adobe Reader。它提供了一個簡單的、無依賴的解決方案來簡化過程。
IronPrint支援哪些平台上的PDF列印?
IronPrint支援在Windows、macOS和移動平台上進行PDF列印,適用於不同環境的多樣性。
是否可以使用IronPrint實施靜默列印?
是的,IronPrint允許進行靜默列印,您可以在不需要使用者交互或列印對話框的情況下列印PDF。
我可以在 VB.NET 中使用 IronPrint 自定義列印設置嗎?
IronPrint支持列印設置的自訂,讓您可以定制列印過程以適應您的具體需求。
IronPrint是否需要任何其他軟體依賴?
不,IronPrint為在VB.NET中列印PDF提供了無依賴的解決方案,消除了像Adobe Reader這樣的其他軟體需求。
將IronPrint整合到我現有的VB.NET應用程式中有多容易?
將IronPrint整合到您的VB.NET應用程式中非常簡單,只需通過調用一個方法即可處理PDF列印。
在.NET應用程式中使用IronPrint進行PDF列印有哪些好處?
IronPrint提供了一個簡單、跨平台且無依賴的解決方案,在.NET應用程式中進行PDF列印,提高了效率和使用方便性。


