如何在 C# 中打印文件到打印機
直接從 C# 應用程式列印 PDF 文件可能是一項有價值的功能,尤其是在開發需要與實體 PDF 文件無縫整合的應用程式時。 無論您是在開發 PDF 文件管理系統、銷售點應用程序,還是任何其他與列印相關的軟體,C# 都提供了一套強大的程式庫來簡化這種 PDF 列印方法的功能。
為此,Microsoft C# 提供了一個列印方法,可以將 PDF 檔案列印到預設印表機。 在本文中,我們將探討如何使用 C# 將 PDF 檔案列印到印表機。
如何在 C# 中將文件列印到印表機
- 建立一個 C# Windows 窗體應用程式
- 使用關鍵字導入System.Drawing.Printing
- 設計表單,新增按鈕和其他必要的控制項
- 使用PrintPage事件處理PrintDocument事件
- 啟動列印作業
- 運行應用程序,然後按一下"列印"按鈕進行列印。
先決條件
在開始之前,請確保您已具備以下先決條件:
- AC# 開發環境(例如 Visual Studio)。
- 擁有與印表機互動的足夠權限。
- 具備 C# 程式設計的基本知識。
步驟 1:設定您的項目
在您首選的開發環境中建立一個新的 C# 專案或開啟一個現有的 C# 專案。 請確保您的專案配置正確,並且您擁有與印表機互動所需的權限。 以下步驟可協助您完成此流程:
安裝 Visual Studio
如果您尚未安裝 Visual Studio,請從官方網站下載並安裝: Visual Studio 。
建立新專案
1.開啟 Visual Studio。
- 點選"建立新項目"。
選擇項目模板
- 在"建立新專案"對話方塊中,依照您的喜好選擇"Windows 窗體應用程式 (.NET Framework)"或"Windows 窗體應用程式 (.NET Core)"。
如何在 C# 中將文件列印到印表機:圖 2 - Windows 窗體應用程式
- 請提供項目名稱和地點。
- 按一下"下一步",然後在附加資訊畫面中選擇 .NET Framework,然後按一下"建立"。
設計表格
- 專案建立完成後,主表單將顯示在設計器中。
- 依需要,使用工具箱為表單新增按鈕、文字方塊、標籤等控制項。
- 使用"屬性"視窗自訂每個控制項的屬性。
- 調整表單的外觀和佈局。
步驟二:導入所需庫
在你的 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 文件列印功能,支援各種文件格式並提供可自訂的列印設定。
主要功能
1.格式支援:IronPrint 支援多種文件格式,包括 PDF、PNG、TIFF、GIF、JPEG 和點陣圖。這種多功能性確保開發人員可以處理不同類型的列印內容。 2.可自訂設定:開發者可以根據應用程式的要求自訂列印設定。 這包括設定 DPI(每英吋點數)、指定紙張方向(縱向或橫向)以及控制影印份數等選項。 3.列印對話框:IronPrint 允許開發人員在列印前顯示列印對話框,從而提供無縫的使用者體驗。 在使用者必須與列印過程互動並選擇特定選項的場景中,這會非常有用。 4.靜音列印:IronPrint 提供靜音列印功能,這對於自動化和提高工作流程效率尤其有利。 這使得開發人員無需用戶互動即可列印發票,從而消除了人工幹預的需要,並簡化了整個流程。 5.跨平台相容性:IronPrint 超越了平台限制,提供與各種環境的相容性,包括 Windows (7+)、macOS (10+)、iOS (11+) 和 Android API 21+ (v5"Lollipop")。 它與不同的專案類型無縫集成,例如行動裝置(Xamarin、MAUI 和 Avalonia)、桌面端(WPF、MAUI 和 Windows Avalonia)以及控制台(應用程式和程式庫)。 6.廣泛的 .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輸出結果如下所示:
如何在 C# 中將文件列印到印表機:圖 8 - 自訂列印輸出
如果未安裝實體印表機,則使用預設印表機列印 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 和附加功能超越了微軟提供的基本軟體包。 隨著我們對 IronPrint 的深入了解,我們發現了一系列自訂選項,包括支援各種檔案類型、能夠添加圖像以及增強格式以更好地滿足應用程式的特定需求。
將 IronPrint 整合到您的 C# 應用程式中,可以簡化列印文件到印表機的過程。 IronPrint 支援多種文件格式和可自訂的列印設置,為希望增強列印功能的開發人員提供了一個強大的解決方案。 無論您是在處理桌面、Web 還是行動項目,IronPrint 都能簡化列印流程,使其成為您 .NET 工具包的寶貴補充。 透過造訪我們的免費試用頁面,了解 IronPrint 的優勢。 從這裡下載庫文件,親身體驗它的功能吧!
常見問題解答
如何使用 C# 將 PDF 檔案列印至印表機?
您可以使用 C# 建立 Windows 窗體應用程式,匯入 System.Drawing.Printing 命名空間,並處理 PrintDocument 事件,將 PDF 檔案列印至印表機。如需更先進的功能,例如支援各種檔案格式和可自訂列印設定,您可以使用 Iron Software 的 IronPrint 函式庫。
設定 C# 專案進行列印需要哪些步驟?
要為列印設定 C# 專案,首先要在 Visual Studio 中建立新的 Windows 窗體應用程式,設計窗體介面,並匯入 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# 應用程式的整體列印流程。
如何使用 IronPrint 在 C# 中啟動列印工作?
要使用 IronPrint 在 C# 中啟動列印工作,您需要建立一個 PrintDocument 類的實體,附加一個 PrintPage 事件處理程式,然後再使用 IronPrint 的方法來執行列印工作,並進行任何所需的自訂。
以 C# 開始列印檔案需要哪些條件?
要開始使用 C# 列印檔案,您需要 Visual Studio 之類的開發環境、存取印表機的權限,以及對 C# 程式設計的基本瞭解。IronPrint 可以添加到您的專案中,以提供增強的列印功能。








