跳過到頁腳內容
使用 IRONPRINT

如何在 C# 中打印 PDF 文件不顯示對話框

本文探討了直接列印 PDF 的重要性,並示範了強大的 C# 庫 IronPDF 如何促進這一過程。

IronPDF - .NET Framework PDF 程式庫

IronPDF是一個功能強大的 C# 庫,旨在簡化 PDF 文件的建立、操作和互動。 透過 IronPDF,開發人員可以輕鬆地從 HTML 內容、圖像和其他來源產生 PDF,使其成為處理簡單和複雜 PDF 相關任務的多功能工具。 它的功能不僅限於生成 PDF 文件; IronPDF 還使您能夠以各種方式合併、分割和操作 PDF 文件。 此外,其直覺的 API 使其既適用於初學者,也適用於經驗豐富的開發人員。 一些重要的功能包括

直接PDF列印的重要性

處理 PDF 文件時,列印是一項基本需求。 直接從 C# 應用程式列印 PDF 檔案而無需顯示列印對話方塊具有以下幾個優點。 它透過消除不必要的互動來增強使用者體驗,自動執行列印任務,並可無縫整合到更大的工作流程中。 IronPDF 簡化了這個過程,使開發人員能夠在確保列印輸出完整性的同時,保持對列印過程的控制。

先決條件

在開始 PDF 列印流程之前,請確保已滿足以下先決條件:

  1. Visual Studio :它是一個整合開發環境(IDE),您可以在其中建立、編輯和編譯您的 C# 專案。 若要下載並將其安裝到您的電腦上,請造訪Visual Studio 官方網站
  2. IronPDFIronPDF庫,可以使用 NuGet 套件管理器輕鬆整合到您的專案中。

建立一個 Visual Studio 項目

建立 Visual Studio 控制台專案是一個簡單的過程。 請依照下列步驟使用 Visual Studio 建立新的控制台應用程式:

1.開啟 Visual Studio :啟動 Visual Studio IDE。 2.建立新專案:開啟 Visual Studio 後,按一下"建立新專案"。 3.選擇項目範本:在"建立新項目"視窗中,您將看到項目範本清單。 選擇 Visual C# 控制台應用程式。

