如何在 C# 中列印文件到列印機
直接從C#應用程式列印PDF文件是一項有價值的功能,特別是在開發需要與實體PDF文件無縫整合的應用程式時。 無論您正在開發PDF文件管理系統、銷售點應用程式或任何涉及列印的軟體,C#提供的強大程式庫可以簡化此PDF列印方法的功能。
為此目的,Microsoft C#提供一個列印方法,將PDF文件列印至預設印表機。 在本文中,我們將探討如何使用C#將PDF文件列印到印表機的步驟。
如何使用C#將文件列印到印表機
- 建立C# Windows Forms應用程式
- 使用關鍵字匯入System.Drawing.Printing
- 使用按鈕和其他必要的控件進行表單設計
- 使用PrintPage事件處理PrintDocument事件
- 啟動列印作業
- 運行應用程式並點擊列印按鈕進行列印
先決條件
在開始之前,請確保您具備以下先決條件:
- C#開發環境(例如,Visual Studio)。
- 互動印表機的適當權限。
- 基本的C#程式設計知識。
步驟1:設置您的專案
建立一個新的C#專案或在您偏好的開發環境中打開現有專案。 確保您的專案正確配置,並且您具備與印表機互動的必要權限。 以下過程允許您完成此過程:
安裝Visual Studio
如果您尚未安裝Visual Studio,請從官方網站下載並安裝:Visual Studio。
建立新專案
- 打開Visual Studio。
- 點擊"建立新專案"。

選擇專案模板
- 在"建立新專案"對話框中,根據您的偏好選擇"Windows Forms App (.NET Framework)"或"Windows Forms App (.NET Core)"。

- 為您的專案提供名稱和位置。

- 點擊下一步,從額外資訊螢幕選擇.NET Framework,然後點擊"建立"。
設計表單
- 專案建立後,主表單將在設計器中顯示。
- 使用工具箱向表單中新增按鈕、文字框、標籤等控件。
- 使用屬性窗口自訂每個控件的屬性。

- 調整表單的外觀和佈局。

步驟2:匯入所需的程式庫
在您的C#程式碼文件中,匯入必要的命名空間來存取與列印相關的類別和方法。
using System.Drawing.Printing;
using System.Drawing.Printing;
Imports System.Drawing.Printing
這些命名空間提供關鍵類別,如PrintDocument、PrintPageEventArgs和PrintController,用於處理列印操作。
步驟3:處理PrintDocument事件
PrintDocument類是C#列印的基石。 處理其PrintPage事件以定義列印的內容以及格式。 讓我們從列印文字文件內容的簡單範例開始:
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
// Specify the file path
string filePath = "C:\\path\\to\\your\\file.txt";
// Read the content of the file
string line = System.IO.File.ReadAllText(filePath);
// Create a Font object (adjust as needed)
Font font = new Font("Arial", 12);
// Create a RectangleF to define the printing area
RectangleF area = new RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height);
// Draw the content to the printing area
e.Graphics.DrawString(line, font, Brushes.Black, area);
// Set HasMorePages to false to indicate that there are no more pages to print
e.HasMorePages = false;
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
// Specify the file path
string filePath = "C:\\path\\to\\your\\file.txt";
// Read the content of the file
string line = System.IO.File.ReadAllText(filePath);
// Create a Font object (adjust as needed)
Font font = new Font("Arial", 12);
// Create a RectangleF to define the printing area
RectangleF area = new RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height);
// Draw the content to the printing area
e.Graphics.DrawString(line, font, Brushes.Black, area);
// Set HasMorePages to false to indicate that there are no more pages to print
e.HasMorePages = false;
}
Private Sub printDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
' Specify the file path
Dim filePath As String = "C:\path\to\your\file.txt"
' Read the content of the file
Dim line As String = System.IO.File.ReadAllText(filePath)
' Create a Font object (adjust as needed)
Dim font As New Font("Arial", 12)
' Create a RectangleF to define the printing area
Dim area As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
' Draw the content to the printing area
e.Graphics.DrawString(line, font, Brushes.Black, area)
' Set HasMorePages to false to indicate that there are no more pages to print
e.HasMorePages = False
End Sub
此範例讀取一個文字文件的內容,並使用指定的字體和格式列印它。
步驟4:啟動列印作業
透過建立PrintDocument類的實例,附加PrintPage事件處理程式,然後觸發列印過程來啟動列印作業。 您可以選擇性地顯示列印對話框供使用者配置:
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
// Optionally, display a print dialog for user configuration
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
{
pd.PrinterSettings = printDialog.PrinterSettings;
pd.Print();
}
}
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
// Optionally, display a print dialog for user configuration
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
{
pd.PrinterSettings = printDialog.PrinterSettings;
pd.Print();
}
}
Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim pd As New PrintDocument()
AddHandler pd.PrintPage, AddressOf printDocument1_PrintPage
' Optionally, display a print dialog for user configuration
Dim printDialog As New PrintDialog()
If printDialog.ShowDialog() = DialogResult.OK Then
pd.PrinterSettings = printDialog.PrinterSettings
pd.Print()
End If
End Sub
此程式碼建立PrintDocument實例,附加PrintPage事件處理程式,然後列印文件。 可選的列印對話框允許使用者在啟動列印作業前配置列印設定。 這將文字文件列印到本地連接的印表機。 如果不存在,則文件將列印至列印對話框中的預設印表機名稱(Microsoft Print to PDF),如下所示:

