跳至頁尾內容
USING IRONPRINT

如何在 C# 中靜默列印 PDF 文件

PDF文件是一種可攜帶文件格式,可按請求的格式儲存和傳輸資料。 它保留了儲存資料的格式,允許使用者專注於處理數位文件的其他方面。 列印數位文件是一項繁瑣的工作,因為資料在不同平台上顯示不同。 但是,將資料發送到PDF文件有助於保持格式以便列印。 然而,以程式方式列印PDF文件可能對於C#的開發者來說是個挑戰。 感謝IronPDF - C# PDF程式庫,它使列印PDF文件的過程極其簡單且無煩惱。

本文將解釋如何使用IronPDF程式庫在C#中靜默列印PDF文件。

IronPDF - C# PDF程式庫

IronPDF是一個C# .NET程式庫,允許開發人員建立、閱讀和編輯PDF文件。 它是一個一流的C#程式庫,優先考慮準確性、易用性和速度。 它專為C#, F#,& VB.NET設計,與.NET 7, 6, 5, Core, Standard, or Framework.高度相容。它利用IronPDF強大的Chromium引擎,幫助從HTML生成Web、桌面和控制台的PDF文件。

此外,IronPDF允許使用者輕鬆操作和編輯PDF、新增頁眉和頁腳、從PDF中提取文字和圖像。

一些重要功能包括:

  • 從不同文件格式載入並生成PDF文件
  • 使用預設列印機保存和列印PDF文件
  • 合併和拆分PDF文件
  • 無需Adobe Reader的PDF編輯功能

使用IronPDF程式庫在C#中靜默列印PDF文件的步驟

要靜默列印PDF文件,首先我們需要在本地電腦上安裝以下組件。

  1. Visual Studio - 它是C#開發的官方IDE,必須安裝在電腦上。 您可以從Visual Studio網站下載並安裝。
  2. 建立專案 - 要在C#中建立一個用於PDF列印的控制台應用程式,請按照以下步驟使用Visual Studio 2022:

    • 打開Visual Studio並點擊建立新專案

      Visual Studio

      Visual Studio

    • 選擇C#控制台應用程式並點擊下一步

      新專案對話框

      新專案對話框

    • 現在,輸入項目名稱,選擇位置,然後點擊下一步

      Web Forms

      Web Forms

    • 選擇最新的.NET框架為您的應用程式使用。 我們將使用穩定版6.0。

      額外資訊

      額外資訊

    • 點擊建立,控制台專案已建立,我們準備以程式方式列印PDF文件。
  3. 安裝IronPDF - 有三種方式下載和安裝IronPDF程式庫。 如下所示:

    • 使用Visual Studio - Visual Studio具有NuGet包管理器,幫助在C#專案中安裝NuGet包。

      • 點擊選單欄上的工具,或
      • 右鍵單擊方案資源管理器中的專案文件

        方案資源管理器

        方案資源管理器

        專案選單>管理NuGet封裝

      • 一旦打開,在NuGet包管理器中瀏覽IronPDF並安裝,如下所示:

        從NuGet包安裝IronPDF

        從NuGet包安裝IronPDF

    • 直接下載NuGet封裝 - 另一種下載IronPDF的方法是導航到NuGet網站並直接下載包。 這裡是連結https://www.nuget.org/packages/IronPdf/
    • 下載IronPDF .DLL程式庫 - IronPDF也可以直接從IronPDF網站下載。導航至:IronPDF DLL下載以安裝。 在您的專案中引用.DLL文件以使用它。

生成PDF文件和列印任務

在這裡,我們將從URL生成一個PDF文件。 建立PDF文件很簡單,通常是兩步過程。 以下程式碼範例生成了一個PDF:

using IronPdf;

// Initialize a new instance of ChromePdfRenderer.
ChromePdfRenderer Renderer = new ChromePdfRenderer();

// Render the specified URL as a PDF document.
PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
using IronPdf;

// Initialize a new instance of ChromePdfRenderer.
ChromePdfRenderer Renderer = new ChromePdfRenderer();

// Render the specified URL as a PDF document.
PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
Imports IronPdf

