如何在不使用 Adobe 的情況下,使用 IronPrint 在 VB.NET 中列印 PDF 文件
在VB.NET中列印PDF文件需要一個能處理渲染、列印機通信和設定管理的程式庫,而不需要您安裝Adobe Reader或其他第三方依賴項。 IronPrint 使用簡潔的API解決了此問題,適用於Windows、macOS、Linux和移動平台。 本指南將帶您完成每個步驟——從安裝套件到處理常見列印錯誤——讓您能夠快速且自信地將PDF列印新增到您的VB.NET應用程式中。
在本教程結束後,您將知道如何靜默列印、顯示Windows列印對話框、應用自訂設定如DPI和紙張方向、枚舉可用的列印機,並優雅地處理例外。
如何在VB.NET專案中安裝IronPrint?
通過NuGet Package Manager安裝IronPrint只需幾秒鐘。 在Visual Studio中打開您的套件管理員控制台,然後執行:
Install-Package IronPrint
安裝完成後,在需要列印的任何文件頂部新增命名空間導入,然後在調用任何IronPrint方法之前應用您的授權金鑰:
Imports IronPrint
' Apply your license key (get a free trial key at ironsoftware.com)
License.LicenseKey = "YOUR-LICENSE-KEY"
Imports IronPrint
' Apply your license key (get a free trial key at ironsoftware.com)
License.LicenseKey = "YOUR-LICENSE-KEY"
Imports IronPrint
' Apply your license key (get a free trial key at ironsoftware.com)
License.LicenseKey = "YOUR-LICENSE-KEY"
IronPrint支持.NET Framework 4.6.2及更高版本,以及.NET 6、7、8、9和10。它為每個平台提供了本機渲染引擎,因此您不需要安裝Adobe Acrobat、Ghostscript或其他任何PDF渲染器。 相同的NuGet套件適用於用VB.NET撰寫的Windows Forms、WPF、控制台應用程式和ASP.NET專案。
支持的文件格式包括PDF、PNG、HTML、TIFF、GIF、JPEG和BMP。您可以在IronPrint統一列印API參考中探索完整的功能。
有關授權和啟用的更多資訊,請存取IronPrint授權金鑰文件。
如何在VB.NET中靜默列印PDF?
靜默列印將文件直接發送到列印機而不顯示任何對話框。 此模式對於自動化工作流、批量發票處理、報告生成管道和無使用者在場的伺服器端文件列印至關重要。
根據Microsoft關於.NET中GDI+列印的文件,傳統的.NET列印涉及建立PrintPage事件並手動處理頁面佈局。 IronPrint消除了所有那些樣板:
Imports IronPrint
Module SilentPrinting
Sub Main()
Dim pdfPath As String = "invoice.pdf"
' Print to the system default printer
Printer.Print(pdfPath)
' Print to a specific printer by name
Dim settings As New PrintSettings()
settings.PrinterName = "Microsoft Print to PDF"
Printer.Print(pdfPath, settings)
End Sub
End Module
Imports IronPrint
Module SilentPrinting
Sub Main()
Dim pdfPath As String = "invoice.pdf"
' Print to the system default printer
Printer.Print(pdfPath)
' Print to a specific printer by name
Dim settings As New PrintSettings()
settings.PrinterName = "Microsoft Print to PDF"
Printer.Print(pdfPath, settings)
End Sub
End Module
Imports IronPrint
Module SilentPrinting
Sub Main()
Dim pdfPath As String = "invoice.pdf"
' Print to the system default printer
Printer.Print(pdfPath)
' Print to a specific printer by name
Dim settings As New PrintSettings()
settings.PrinterName = "Microsoft Print to PDF"
Printer.Print(pdfPath, settings)
End Sub
End Module
Printer.Print方法在內部處理所有的PDF渲染。 當您不帶Printer.Print(pdfPath)時,IronPrint將自動選擇系統預設列印機。 當您提供一個帶有PrintSettings物件時,它將任務路由到指定的裝置。
對於批量場景,您可以遍歷文件路徑列表並為每個文件調用Printer.Print。 由於IronPrint不會為靜默列印打開任何UI執行緒,它可以正確地在控制台應用程式、Windows服務和ASP.NET請求處理器中運行。
有關可用選項的更深入了解,請閲讀IronPrint列印設定文件。
如何在列印前顯示列印對話框?
有時您需要讓使用者在提交列印任務之前,選擇列印機、調整頁面範圍或設定副本數量。 IronPrint提供了Printer.ShowPrintDialog來專門應對這種情況:
Imports IronPrint
Module DialogPrinting
Sub Main()
' Display the standard Windows print dialog
Printer.ShowPrintDialog("report.pdf")
End Sub
End Module
Imports IronPrint
Module DialogPrinting
Sub Main()
' Display the standard Windows print dialog
Printer.ShowPrintDialog("report.pdf")
End Sub
End Module
Imports IronPrint
Module DialogPrinting
Sub Main()
' Display the standard Windows print dialog
Printer.ShowPrintDialog("report.pdf")
End Sub
End Module
當此方法運行時,Windows顯示其標準列印對話框。 使用者可以選擇任何已安裝的列印機、選擇頁面範圍、選擇副本數量並調整雙面設定。 在使用者點擊列印或取消後,控制權返回給您的程式碼。
此方法自然地整合到Windows Forms應用程式中。 您可以從按鈕點擊處理程式中調用它,並相信該對話框將一如使用者所知的其他Windows應用程式一致地運行。

