如何在 C# 中将文件打印到打印机
从C#应用程序直接打印PDF文件可以是一项有价值的功能,特别是在开发需要与物理PDF文档无缝集成的应用程序时。 无论您是在开发PDF文档管理系统、销售点应用程序,还是处理打印的其他软件,C#都提供了一套强大的库来支持这种PDF打印方法的功能。
为此,微软的C#提供了打印方法,用于将PDF文件打印到默认打印机。 在本文中,我们将探讨使用C#将PDF文件打印到打印机的步骤。
如何在C#中打印文件到打印机
- 创建一个C# Windows窗体应用程序
- 使用关键字导入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")。 它与各种项目类型无缝集成,如移动端(Xamarin、MAUI& Avalonia)、桌面端(WPF、MAUI & Windows Avalonia)和控制台(应用程序&库)。
- 广泛的.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有关更详细的信息,请访问文档页面。
结论
在C#中打印文件是一项可以通过System.Drawing.Printing命名空间提供的能力轻松实现的任务。 通过处理PrintPage事件并利用PrintDocument类,您可以根据具体需求定制打印过程。 本综合指南涵盖了从C#应用程序打印文件的基本步骤,为将此功能集成到您的项目中提供了坚实的基础。
对于寻求先进功、具有可扩展性及自动化的开发者,IronPrint是理想的解决方案。 其直观的API及附加功能超越了微软提供的基本包。 随着我们深入了解IronPrint,我们发现了一系列的自定义选项,包括支持多种文件类型、加入图像的能力,以及为了更好地契合应用程序特定需求而在格式上的增强。
将IronPrint集成到您的C#应用程序中简化了将文件打印到打印机的过程。 通过支持多种文件格式和可定制的打印设置,IronPrint为希望提升打印能力的开发人员提供了强大的解决方案。 无论您是在开发桌面、网页或移动项目,IronPrint简化了打印过程,使其成为您的.NET工具包中有价值的补充。 通过浏览我们的免费试用页面,发现IronPrint的好处。 从这里下载库并亲自体验其功能!
常见问题解答
如何使用 C# 将 PDF 文件打印到打印机?
您可以通过创建 Windows 窗体应用程序、导入 System.Drawing.Printing 命名空间,并处理 PrintDocument 事件来使用 C# 将 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# 应用程序中的整体打印流程。
如何在 C# 中使用 IronPrint 启动打印任务?
要在 C# 中使用 IronPrint 启动打印任务,您需要创建 PrintDocument 类的实例,附加 PrintPage 事件处理程序,然后使用 IronPrint 的方法执行打印任务,并进行任何所需的自定义设置。
开始在 C# 中打印文件需要什么?
要在 C# 中开始打印文件,您需要开发环境,如 Visual Studio,访问打印机的权限,以及 C# 编程的基本知识。可以将 IronPrint 添加到您的项目中,以提供增强的打印功能。