' Initialize a new instance of ChromePdfRenderer.
Private Renderer As New ChromePdfRenderer()

' Render the specified URL as a PDF document.
Private Pdf As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/")
$vbLabelText   $csharpLabel

使用上述程式碼建立的PDF文件物件已準備好列印。 接下來,我們將使用預設列印機紙張列印PDF文件。 這段程式碼是一行,內容如下:

// Print the PDF document using the default printer settings.
Pdf.Print();
// Print the PDF document using the default printer settings.
Pdf.Print();
' Print the PDF document using the default printer settings.
Pdf.Print()
$vbLabelText   $csharpLabel

這個Print方法將PDF發送到預設列印機進行列印。

高級列印選項

對於靜默列印,IronPDF提供各種高級列印選項。

使用System.Drawing.Printing.PrintDocument物件中。 程式碼簡單,內容如下:

// Remember to add assembly reference to System.Drawing.dll in project

// Get the print document for the PDF.
System.Drawing.Printing.PrintDocument PrintPDF = Pdf.GetPrintDocument();
// Remember to add assembly reference to System.Drawing.dll in project

// Get the print document for the PDF.
System.Drawing.Printing.PrintDocument PrintPDF = Pdf.GetPrintDocument();
' Remember to add assembly reference to System.Drawing.dll in project

' Get the print document for the PDF.
Dim PrintPDF As System.Drawing.Printing.PrintDocument = Pdf.GetPrintDocument()
$vbLabelText   $csharpLabel

指定列印機名稱

IronPDF還提供列印到特定列印機的機會。 要指定名稱,使用PrinterSettings.PrinterName屬性。 首先,我們需要獲取當前的PDF文件物件。 程式碼範例如下:

using (var printDocument = pdfDocument.GetPrintDocument())
{
    // Specify the printer name.
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

    // Send the print job to the specified printer.
    printDocument.Print();
}
using (var printDocument = pdfDocument.GetPrintDocument())
{
    // Specify the printer name.
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

    // Send the print job to the specified printer.
    printDocument.Print();
}
Using printDocument = pdfDocument.GetPrintDocument()
	' Specify the printer name.
	printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"

	' Send the print job to the specified printer.
	printDocument.Print()
End Using
$vbLabelText   $csharpLabel

設置列印機解析度

另一個很酷的功能是設置列印機解析度。 我們可以根據輸出控制要列印的像素數量。 來自PDF文件的DefaultPageSettings.PrinterResolution屬性可用於設置解析度。 這裡是一個非常簡單的程式碼範例:

// Set the custom resolution for the printer.
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
    Kind = PrinterResolutionKind.Custom,
    X = 1200,
    Y = 1200
};

// Send the print job with the custom resolution settings.
printDocument.Print();
// Set the custom resolution for the printer.
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
    Kind = PrinterResolutionKind.Custom,
    X = 1200,
    Y = 1200
};

// Send the print job with the custom resolution settings.
printDocument.Print();
' Set the custom resolution for the printer.
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
	.Kind = PrinterResolutionKind.Custom,
	.X = 1200,
	.Y = 1200
}

' Send the print job with the custom resolution settings.
printDocument.Print()
$vbLabelText   $csharpLabel

PrintToFile方法

這個PrintToFile方法,允許我們在C#中將PDF列印到文件。 它將pathToFile作為參數,用來直接列印文件到該位置而不打開列印機對話框。 程式碼簡單,內容如下:

// Print the document to a specified file location.
printDocument.PrintToFile("PathToFile", false);
// Print the document to a specified file location.
printDocument.PrintToFile("PathToFile", false);
' Print the document to a specified file location.
printDocument.PrintToFile("PathToFile", False)
$vbLabelText   $csharpLabel

完整的程式碼範例如下:

using IronPdf;

// Initialize the PDF renderer and create the PDF document.
ChromePdfRenderer Renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/");

using (var printDocument = pdfDocument.GetPrintDocument())
{
    // Specify the printer name.
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

    // Set a custom print resolution.
    printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
    {
        Kind = PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };

    // Execute the print job.
    printDocument.Print();
}
using IronPdf;

// Initialize the PDF renderer and create the PDF document.
ChromePdfRenderer Renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/");