提供了預選列印機或在對話框開啟之前預先填充副本數量等使用案例,請參閱列印對話框範例頁面。
如何在VB.NET中自訂PDF列印設定?
PrintSettings類使您可以完全以程式化方式控制列印作業的每個方面。 您可以設定DPI、紙張大小、方向、副本數量以及目標列印機——這些都是在作業到達列印隊列之前設定:
Imports IronPrint
Module CustomPrintSettings
Sub Main()
Dim settings As New PrintSettings() With {
.Dpi = 300,
.NumberOfCopies = 2,
.PaperOrientation = PaperOrientation.Landscape,
.PaperSize = PaperSize.A4,
.PrinterName = "Office Printer"
}
Printer.Print("document.pdf", settings)
End Sub
End Module
Imports IronPrint
Module CustomPrintSettings
Sub Main()
Dim settings As New PrintSettings() With {
.Dpi = 300,
.NumberOfCopies = 2,
.PaperOrientation = PaperOrientation.Landscape,
.PaperSize = PaperSize.A4,
.PrinterName = "Office Printer"
}
Printer.Print("document.pdf", settings)
End Sub
End Module
Imports IronPrint
Module CustomPrintSettings
Sub Main()
Dim settings As New PrintSettings() With {
.Dpi = 300,
.NumberOfCopies = 2,
.PaperOrientation = PaperOrientation.Landscape,
.PaperSize = PaperSize.A4,
.PrinterName = "Office Printer"
}
Printer.Print("document.pdf", settings)
End Sub
End Module
了解主要PrintSettings屬性
Dpi屬性控制輸出解析度。 300是專業文件的標準值; 600適合高質量圖形或工程圖紙。 較低的值如150對於速度比質量更重要的草稿列印是有用的。
NumberOfCopies告訴列印機在單一作業中產生多少副本。 將此值設為3相當於使用者按Ctrl+P並在副本欄位中輸入3——但您從程式碼中控制而無需任何UI互動。
PaperOrientation.Landscape。 為設計為橫向模式的PDF選擇錯誤的方向將導致內容被裁切或縮小。 明確地設定此值可確保輸出與文件設計相匹配。
PaperSize.Legal。 當您的列印環境混合裝載不同尺寸的紙張托盤時,這一點很重要。
選擇正確的列印機名稱
PrinterName字串必須與Windows中列印機的註冊名稱完全匹配,包括大小寫和空格。 使用Printer.GetPrinterNames()(在下一節中介紹)在運行時檢索有效名稱列表。
有關更高級的自訂選項,請存取高級列印自訂功能頁面。
如何程式化地枚舉和選擇列印機?
在企業環境中,工作站通常可以存取數十台網路列印機。 IronPrint使您可以輕鬆列出所有可用列印機並在運行時選擇合適的列印機:
Imports IronPrint
Module PrinterManagement
Sub Main()
' Retrieve all available printers
Dim printers As List(Of String) = Printer.GetPrinterNames()
Console.WriteLine($"Found {printers.Count} printer(s):")
For Each printerName As String In printers
Console.WriteLine($" - {printerName}")
Next
' Print to the first available printer
If printers.Count > 0 Then
Dim settings As New PrintSettings()
settings.PrinterName = printers(0)
Printer.Print("document.pdf", settings)
Else
Console.WriteLine("No printers found. Check system configuration.")
End If
End Sub
End Module
Imports IronPrint
Module PrinterManagement
Sub Main()
' Retrieve all available printers
Dim printers As List(Of String) = Printer.GetPrinterNames()
Console.WriteLine($"Found {printers.Count} printer(s):")
For Each printerName As String In printers
Console.WriteLine($" - {printerName}")
Next
' Print to the first available printer
If printers.Count > 0 Then
Dim settings As New PrintSettings()
settings.PrinterName = printers(0)
Printer.Print("document.pdf", settings)
Else
Console.WriteLine("No printers found. Check system configuration.")
End If
End Sub
End Module
Imports IronPrint
Module PrinterManagement
Sub Main()
' Retrieve all available printers
Dim printers As List(Of String) = Printer.GetPrinterNames()
Console.WriteLine($"Found {printers.Count} printer(s):")
For Each printerName As String In printers
Console.WriteLine($" - {printerName}")
Next
' Print to the first available printer
If printers.Count > 0 Then
Dim settings As New PrintSettings()
settings.PrinterName = printers(0)
Printer.Print("document.pdf", settings)
Else
Console.WriteLine("No printers found. Check system configuration.")
End If
End Sub
End Module
Printer.GetPrinterNames()查詢Windows列印子系統並返回當前使用者可以存取的每台列印機,包括本地USB列印機、網路列印機、虛擬列印機如Microsoft Print to PDF、以及通過第三方軟體安裝的任何列印機驅動程式。
將文件路由到正確的列印機
在多列印機設置中,您可能希望將大型文件路由到高速部門列印機而小型文件路由到附近的桌面裝置。您可以通過檢查文件的頁數或文件大小來實現這種邏輯,然後從列表中選擇適當的列印機名稱。