透過IronPrint程式庫進行C#高級列印
在C#控制台應用中有效處理列印功能時,IronPrint程式庫提供了一個強大的解決方案。
IronPrint簡介
IronPrint是由Iron Software開發的一個全面列印程式庫,設計用於與.NET應用程式無縫整合。 無論您正在進行桌面、網頁還是行動專案,IronPrint提供了多功能的列印PDF文件功能,支援各種文件格式並提供可自訂列印設定。

主要特點
- 格式支援:IronPrint支援各種文件格式,包括PDF、PNG、TIFF、GIF、JPEG和BITMAP。這種多樣性確保開發者可以處理不同型別的內容以進行列印。
- 可自訂設定:開發者可以根據其應用程式的需求自訂列印設定。 這包括設置DPI(每英寸點數)、指定紙張方向(縱向或橫向)和控制副本數量的選項。
- 列印對話框:IronPrint 通過允許開發者在列印前顯示列印對話框來促進無縫的使用者體驗。 這在使用者必須與列印過程互動並選擇特定選項的情況下非常有用。
- 靜默列印:IronPrint提供了靜默列印功能,特別有利於自動化和提升工作流程效率。 這允許開發者在沒有使用者互動的情況下列印發票,消除手動干預需求並精簡整體過程。
- 跨平台相容性:IronPrint超越平台限制,提供與多種環境的相容性,包括Windows (7+)、macOS (10+)、iOS (11+)和Android API 21+ (v5 "Lollipop")。 它無縫整合了不同的專案型別,如Mobile (Xamarin, MAUI & Avalonia)、Desktop (WPF, MAUI & Windows Avalonia) 和 Console (App & Library)。
- 廣泛的.NET版本支援:無論您使用最新的.NET 8、7、6或Core 3.1+,IronPrint都支持。 它也支援.NET Framework (4.6.2+),確保在各種開發環境中具有相容性。
安裝IronPrint
在深入進行文件列印之前,您需要安裝IronPrint程式庫。 您可以輕鬆使用NuGet套件管理器控制台來執行此操作:
Install-Package IronPrint
此命令行將下載並安裝IronPrint程式庫到您的C#專案中。
初始化IronPrint
安裝IronPrint後,您需要在您的C#程式碼中初始化它。 匯入IronPrint命名空間,並設置授權金鑰以確保其正常運作:
using IronPrint;
class Program
{
public static void Main()
{
License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
}
}
using IronPrint;
class Program
{
public static void Main()
{
License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
}
}
Imports IronPrint
Friend Class Program
Public Shared Sub Main()
License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY"
End Sub
End Class
使用IronPrint列印文件
使用IronPrint列印文件很簡單。 您可以使用Printer類別,指定文件路徑到Print方法進行靜默列印PDF:
using IronPrint;
class Program
{
public static void Main()
{
License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
// Specify the file path
var document = "C:\\path\\to\\your\\file.pdf";
// Print PDFs
Printer.Print(document);
}
}
using IronPrint;
class Program
{
public static void Main()
{
License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
// Specify the file path
var document = "C:\\path\\to\\your\\file.pdf";
// Print PDFs
Printer.Print(document);
}
}
Imports IronPrint
Friend Class Program
Public Shared Sub Main()
License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY"
' Specify the file path
Dim document = "C:\path\to\your\file.pdf"
' Print PDFs
Printer.Print(document)
End Sub
End Class
靜默列印的好處是不需要任何使用者互動即可列印PDF,這對於需要自動化來精簡工作流程的情況非常有幫助。 對於需要使用者互動的情況,如列印前的對話框,您可以使用ShowPrintDialog,該功能實現相同結果,但帶有對話框作為額外步驟。
儘管此範例僅顯示列印PDF文件,IronPrint還支援多種文件格式,包括PDF、PNG、TIFF、GIF、JPEG、IMAGE和BITMAP。
自訂列印設定
IronPrint允許您根據應用程式需求自訂列印設定。 您可以使用PrintSettings類別配置DPI、列印份數、紙張方向等設定。 以下程式碼範例幫助您設置頁面設定並列印PDF文件:
using IronPrint;
class Program
{
public static void Main()
{
IronPrint.License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
Console.WriteLine("Printing Started...");
// Specify the file path
string filePath = "C:\\path\\to\\your\\file.pdf";
// Configure print settings
PrintSettings printSettings = new PrintSettings();
printSettings.Dpi = 300;
printSettings.NumberOfCopies = 2;
printSettings.PaperOrientation = PaperOrientation.Landscape;
// Print the document with custom settings
Printer.Print(filePath, printSettings);
// Print using the Print dialog
Printer.ShowPrintDialog(filePath, printSettings);
}
}
using IronPrint;
class Program
{
public static void Main()
{
IronPrint.License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY";
Console.WriteLine("Printing Started...");
// Specify the file path
string filePath = "C:\\path\\to\\your\\file.pdf";
// Configure print settings
PrintSettings printSettings = new PrintSettings();
printSettings.Dpi = 300;
printSettings.NumberOfCopies = 2;
printSettings.PaperOrientation = PaperOrientation.Landscape;
// Print the document with custom settings
Printer.Print(filePath, printSettings);
// Print using the Print dialog
Printer.ShowPrintDialog(filePath, printSettings);
}
}
Imports IronPrint
Friend Class Program
Public Shared Sub Main()
IronPrint.License.LicenseKey = "YOUR_IRONPRINT_LICENSE_KEY"
Console.WriteLine("Printing Started...")
' Specify the file path
Dim filePath As String = "C:\path\to\your\file.pdf"
' Configure print settings
Dim printSettings As New PrintSettings()
printSettings.Dpi = 300
printSettings.NumberOfCopies = 2
printSettings.PaperOrientation = PaperOrientation.Landscape
' Print the document with custom settings
Printer.Print(filePath, printSettings)
' Print using the Print dialog
Printer.ShowPrintDialog(filePath, printSettings)
End Sub
End Class
這就是輸出的樣子:

