使用 IRONPRINT

如何在 C# 中無對話框列印 PDF 檔案

已更新 2024年3月17日
分享:

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

IronPDF - .NET Framework PDF 函式庫

IronPDF 是一個強大的C#函式庫,專為簡化創建、操作和互動PDF文件而設計。使用 IronPDF,開發人員可以輕鬆地從HTML內容、圖片和其他來源生成PDF,使其成為一個多功能的工具,能夠處理簡單和複雜的PDF相關任務。其功能不僅限於生成PDF文件;IronPDF還可幫助您合併、拆分和以多種方式操作PDF。另外,其直觀的API使得初學者和經驗豐富的開發人員都能輕鬆上手。一些重要的功能包括:

直接打印PDF的重要性

當使用PDF時,打印是基本需求之一。直接從C#應用程序中打印PDF而不顯示打印對話框提供了若干優點。它通過消除不必要的互動來提升用戶體驗,自動化打印任務,並實現與較大工作流程的無縫集成。IronPDF簡化了這個過程,使開發人員在保持對打印過程控制的同時,確保打印輸出的完整性。

先決條件

在進行PDF列印過程之前,請確保您具備以下先決條件:

  1. Visual Studio:這是一個集成開發環境 (集成開發環境) 您可以在此創建、編輯和編譯您的 C# 專案。要下載並安裝到您的電腦上,請訪問官方網站 Visual Studio 網站.

  2. IronPDF: The IronPDF 可以輕鬆透過 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# 控制台應用項目

    **在 Visual Studio 中創建一個新的 C# 控制台應用程式專案**
  4. 配置專案詳情:選擇模板後,系統會提示您配置專案詳情。

    如何在 C# 中打印 PDF 文件而不彈出對話框,圖 2:配置您的新項目

    配置您的新專案

  5. 配置其他設定:選擇具有長期支援的 .NET Framework。IronPDF 支援最新版本的 .NET Framework。

  6. 建立專案:當您配置好專案詳情後,點擊 Create 按鈕。Visual Studio 會建立專案並在 IDE 中打開它。

安裝 IronPDF via NuGet

你可以參考以下步驟在你的專案中安裝 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
VB   C#

使用 ChromePdfRenderer 建立 PDF 文檔

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

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/");
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/");
Dim renderer As New ChromePdfRenderer()
Dim pdfDocument As PdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/")
VB   C#

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

無需對話框打印 PDF 文件

pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf");
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf");
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf")
VB   C#

上述程式碼行使用指定的列印解析度啟動 PdfDocument 的列印過程 (DPI)將其發送到「Microsoft Print to PDF」印表機,通常在沒有安裝其他印表機的情況下,這是預設的印表機,並將列印的輸出存儲為名為「printedfile1.pdf」的 PDF 文件在文件目錄中。您可以根據需要更改印表機名稱和文件位置。

PDF 文件列印的效果為像素完美:

如何在 C# 中無對話框打印 PDF 文件,圖 5:「printedfile1.pdf」文件的輸出圖像

PDF 文件名 "printedfile1.pdf" 的輸出圖像

使用進階列印選項靜默列印

為了更好地控制 以程式方式列印 PDF 文件,請參閱具有高級選項的以下代碼片段:

using (var printDocument = pdfDocument.GetPrintDocument())
{
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
    printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf";
    printDocument.PrinterSettings.PrintToFile = true;
    printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
    {
        Kind = PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };

    printDocument.Print();
}
using (var printDocument = pdfDocument.GetPrintDocument())
{
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
    printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf";
    printDocument.PrinterSettings.PrintToFile = true;
    printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
    {
        Kind = PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };

    printDocument.Print();
}
Using printDocument = pdfDocument.GetPrintDocument()
	printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
	printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf"
	printDocument.PrinterSettings.PrintToFile = True
	printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
		.Kind = PrinterResolutionKind.Custom,
		.X = 1200,
		.Y = 1200
	}

	printDocument.Print()
End Using
VB   C#

在上述代碼片段中,IronPDF 提供了進階的 PDF 列印自訂功能。它從 PdfDocument 生成一個 PrintDocument,允許自訂印表機設定、印表機名稱、輸出檔案名稱和解析度。設定 PrintFileName 很重要,因為這允許您在列印時繞過列印對話框。然後,代碼使用印刷方法觸發列印,將打印的 PDF 文件內容導向至指定的印表機,即本文中的 "Microsoft Print to PDF"。最後,將輸出儲存為 PDF 文件。這使得使用自訂設定進行無對話框的無縫 PDF 列印成為可能。

您還可以指定頁面範圍來列印 PDF 文件,而不需要列印對話框和 Adobe Acrobat Reader,只需將 IronPDF 整合到您的專案中即可。欲瞭解更多列印的詳細資訊,請訪問 程式碼範例頁面.

輸出

執行專案後,兩個輸出的 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 到特定打印機(代碼示例教程)
下一個 >
如何在 C# 中靜默列印 PDF 檔案

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 9,531 查看許可證 >