閱讀列印機資訊功能文件,了解您可以查詢的額外屬性,如列印機狀態和預設紙張托盤。
如何在VB.NET中處理列印錯誤?
生產印刷程式碼必須妥善處理故障。 可能出現的問題包括:文件路徑錯誤、列印機離線或使用者帳戶缺乏列印權限。 將您的列印呼叫封裝在結構化異常處理中可以防止未處理的異常崩潰您的應用程式:
Imports IronPrint
Imports System.IO
Module ErrorHandling
Sub Main()
Dim filePath As String = "document.pdf"
If Not File.Exists(filePath) Then
Console.WriteLine("PDF file not found. Verify the path and try again.")
Return
End If
Try
Dim settings As New PrintSettings()
settings.PrinterName = "Office Printer"
Printer.Print(filePath, settings)
Console.WriteLine("Print job sent successfully.")
Catch ex As Exception
Console.WriteLine($"Printing failed: {ex.Message}")
End Try
End Sub
End Module
Imports IronPrint
Imports System.IO
Module ErrorHandling
Sub Main()
Dim filePath As String = "document.pdf"
If Not File.Exists(filePath) Then
Console.WriteLine("PDF file not found. Verify the path and try again.")
Return
End If
Try
Dim settings As New PrintSettings()
settings.PrinterName = "Office Printer"
Printer.Print(filePath, settings)
Console.WriteLine("Print job sent successfully.")
Catch ex As Exception
Console.WriteLine($"Printing failed: {ex.Message}")
End Try
End Sub
End Module
Imports IronPrint
Imports System.IO
Module ErrorHandling
Sub Main()
Dim filePath As String = "document.pdf"
If Not File.Exists(filePath) Then
Console.WriteLine("PDF file not found. Verify the path and try again.")
Return
End If
Try
Dim settings As New PrintSettings()
settings.PrinterName = "Office Printer"
Printer.Print(filePath, settings)
Console.WriteLine("Print job sent successfully.")
Catch ex As Exception
Console.WriteLine($"Printing failed: {ex.Message}")
End Try
End Sub
End Module
常見錯誤及其修正方法
下表描述了最常見的列印錯誤及其解決方案:
| 錯誤 | 原因 | 解決方案 |
|---|---|---|
| 找不到列印機 | 列印機名稱與Windows註冊表項不匹配 | 調用Printer.GetPrinterNames()並使用返回的精確名稱 |
| 列印空白頁 | 文件路徑無效或PDF已損壞 | 使用File.Exists驗證路徑;在查看器中打開PDF以檢查完整性 |
| 列印質量差 | DPI設定過低 | 將PrintSettings.Dpi設置為300或更高 |
| 存取被拒絕 | 列印權限不足 | 使用具有目標列印機列印權限的使用者帳戶運行應用程式 |
| 列印機離線 | 網路或裝置連接問題 | 在Windows裝置和列印機中檢查列印機狀態;重新連接後重試 |