如果沒有安裝實體印表機,則使用預設印表機列印PDF文件。 要獲取所有可用的印表機,您還可以使用GetPrinterNames方法。
// Retrieve printers' name
List<string> printersName = Printer.GetPrinterNames();
foreach (string printer in printersName)
{
Console.WriteLine(printer);
}
// Retrieve printers' name
List<string> printersName = Printer.GetPrinterNames();
foreach (string printer in printersName)
{
Console.WriteLine(printer);
}
' Retrieve printers' name
Dim printersName As List(Of String) = Printer.GetPrinterNames()
For Each printer As String In printersName
Console.WriteLine(printer)
Next printer
如需更多詳細資訊,請存取文件頁面。
結論
使用System.Drawing.Printing命名空間提供的功能,C#中文件列印是一項易於管理的任務。 透過處理PrintPage事件和使用PrintDocument類別,您可以根據具體需求調整列印過程。 這份全面指南已涵蓋從C#應用程式列印文件的基本步驟,提供了一個堅實的基礎,使您能夠將此功能整合到您的專案中。
對於尋求高級功能、擴展性和自動化的開發者來說,IronPrint是一個理想的解決方案。 其直觀的API和額外的功能超越了Microsoft提供的基本套件。 隨著我們更深入地探討IronPrint,我們發現了包括支援多種文件型別、能夠合併圖像、改善格式化以更好地滿足應用程式特定需求的定制選項。
將IronPrint整合到您的C#應用程式中可以簡化文件列印到印表機的過程。 憑藉對多種文件格式和可自訂列印設定的支援,IronPrint為希望提升其列印能力的開發者提供了一個強大解決方案。 無論您正在進行桌面、網頁或行動專案,IronPrint簡化了列印過程,使其成為您.NET工具包中的寶貴補充。 透過探索我們的免費試用頁面,發現IronPrint的好處。 從這裡下載程式庫,親自體驗其功能!
常見問題
如何使用C#將PDF文件列印至印表機?
您可以通過建立Windows Forms應用程式、匯入System.Drawing.Printing命名空間並處理PrintDocument事件來將PDF文件列印至印表機。如需更多高級功能,例如支持多種文件格式和可定制的列印設置,您可以使用Iron Software的IronPrint程式庫。
設置C#項目以進行列印涉及哪些步驟?
要設置C#項目以進行列印,請首先在Visual Studio中建立一個新的Windows Forms應用程式,設計表單介面,並匯入System.Drawing.Printing命名空間。IronPrint可整合以增強列印功能,例如處理多種格式和設置。
IronPrint可以處理不同的文件格式進行列印嗎?
是的,IronPrint支持多種文件格式,包括PDF、PNG、HTML、TIFF、GIF、JPEG、IMAGE和BITMAP,這使其成為開發者需要列印各種型別內容時的多功能選擇。
IronPrint為什麼是一個在C#應用程式中強大的列印解決方案?
IronPrint提供強大的解決方案,其支持多種格式、自定義設置如DPI和紙張方向以及跨平臺相容性。它還易於整合到.NET應用程式中,提供增強的列印功能。
如何使用IronPrint自定義列印設置?
IronPrint允許您使用其PrintSettings類自定義列印設置。您可以調整設置如DPI、拷貝數和紙張方向以滿足特定的列印要求。
IronPrint是否具有跨平臺相容性?
是的,IronPrint具有跨平臺相容性,支持Windows、macOS、iOS和Android等環境,這使得開發者可以在不同操作系統的各種應用程式中使用該程式庫。
IronPrint如何增強C#應用程式的列印功能?
IronPrint通過提供高級功能,如支持多種文件格式、可定制的列印設置以及無縫整合到.NET應用程式中,來增強C#應用程式的列印功能,從而提高整體列印過程。
如何在C#中使用IronPrint啟動列印任務?
要在C#中使用IronPrint啟動列印任務,您需要建立PrintDocument類的實例,附加PrintPage事件處理器,然後使用IronPrint的方法來執行列印工作,並進行任何所需的自定義。
開始在C#中列印文件需要什麼?
要開始在C#中列印文件,您需要一個開發環境,如Visual Studio,存取印表機的權限,以及對C#編程的基本瞭解。可以將IronPrint新增到您的項目中以提供增強的列印功能。


