跳過到頁腳內容
使用 IRONPRINT

如何使用 IronPrint 在 VB.NET 中打印 PDF

過去在.NET中列印 PDF 文件很複雜,通常需要 Adob​​e Reader 依賴項或巧妙的變通方法。 IronPrint透過提供一個簡單、無依賴的解決方案改變了這種情況。 它可在 Windows、macOS 和行動平台上運作。 在.NET中,PDF列印變得像呼叫一個方法一樣簡單。

本指南展示如何在.NET中實現靜默列印、顯示 Windows 列印對話方塊、自訂設定以及管理多台印表機。

如何開始使用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"
$vbLabelText   $csharpLabel

IronPrint支援.NET Framework 4.6.2+ 和所有現代.NET版本(5、6、7、8+),確保與傳統和前沿的 VB .NET PDF 列印專案相容。 該程式庫透過其統一的列印 API支援無縫處理 PDF、PNG、HTML、TIFF、GIF、JPEG 和 BMP 格式。

如何在.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
$vbLabelText   $csharpLabel

這段程式碼展示如何將載入的 PDF 文件直接傳送到列印佇列,而不顯示任何對話方塊。 列印方式會自動使用系統的預設印表機。 否則,在列印設定中設定特定的印表機名稱,並在列印 PDF 檔案時使用該名稱,即可精確指定要使用的印表機。

IronPrint在內部處理所有複雜的 PDF 渲染,無需 Adob​​e 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
$vbLabelText   $csharpLabel

這種方法使用戶能夠透過標準的 Windows 介面完全控製印表機選擇、頁面範圍、份數和其他設定。 使用者列印或取消對話方塊後,此方法會返回,因此很容易整合到現有的 Windows Forms 應用程式中。 如需更多對話方塊選項,請參閱列印對話方塊範例

如何在.NET使用IronPrint列印PDF:圖1 - 列印對話框

如何自訂PDF列印設定?

IronPrint透過 PrintSettings 類別提供對列印設定的精細控制。 您可以根據.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
$vbLabelText   $csharpLabel

這些設定讓您可以完全控制 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 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
$vbLabelText   $csharpLabel

此功能可在多印表機環境中實現動態印表機選擇,使應用程式能夠根據可用性或文件類型智慧地將文件路由到適當的印表機。 了解更多印表機資訊功能

如何在.NET使用IronPrint列印PDF:圖2

常見問題及解決方法有哪些?

在.NET中實作PDF列印時,開發人員經常會遇到以下常見問題:

問題:"找不到印表機"錯誤

使用 GetPrinterNames() 驗證印表機名稱是否完全符合。 印表機名稱區分大小寫,且必須與 Windows 登錄項目完全相符。

問題:PDF 列印出來是空白頁

請確保PDF檔案路徑正確且文件未損壞。 為了確保可靠性,請使用絕對路徑,並在列印前驗證文件可訪問性。

問題:列印品質差

將列印設定中的 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
$vbLabelText   $csharpLabel

如何在.NET使用IronPrint列印PDF:圖3 - 範例錯誤

如需更多故障排除協助,請參閱綜合故障排除指南或聯絡Iron Software 的技術支援

結論

IronPrint簡化了.NET中的 PDF 列印,將複雜的挑戰轉化為簡單的任務。 只需幾行程式碼,即可實現靜默列印、顯示列印對話方塊、自訂設定和管理多台印表機。 為了實現完整的文件工作流程, IronPrint與功能強大的 PDF 庫IronPDF無縫集成,讓您以程式方式產生 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應用程序提供靈活性。

與其他PDF打印解決方案相比,IronPrint有何獨特之處?

IronPrint提供了簡單高效的PDF打印解決方案,無需Adobe Reader或復雜的替代方案,使得使用VB.NET的開發者可以輕鬆使用。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me