using (var printDocument = pdfDocument.GetPrintDocument())
{
    // Specify the printer name.
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

    // Set a custom print resolution.
    printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
    {
        Kind = PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };

    // Execute the print job.
    printDocument.Print();
}
Imports IronPdf

' Initialize the PDF renderer and create the PDF document.
Private Renderer As New ChromePdfRenderer()
Private pdfDocument As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/")

Using printDocument = pdfDocument.GetPrintDocument()
	' Specify the printer name.
	printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"

	' Set a custom print resolution.
	printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
		.Kind = PrinterResolutionKind.Custom,
		.X = 1200,
		.Y = 1200
	}

	' Execute the print job.
	printDocument.Print()
End Using
$vbLabelText   $csharpLabel

執行程式碼後,它將URL轉換為PDF文件。 然後要靜默列印PDF文件,使用GetPrintDocument方法。 成功編譯和執行程式文件後,出現列印對話框,將其保存為PDF文件。 然後使用指定的列印機名稱保存該PDF。

Csharp Print Pdf Silently 8 related to PrintToFile方法

Csharp Print Pdf Silently 9 related to PrintToFile方法

總結

在本文中,我們仔細研究了如何使用IronPDF靜默列印PDF文件。 IronPDF在PDF列印時提供了許多有用的選項。 它還可以跟蹤列印的頁面,並允許在頁面範圍內列印。

靜默列印及其他列印選項,使IronPDF成為C#中處理PDF時一個傑出的程式庫。

IronPDF有助於將不同格式的資料轉換為PDF,從PDF轉為不同格式。 它使開發者更容易在應用程式開發過程中整合PDF功能。 更重要的是,它不需要Adobe Acrobat Reader來查看和編輯PDF文件。

IronPDF對於個人開發免費,並可用於商業用途授權。 它提供一個免費試用授權以存取並測試程式庫的全部功能。 您可以在此連結查詢更多細節。

常見問題

如何在C#中靜默列印PDF?

您可以在C#中使用IronPDF的Print方法來靜默列印PDF。這可讓您在不開啟列印對話框的情況下列印,確保文件的格式得到保留。

設置IronPDF以進行靜默PDF列印需要哪些步驟?

要設置IronPDF以進行靜默PDF列印,通過NuGet套件管理器或下載.DLL文件來安裝程式庫。然後,使用Print方法配置靜默列印設置,並指定如PrinterNamePrinterResolution等列印屬性。

如何確保在列印時保留PDF格式?

IronPDF通過允許您配置特定的列印設置,如設置PrinterResolution屬性,來幫助維護PDF格式,確保列印輸出與原始文件匹配。

我可以將PDF列印到C#中的檔案位置嗎?

可以,IronPDF提供PrintToFile方法,允許您直接將PDF列印到指定的檔案位置,而不開啟列印對話框。

是否可以不使用Adobe Acrobat Reader列印PDF?

可以,IronPDF允許您在不需要Adobe Acrobat Reader的情況下列印、查看和編輯PDF,使其成為C#應用程式中處理PDF文件的靈活工具。

IronPDF是否支持跨平台PDF列印?

是的,IronPDF支持跨平台PDF列印,提供Windows、macOS、Android和iOS的相容性,使其在各種開發環境中具有多功能性。

IronPDF有哪些試用選項?

IronPDF對個人開發免費,並提供試用授權,允許您在投入商業授權前測試其全部功能。

如何指定特定的印表機進行PDF列印與IronPDF?

您可以在IronPDF中設置PrinterSettings.PrinterName屬性,將列印工作引導到您想要的印表機。

推薦哪個IDE用于使用IronPDF開發C#應用程式?

Visual Studio是推薦的整合開發環境(IDE),用於使用IronPDF開發C#應用程式,提供一個強大的平臺進行編碼和專案管理。

如何解決C#中靜默PDF列印的問題?

如果在C#中遇到靜默PDF列印問題,請確保IronPDF已正確安裝並且列印設置如PrinterNamePrinterResolution已精確配置。查閱IronPDF文件也可提供有用的故障排除提示。

Curtis Chau
技術作家

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

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

Iron 支援團隊

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