有關其他故障排除指南,請參閱IronPrint工程請求和疑難解答指南或聯繫Iron Software技術支援。
如何在VB.NET中結合PDF生成與列印?
一種常見的工作流程是在運行時生成PDF然後立即列印它——例如,從資料庫記錄生成發票並在一個步驟中將其發送到列印機。IronPDF負責PDF建立,IronPrint負責輸出:
為什麼要結合IronPDF和IronPrint?
IronPDF將HTML、ASPX和現有文件轉換為格式正確的PDF文件。 一旦PDF儲存在記憶體中或保存到磁碟,IronPrint將其發送到列印機而不需要Adobe Reader。 兩個程式庫共享相同的基於NuGet的部署模型,因此您只需新增兩個包並撰寫直接的VB.NET程式碼即可涵蓋整個生成和列印管道。
此方法特別適用於:
- 發票和收據列印——從HTML模板生成PDF,立即列印
- 報告生成——將資料庫查詢結果轉換為格式化PDF,按需列印
- 標籤和條碼列印——將IronBarcode用於條碼生成和IronPrint用於輸出相結合
- 批次文件處理——遍歷記錄,生成每個記錄一個PDF,依次列印每個
要了解如何在VB.NET中從HTML生成PDF,請存取IronPDF for VB.NET文件。
根據W3C的印刷媒體查詢規範,通過CSS @page 規則控制列印佈局是HTML到列印工作流程的標準方法。 IronPDF在渲染過程中遵循這些CSS規則,因此您生成的PDF將具有正確的邊距、頁面分隔及尺寸,在IronPrint將其發送至列印機之前。
如何列印多頁並控制頁面範圍?
在列印大型文件時,您可能只需要列印特定的頁面——例如,印刷50頁報告中的第1到5頁。 IronPrint的PrintSettings類支持頁面範圍設定:
設定頁面範圍
您可以在PrintSettings物件內指定起始頁和結束頁。 頁碼是以1為基準,符合使用者從標準列印對話框中所期望的習慣。 如果您省略了頁面範圍,IronPrint將列印整個文件。
頁面範圍印刷在高流量環境中特別有用,因為在重新列印受損單頁比重新列印整個文件更快。 結合NumberOfCopies,您可以精確控制要列印到輸出托盤的指定頁數。
有關包括頁面範圍的PrintSettings屬性的完整列表,請存取IronPrint列印設定使用指南。
在灰階中列印
有些列印機即使在文件不包含彩色內容時預設為彩色輸出,這會浪費碳粉或墨水。 您可以通過PrintSettings強制灰階輸出。 此設定在Windows和macOS目標上均可用,並適用於所有支持的文件格式,而不僅僅是PDF。
IronPrint功能概述列出了所有可用的列印設定,每個平台都有相應的範例。
您的下一步是什麼?
您現在擁有在VB.NET應用程式中進行PDF列印的基本元件:
- 靜默列印——
Printer.Print(path)用於自動化工作流程 - 對話框列印——
Printer.ShowPrintDialog(path)用於使用者控制作業 - 自訂設定——
PrintSettings用於DPI、方向、副本和列印機選擇 - 列印機枚舉——
Printer.GetPrinterNames()用於動態列印機路由 - 錯誤處理——結構化的例外處理和預檢文件驗證
- 結合工作流程——使用IronPDF進行生成,IronPrint進行輸出
要將這些能力付諸實踐,開始您的免費IronPrint試用並探索這些額外資源:
- IronPrint入門手冊——完整的設定指南
- 對話框印刷範例——對話框自訂模式
- 高級列印設定功能——完整設定參考
- 列印機資訊功能——查詢列印機屬性
- IronPrint API參考——完整的類和方法文件
- IronPrint疑難解答指南——解決常見問題
常見問題
什麼是IronPrint?
IronPrint是一個程式庫,透過消除對Adobe Reader和其他依賴的需要,簡化了在VB.NET中列印PDF文件的過程。
IronPrint如何促進VB.NET中的PDF列印?
IronPrint提供一種簡單的方法,使PDF能在多個平台上被列印,包括Windows、macOS和移動裝置。
我可以使用IronPrint進行靜默列印嗎?
可以,IronPrint支援靜默列印,允許PDF文件在不顯示對話框或需要使用者操作的情況下列印。
IronPrint與移動平台相容嗎?
是的,IronPrint設計用於跨越Windows、macOS和移動平台工作,在PDF列印應用中提供靈活性。
IronPrint需要任何外部依賴嗎?
不,IronPrint是一個無依賴的解決方案,意味著您不需要額外的軟體,如Adobe Reader來於VB.NET中列印PDF。
IronPrint相較於傳統PDF列印方法有哪些優勢?
IronPrint透過移除複雜的解決方法和外部依賴,提供了一個簡化且高效的PDF列印過程,使其更容易整合到.NET應用中。
我可以使用IronPrint自訂列印設定嗎?
可以,IronPrint提供透過PrintSettings類自訂列印設定的選項,允許開發者控制DPI、紙張方向、份數及印表機選擇。
IronPrint如何改善VB.NET應用程式中的PDF列印過程?
IronPrint透過提供一種單一、簡單易用的方法來列印多個平台的PDF文件,而不需要額外的軟體來改善PDF列印過程。
IronPrint適合跨平台開發嗎?
是的,IronPrint適合跨平台開發,因為它支援在Windows、macOS、Linux和移動平台上的列印。
IronPrint為開發者提供什麼樣的支持?
IronPrint透過詳細的文件、程式碼範例和技術支持存取,為開發者提供支援,確保順利的開發體驗。



