使用 IronPrint 在不開啟 Adobe 的情況下透過 VB.NET 列印 PDF
過去在 VB.NET 中列印 PDF 文件很複雜,通常需要 Adobe Reader 依賴項或巧妙的變通方法。 IronPrint透過提供一個簡單、無依賴的解決方案改變了這種情況。 它可在 Windows、macOS 和行動平台上運作。 在 VB.NET 中列印 PDF 檔案就像呼叫一個方法一樣簡單。
本指南展示如何在 VB.NET 中實現靜默列印、顯示 Windows 列印對話方塊、自訂設定以及管理多台印表機。
如何開始使用 IronPrint?
透過NuGet 套件管理器安裝 IronPrint 只需幾秒鐘。 在 Visual Studio 中開啟程式包管理器控制台並執行:
Install-Package IronPrintInstall-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 文檔,非常適合自動化工作流程和批次處理。 根據微軟關於 .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 ModuleImports 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 文件直接傳送到列印佇列,而不顯示任何對話方塊。 列印方式會自動使用系統的預設印表機。 否則,在列印設定中設定特定的印表機名稱,並在列印 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 ModuleImports 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 ModuleImports 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 屬性則可消除手動循環列印的需要。 設定紙張方向和紙張大小可確保 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 ModuleImports 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 與功能強大的 PDF 庫IronPDF無縫集成,讓您以程式方式產生 PDF 並立即列印,所有操作均可在同一個應用程式中完成。
準備好簡化VB.NET應用程式中的PDF列印流程了嗎? 立即開始免費試用,體驗專業級列印功能和全面的技術支援。
常見問題解答
如何在不使用 Adobe Reader 的情況下,用 VB.NET 列印 PDF 文件?
使用 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 列印提供了一個簡單、跨平台且無依賴項的解決方案,提高了效率和易用性。







