在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
从 C# 应用程序直接打印 PDF 文件是一项非常有价值的功能,尤其是在开发需要与物理 PDF 文档无缝集成的应用程序时。 无论您是在开发 PDF 文档管理系统、销售点应用程序,还是其他任何涉及打印的软件,C# 都能提供一套强大的库来促进这种 PDF 打印方法功能的实现。
为此,Microsoft C# 提供了一个打印方法,用于将 PDF 文件打印到默认打印机。 在本文中,我们将探讨使用 C# 将 PDF 文件打印到打印机的步骤。
创建 C# Windows 窗体应用程序
使用关键字导入System.Drawing.Printing
设计带有按钮和其他必要控件的表单
使用PrintPage事件处理PrintDocument事件
启动打印作业
在开始之前,请确保您已准备好以下先决条件:
C# 开发环境(例如,Visual Studio)。
与打印机交互的充分权限。
在您喜欢的开发环境中创建一个新的 C# 项目或打开一个现有项目。 确保您的项目配置正确,并拥有打印机交互所需的权限。 您可以通过以下流程完成这一过程:
如果您没有安装Visual Studio,请从官网下载并安装:Visual Studio。
打开 Visual Studio。
如何在 C# 中将文件打印到打印机:图 1 - 新项目
在“创建新项目”对话框中,根据您的偏好选择“Windows 窗体应用程序 (.NET Framework)”或“Windows 窗体应用程序 (.NET Core)”。
请提供项目的名称和地点。
项目创建后,主表单将显示在设计器中。
使用工具箱可根据需要在表单中添加按钮、文本框、标签等控件。
在您的 C# 代码文件中,导入必要的命名空间以访问与打印相关的类和方法。
using System.Drawing.Printing;
using System.Drawing.Printing;
Imports System.Drawing.Printing
这些命名空间提供了处理打印操作所需的重要类,如PrintDocument、PrintPageEventArgs和PrintController。
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
此示例读取文本文件的内容,并使用指定的字体和格式进行打印。
通过创建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):
在 C# 控制台应用程序中高效处理打印功能时,IronPrint 库提供了强大的解决方案。
IronPrint 是由 Iron Software 开发的综合打印库,旨在无缝集成到 .NET 应用程序中。 无论您从事的是桌面、网络还是移动项目,IronPrint 都能提供多功能的打印 PDF 文档功能,支持各种文件格式并提供可定制的打印设置。
格式支持:IronPrint 支持多种文档格式,包括 PDF、PNG、HTML、TIFF、GIF、JPEG 和 BITMAP。这种多样性确保开发人员可以处理不同类型的打印内容。
可定制设置:开发人员可以根据其应用程序的要求灵活定制打印设置。 这包括设置 DPI(每英寸点数)的选项,指定纸张方向(纵向或横向),以及控制复印数量。
打印对话框:IronPrint通过允许开发人员在打印前显示打印对话框来促进无缝的用户体验。 这在用户需要与打印过程进行交互并选择特定选项的场景中非常有用。
跨平台兼容性:IronPrint 超越平台限制,提供与多种环境的兼容性,包括 Windows (7+)、macOS (10+)、iOS (11+) 和 Android API 21+(v5 "Lollipop")。 它可以无缝集成到不同类型的项目中,例如移动应用(Xamarin、MAUI 和 Avalonia)、桌面应用(WPF、MAUI 和 Windows Avalonia)以及控制台应用(应用程序和库)。
在深入学习文件打印之前,您需要安装 IronPrint 库。 您可以使用 NuGet 软件包管理器控制台轻松完成这项工作:
Install-Package IronPrint
此命令行将下载 IronPrint 库并安装到您的 C# 项目中。
安装 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 打印文件非常简单。 您可以使用Printer类,指定文件路径到Print方法来无声打印PDF:
using IronPrint;
class Program
{
// static void main
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
{
// static void main
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
' static void main
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文件,但IronPrint支持各种文件格式,包括PDF、PNG、HTML、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 的其他定制选项,例如处理不同的文件类型、整合图片以及根据应用程序的需求增强格式。
将 IronPrint 集成到您的 C# 应用程序中可简化将文件打印到打印机的过程。 IronPrint 支持各种文件格式和可定制的打印设置,为希望增强打印能力的开发人员提供了强大的解决方案。 无论您是在开发桌面、Web 还是移动项目,IronPrint 都能简化打印流程,使其成为您 .NET 工具包中的重要一员。