跳至頁尾內容
USING 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. IronPDF:IronPDF程式庫,可以輕鬆使用NuGet包管理器整合到您的專案中。

建立Visual Studio專案

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

  1. 開啟Visual Studio:啟動您的Visual Studio IDE。
  2. 建立新專案:Visual Studio開啟後,點擊"建立新專案"。
  3. 選擇專案模板:在"建立新專案"窗口中,您將看到專案模板的列表。 選擇Visual C#控制台應用程式。

    How to Print PDF File Without Dialog in C#, Figure 1: Create a new C# Console App project in Visual Studio 在Visual Studio中建立新的C#控制台應用程式

  4. 設定專案詳細資訊:選擇模板後,系統將提示您設定專案詳細資訊。

    How to Print PDF File Without Dialog in C#, Figure 2: Configure your new project 設定您的新專案

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

透過NuGet安裝IronPDF

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

  1. 開啟Visual Studio和您的專案。
  2. 前往"工具"選單並選擇"NuGet包管理器",然後點選"管理解決方案的NuGet包"。

    How to Print PDF File Without Dialog in C#, Figure 3: Navigate to NuGet Package Manager 導航至NuGet包管理器

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

    How to Print PDF File Without Dialog in C#, Figure 4: Search for IronPDF in NuGet Package Manager UI 在NuGet包管理器使用者介面中搜索IronPDF

  4. 點選IronPdf包並為您的專案選擇它,然後點擊"安裝"按鈕。

IronPDF無對話框列印 - 步驟過程

導入IronPdf命名空間

程式碼開始時先導入System.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(https://ironpdf.com/)中生成PDF文件:

// 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

在上述程式碼範例中,第一行初始化一個ChromePdfRenderer實例,來自IronPDF程式庫,負責將網頁轉換為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文件在文件目錄中。 您可以根據需要更改列印機名稱和文件位置。

PDF文件是以像素完美的方式列印出來的:

How to Print PDF File Without Dialog in C#, Figure 5: Output image of the PDF file named printedfile1.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列印自訂。 它從PrintDocument,允許自訂列印機設置、列印機名稱、輸出文件名和解析度。 設定PrintFileName很重要,因為它可讓您繞過列印對話框直接列印。 然後程式碼觸發列印,使用列印方法將PDF文件內容導向到指定的列印機,例如,本例中的是"Microsoft Print to PDF"。 最後將輸出保存為PDF文件。這使這無縫、無需對話框的PDF列印具有自定義設置。

您還可以指定頁面範圍以便無需列印機對話框和Adobe Acrobat Reader的情況下列印PDF文件,只需將IronPDF整合到您的專案中。 有關列印的更多詳細資訊,請存取程式碼範例頁

輸出

執行專案後,兩個輸出列印的PDF文件在指定的資料夾裡生成,無需使用者參與。 您還可以比較使用高級選項後的尺寸差異。

How to Print PDF File Without Dialog in C#, Figure 6: Image displaying the two printed PDF files printedfile1.pdf and printedfile2.pdf 顯示列印的兩個PDF文件's "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” 一樣自動化列印過程。

using IronPDF 在 C# 中列印 PDF 的優點是什麼?

IronPDF 通過自動化 PDF 列印任務而無需使用者交互來提升使用者體驗,無縫整合至工作流程中,並提供進階選項如自定義列印機設置和輸出文件規範。

如何設置 Visual Studio 項目以使用 IronPDF 列印 PDF?

為設置 Visual Studio 項目,打開 Visual Studio,建立一個新控制台應用程式,選擇合適的項目模板,並使用 NuGet 包管理器安裝 IronPDF 以啟用 PDF 列印功能。

IronPDF 可以無使用者干預列印 PDF 的特定頁面嗎?

可以,IronPDF 允許您指定頁面範圍以列印,無需使用者干預或顯示列印機對話框,讓其成為自動化任務的理想選擇。

using IronPDF 從網頁 URL 生成 PDF 的過程是什麼?

IronPDF 提供了從網頁 URL 生成 PDF 的方法,然後可以直接列印到指定的列印機。這允許對網頁內容自動轉換和列印。

如何使用 IronPDF 自定義列印設置?

IronPDF 提供先進的列印設置自定義選項,包括定義列印機的解析度和指定輸出文件名稱,讓您對列印過程有更大的控制權。

哪個平台與 Iron Software 的 .NET 列印庫 IronPrint 相容?

IronPrint 支持多個平台,包括 Windows, macOS, Android 和 iOS,允許在不同環境中靈活使用。

如何在我的 C# 項目中安裝 IronPDF?

您可以在 Visual Studio 的 NuGet 包管理器中安裝 IronPDF 到您的 C# 項目中。只需在“瀏覽”選項卡中搜索 IronPDF 並將其新增到您的項目中即可。

IronPDF對於在C#中操作PDF提供了哪些功能?

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

IronPDF 提供了哪些授權選擇?

IronPDF 為開發者提供免費試用以評估其功能,並提供多種授權選擇以便在生產環境中持續使用。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話