[如何在 C# 中不彈出對話框列印 PDF 文件?圖 1:在 Visual Studio 中建立一個新的 C# 控制台應用程式項目](/static-assets/pdf/blog/csharp-print-pdf-file-without-dialog-tutorial/csharp-print-pdf-file-without-dialog-tutorial-1.webp)
**在 Visual Studio 中建立一個新的 C# 控制台應用程式項目**

4.設定項目詳情:選擇範本後,系統會提示您設定專案詳情。

[如何在 C# 中不彈出對話框列印 PDF 文件,圖 2:配置您的新項目](/static-assets/pdf/blog/csharp-print-pdf-file-without-dialog-tutorial/csharp-print-pdf-file-without-dialog-tutorial-2.webp)
**配置您的新項目**

5.配置其他設定:選擇具有長期支援的 .NET Framework。 IronPDF 支援最新版本的 .NET Framework。 6.建立專案:配置好專案詳細資料後,點選"建立"按鈕。 Visual Studio 將會建立專案並在 IDE 中開啟它。

透過 NuGet 安裝 IronPDF

您可以參考以下步驟在專案中安裝 IronPDF:

  1. 開啟 Visual Studio 和你的專案。
  2. 前往"工具"選單,選擇"NuGet 套件管理器",然後按一下"管理解決方案的 NuGet 套件"。

    如何在 C# 中不彈出對話框列印 PDF 文件?圖 3:導覽至 NuGet 套件管理器 導覽至 NuGet 套件管理器

  3. 在"瀏覽"標籤中,在搜尋框中搜尋"IronPDF"。

    如何在 C# 中不彈出對話框列印 PDF 文件?圖 4:在 NuGet 套件管理器 UI 中搜尋 IronPDF 在 NuGet 套件管理器 UI 中搜尋 IronPDF

  4. 點擊IronPdf套件並選擇它用於您的項目,然後點擊"安裝"按鈕。

IronPDF 無需對話方塊即可列印 - 逐步指南

導入IronPdf命名空間

程式碼首先匯入IronPdfSystem.Drawing.Printing命名空間,允許使用 IronPDF 庫中的類別和方法,以及 System 命名空間的繪圖和列印選項。

using IronPdf;
using System.Drawing.Printing;
using IronPdf;
using System.Drawing.Printing;
Imports IronPdf
Imports System.Drawing.Printing
$vbLabelText   $csharpLabel

使用 ChromePdfRenderer 建立 PDF 文檔

ChromePdfRenderer負責渲染網頁。 PdfDocument類表示 PDF 文檔,並提供對其執行各種操作(包括列印)的方法。 以下程式碼從 IronPDF 網站 URL 產生 PDF 檔案(https://ironpdf.com/ ):

// Initialize the ChromePdfRenderer which is used to convert URLs to PDFs
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create a PdfDocument by rendering a specified URL as a PDF
PdfDocument pdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/");
// Initialize the ChromePdfRenderer which is used to convert URLs to PDFs
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create a PdfDocument by rendering a specified URL as a PDF
PdfDocument pdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/");
' Initialize the ChromePdfRenderer which is used to convert URLs to PDFs
Dim renderer As New ChromePdfRenderer()

' Create a PdfDocument by rendering a specified URL as a PDF
Dim pdfDocument As PdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/")
$vbLabelText   $csharpLabel

在上面的程式碼範例中,第一行初始化了 IronPDF 庫中的ChromePdfRenderer實例,該實例負責將網頁轉換為 PDF 格式。 第二行程式碼使用此實例透過渲染指定 URL 中的內容來建立PdfDocument ,從而實現進一步的 PDF 相關操作。

無需對話框即可列印 PDF 文件

// Initiate the printing process for the PdfDocument
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf");
// Initiate the printing process for the PdfDocument
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf");
' Initiate the printing process for the PdfDocument
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf")
$vbLabelText   $csharpLabel

上述程式碼行使用指定的列印解析度 (DPI) 啟動PdfDocument的列印過程,將其傳送至" Microsoft Print to PDF "印表機(如果沒有安裝印表機,通常是預設印表機),並將列印輸出儲存為名為" printedfile1.pdf "的 PDF 檔案到 files 目錄中。 您可以根據需要變更印表機名稱和文件位置。

PDF 檔案列印效果完美無瑕:

如何在 C# 中不彈出對話框打印 PDF 文件,圖 5:名為"printedfile1.pdf"的 PDF 文件的輸出圖像 輸出名為"printedfile1.pdf"的PDF檔案的影像

具備進階列印選項的靜音列印

若要以程式設計方式更好地控制 PDF 檔案的列印,請查看以下包含進階選項的程式碼片段:

using (var printDocument = pdfDocument.GetPrintDocument())
{
    // Set the printer name to "Microsoft Print to PDF"
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
    // Specify the file name for the printed document
    printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf";
    // Enable printing to file
    printDocument.PrinterSettings.PrintToFile = true;
    // Set custom printer resolution
    printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
    {
        Kind = PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };

    // Initiate the print process
    printDocument.Print();
}
using (var printDocument = pdfDocument.GetPrintDocument())
{
    // Set the printer name to "Microsoft Print to PDF"
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
    // Specify the file name for the printed document
    printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf";
    // Enable printing to file
    printDocument.PrinterSettings.PrintToFile = true;
    // Set custom printer resolution
    printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
    {
        Kind = PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };

    // Initiate the print process
    printDocument.Print();
}
Using printDocument = pdfDocument.GetPrintDocument()
	' Set the printer name to "Microsoft Print to PDF"
	printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
	' Specify the file name for the printed document
	printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf"
	' Enable printing to file
	printDocument.PrinterSettings.PrintToFile = True
	' Set custom printer resolution
	printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
		.Kind = PrinterResolutionKind.Custom,
		.X = 1200,
		.Y = 1200
	}

	' Initiate the print process
	printDocument.Print()
End Using
$vbLabelText   $csharpLabel

在上面的程式碼片段中,IronPDF 實作了進階 PDF 列印自訂功能。 它可以根據PdfDocument產生PrintDocument ,允許自訂印表機設定、印表機名稱、輸出檔案名稱和解析度。 設定PrintFileName非常重要,因為它允許您繞過列印對話方塊進行列印。 然後,程式碼使用 print 方法觸發列印,將列印 PDF 文件內容導向指定的印表機,即本例中的" Microsoft Print to PDF "。 最後,將輸出結果儲存為 PDF 檔案。這樣即可實現無縫、無對話框的 PDF 列印,並可自訂設定。

您也可以透過將 IronPDF 整合到您的專案中,指定列印 PDF 文件的頁面範圍,而無需印表機對話方塊和 Adobe Acrobat Reader。 有關列印的更多詳細信息,請訪問程式碼範例頁面

輸出

專案執行完畢後,無需任何使用者交互,即可在指定資料夾中產生兩個輸出列印的 PDF 檔案。 您也可以比較使用進階選項後的尺寸差異。

如何在 C# 中不彈出對話框列印 PDF 文件?圖 6:顯示兩個已列印 PDF 檔案"printedfile1.pdf"和"printedfile2.pdf"的圖像 圖片顯示了兩個列印出來的PDF檔案"printedfile1.pdf"和"printedfile2.pdf"。

結論

直接從 C# 應用程式列印 PDF 文件可以簡化文件處理並提升使用者體驗。 IronPDF擁有豐富的 PDF 處理工具,使開發人員能夠無縫列印 PDF 文件。 透過將 IronPDF 整合到您的 C# 專案中,您可以充分利用其功能。

IronPDF是一個商業庫,以其強大的 PDF 處理功能而聞名。 它提供免費試用期,使用戶能夠測試和體驗其功能。 試用期結束後,即可獲得繼續使用的許可證。 首先,您可以從IronPDF 官方網站下載產品。

常見問題解答

如何從 C# 應用程式直接列印 PDF 檔案而不產生對話方塊?

您可以使用 IronPDF 的 API 直接從 C# 應用程式列印 PDF 檔案,而無需對話方塊。透過程式化管理印表機設定,您可以將 PDF 傳送至類似「Microsoft Print to PDF」的印表機,並自動執行列印程序。

使用 IronPDF 在 C# 中打印 PDF 有哪些優勢?

IronPDF 可在無需使用者互動的情況下自動執行 PDF 列印任務、無縫整合至工作流程,並提供自訂印表機設定和輸出檔案規格等進階選項,進而提升使用者體驗。

如何設定 Visual Studio 專案使用 IronPDF 列印 PDF?

若要建立 Visual Studio 專案,請開啟 Visual Studio,建立新的主控台應用程式,選擇適當的專案範本,並透過 NuGet 套件管理員安裝 IronPDF 以啟用 PDF 列印功能。

IronPDF 是否可以在無需使用者介入的情況下列印 PDF 的特定頁面?

是的,IronPDF 可讓您指定列印的頁面範圍,無需使用者介入或印表機對話框,因此非常適合自動化工作。

使用 IronPDF 從網頁 URL 生成 PDF 的過程是怎樣的?

IronPDF 提供從網頁 URL 產生 PDF 的方法,然後可以直接列印到指定的印表機。這樣就可以自動轉換和列印網頁內容。

如何使用 IronPDF 自訂列印設定?

IronPdf 為列印設定提供進階的客製化選項,包括定義印表機解析度和指定輸出檔案名稱,讓您更能掌控列印流程。

Iron Software 的 .NET 列印函式庫 IronPrint 相容於哪些平台?

IronPrint 支援多種平台,包括 Windows、macOS、Android 和 iOS,可在不同環境下使用。

如何在我的 C# 專案中安裝 IronPDF?

您可以使用 Visual Studio 中的 NuGet Package Manager 在 C# 專案中安裝 IronPDF。只需在「瀏覽」標籤中搜尋 IronPDF,並將其新增至您的專案即可。

IronPDF 為 C# 語言的 PDF 處理提供哪些功能?

IronPDF 提供了強大的 PDF 創建、操作和互動功能,包括合併、分割、加密 PDF 和直接從 C# 應用程式列印。

IronPDF 有哪些授權選項?

IronPdf 提供免費試用版供開發人員評估其功能,並提供多種授權選項供在生產環境中持續使用。

Curtis Chau
技術作家

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

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