使用IRONPRINT

C# 发送 PDF 到打印机(逐步教程)

发布 2022年十二月6日
分享:

PDF 的意思是 "便携式文档格式"。在许多情况下,开发人员需要在应用程序中以编程方式打印 PDF 文件。在 C# 中,这可能是一项非常繁琐的任务,但有了 IronPDF,只需几行代码就能轻松完成。该工具允许我们使用默认打印机设置和自定义打印选项打印 PDF 文档。在本教程中,您将学习如何使用 C# 语言打印 PDF。

本教程涵盖的主题

本教程将涉及以下主题:

  • IronPDF 库
  • 创建 C# 控制台项目

  • 安装 IronPDF

    1.NuGet 软件包管理器

    2.NuGet 软件包管理器控制台

    3.使用 DLL 文件

  • 添加 IronPDF 命名空间
  • 打印 PDF 文档

    1.创建 PDF 文档并打印 PDF

    2.根据 URL 创建 PDF 文档并打印

    3.高级打印

  • 摘要

IronPDF

IronPDF 是一款适用于 .NET Framework 的 PDF 库,允许开发人员轻松创建 PDF 文件。对于 Google Chrome 浏览器的桌面版本而言,IronPDF 的渲染效果堪称 "完美像素"。只需一行代码,IronPDF 就能轻松创建 PDF 文档。它无需 Acrobat Reader 或其他 PDF 查看器即可处理 PDF 文档。

IronPDF 可用于从 HTML 字符串、HTML 文件或 URL 创建 PDF 文件。之后,它还能将这些文件发送到默认打印机进行打印。

A 免费试用 的 IronPDF 版本。

IronPDF 库的一些重要功能

  • 利用 HTML 4 和 5、CSS 和 JavaScript 创建 PDF 文档
  • 从 URL 生成 PDF 文档
  • 将 PDF 打印到默认的物理打印机
  • 设置打印任务 (用于打印特定页面等。)
  • 加载带有自定义网络登录凭证、用户代理、代理服务器、cookie、HTTP 标头和表单字段或变量的 URL,从而允许访问 HTML 登录表单后面的网页
  • 读取和填写 PDF (便携式文档格式) 表单字段数据
  • 从 PDF 文件中提取图像和文本
  • 对 PDF 文档进行数字签名

  • 无需第三方库

1.创建一个 C# 项目

本教程将使用 Visual Studio 2022,但也可以使用更早的版本。

  • 打开 Visual Studio 2022。
  • 创建一个新的 C# .NET 控制台项目。选择 .NET Core 控制台应用程序。
How to Send PDFs to Printer Using C#, Figure 1: 控制台应用程序

控制台应用程序

  • 为项目命名,例如 DemoApp。
  • 我们要使用的 .NET Framework 6.0 是最新、最稳定的版本。点击 "创建 "按钮。
How to Send PDFs to Printer Using C#, Figure 2: .NET框架

.NET框架

2.安装 IronPDF 库

要安装 IronPDF 库,我们可以使用下面列出的任何一种方法:

2.1.NuGet 软件包管理器

我们可以通过 NuGet 包管理器安装 IronPDF C# .NET Core 库。

点击工具 > NuGet 包管理器 > 管理解决方案的 NuGet 包,打开包管理器。

How to Send PDFs to Printer Using C#, Figure 3: 软件包管理器

软件包管理器

或者,在解决方案资源管理器中右键单击项目,然后单击管理 NuGet 包

How to Send PDFs to Printer Using C#, Figure 4: NuGet 包管理器 - 解决方案资源管理器

NuGet 包管理器 - 解决方案资源管理器

搜索 IronPDF。选择 IronPDF 并点击安装。程序库将开始安装。

How to Send PDFs to Printer Using C#, Figure 5: 安装 IronPDF

安装 IronPDF

2.2.NuGet 软件包管理器控制台

点击工具 > NuGet 包管理器 > **管理器控制台,打开 NuGet 包管理器控制台。

在命令行中键入以下命令

Install-Package IronPrint
How to Send PDFs to PRinter Using C#, Figure 6: NuGet 软件包管理器控制台

软件包管理器控制台

2.3.使用 DLL 文件

在项目中使用 IronPDF 的另一种方法是从 IronPDF 库中添加一个 DLL 文件。您可以从以下链接下载 DLL 文件 链接.

  • 下载 DLL 压缩文件。解压到指定文件夹。
  • 在 Visual Studio 中打开一个项目。在 "解决方案资源管理器 "中,右键单击 "引用 "并浏览 IronPDF DLL 文件。

2.4.添加 IronPDF 命名空间

安装完成后,在程序文件中添加 IronPDF 和 System.Drawing.DLL 命名空间。

using IronPdf;
using System.Drawing.dll;
using IronPdf;
using System.Drawing.dll;
Imports IronPdf
Imports System.Drawing.dll
VB   C#

**注意:您必须在希望使用 IronPDF 功能的每个文件中添加这些引用。

IronPDF 已安装就绪! 现在,我们可以为 .NET Core 应用程序创建第一个 PDF 文档,并将其发送到默认打印机进行打印。下面让我们通过代码示例来了解其中的一些功能。

3.打印 PDF 文档

3.1.从 HTML 创建并打印 PDF 文档

处理 HTML 字符串并将其转换为 PDF 格式非常简单。然后就可以使用 IronPDF 打印新生成的文件。下面是轻松创建 PDF 的代码。

// Render any HTML fragment or document to HTML
var html= new ChromePdfRenderer();
using var PDF = html.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to PDF file");

// Send the PDF to the default printer to print
Pdf.Print();

System.Drawing.Printing.PrintDocument PrintDocYouCanWorkWith = Pdf.GetPrintDocument();
// Render any HTML fragment or document to HTML
var html= new ChromePdfRenderer();
using var PDF = html.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to PDF file");

// Send the PDF to the default printer to print
Pdf.Print();

System.Drawing.Printing.PrintDocument PrintDocYouCanWorkWith = Pdf.GetPrintDocument();
' Render any HTML fragment or document to HTML
Dim html= New ChromePdfRenderer()
Dim PDF = html.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to PDF file")

' Send the PDF to the default printer to print
Pdf.Print()

Dim PrintDocYouCanWorkWith As System.Drawing.Printing.PrintDocument = Pdf.GetPrintDocument()
VB   C#

该代码将使用 RenderHtmlAsPdf 函数中传递的 HTML 内容创建 PDF 文件。该函数将 HTML 片段转换为 PDF 文档。

要使用 IronPDF 库生成 PDF 文件或 PDF 页面,您必须熟悉 HTML 标记。我们使用 Print 功能将 PDF 文件的输出发送到打印机。打印机对话框将出现,允许您确认打印任务。

3.2.从 URL 创建并打印 PDF 文档

您还可以使用 URL 创建 PDF 文档:

var Render = new ChromePdfRenderer();
var PDF = Render.RenderUrlAsPdf("https://ironpdf.com/");

// Send the PDF to the default printer to print
Pdf.Print();

System.Drawing.Printing.PrintDocument PrintDoc = Pdf.GetPrintDocument();
var Render = new ChromePdfRenderer();
var PDF = Render.RenderUrlAsPdf("https://ironpdf.com/");

// Send the PDF to the default printer to print
Pdf.Print();

System.Drawing.Printing.PrintDocument PrintDoc = Pdf.GetPrintDocument();
Dim Render = New ChromePdfRenderer()
Dim PDF = Render.RenderUrlAsPdf("https://ironpdf.com/")

' Send the PDF to the default printer to print
Pdf.Print()

Dim PrintDoc As System.Drawing.Printing.PrintDocument = Pdf.GetPrintDocument()
VB   C#

PDF 打印出来的效果如下图所示:

如何使用 C# 将 PDF 发送至 PRinter,图 7:打印从 URL 生成的 PDF

软件包管理器控制台

4.高级打印选项

IronPDF 功能多样,能够处理查找打印机和设置打印分辨率等打印功能。

4.1 指定打印机

要指定打印机,只需获取当前打印文档对象 (借助 "GetPrintDocument "方法)然后使用 PrinterSettings.PrinterName 属性。您可以选择任何可用的打印机。

using (var printDocument = PDF.GetPrintDocument())
{
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.Print();
}
using (var printDocument = PDF.GetPrintDocument())
{
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.Print();
}
Using printDocument = PDF.GetPrintDocument()
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
printDocument.Print()
End Using
VB   C#

在上面的代码示例中,我选择了 "Microsoft Print to PDF"。更多信息 设置特定打印设置 请参见文档页面。

4.2 设置打印机分辨率

您还可以设置打印 PDF 的分辨率。分辨率指的是打印或显示的像素数,具体取决于输出结果。您也可以通过 IronPDF 设置打印文档的分辨率。 默认页面设置.打印机分辨率 属性。

printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
    Kind = PrinterResolutionKind.Custom,
    X = 1200,
    Y = 1200
};
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
    Kind = PrinterResolutionKind.Custom,
    X = 1200,
    Y = 1200
};
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
	.Kind = PrinterResolutionKind.Custom,
	.X = 1200,
	.Y = 1200
}
VB   C#

4.3 使用 C#&num 追踪打印过程;

在以下代码示例中,您将看到如何更改打印机名称和分辨率,以及如何获得已打印页数的计数。

using (var printDocument = PDF.GetPrintDocument())
{
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
    printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
    {
        Kind = PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };
    var printedPages = 0;
    printDocument.PrintPage += (sender, args) => printedPages++;
    printDocument.Print();
}
using (var printDocument = PDF.GetPrintDocument())
{
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
    printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
    {
        Kind = PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };
    var printedPages = 0;
    printDocument.PrintPage += (sender, args) => printedPages++;
    printDocument.Print();
}
Using printDocument = PDF.GetPrintDocument()
	printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
	printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
		.Kind = PrinterResolutionKind.Custom,
		.X = 1200,
		.Y = 1200
	}
	Dim printedPages = 0
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: printDocument.PrintPage += (sender, args) => printedPages++;
	AddHandler printDocument.PrintPage, Sub(sender, args) printedPages
	printedPages += 1
	printDocument.Print()
End Using
VB   C#

5.总结

IronPDF 是处理 PDF 文档的完整解决方案。它提供了从不同格式转换为 PDF 的功能。使用 IronPDF 库函数,PDF 文件的操作和格式化变得非常简单。只需几行代码即可创建和格式化 PDF 文件。它还能以编程方式打印 PDF。它通过将 PDF 发送到计算机的默认打印机来打印 PDF。我们既可以向用户显示打印对话窗口,也可以使用 Print 方法的重载进行静默打印。

A 免费试用 此外,您还可以使用 IronPDF 软件的许可证测试其在应用程序中生成和打印 PDF 文档的全部潜力。有关许可的更多信息,请访问以下网站 链接.

此外 当前特惠 您只需支付两套产品的价格,即可获得五套 Iron 软件产品。

< 前一页
C#程序化打印PDF(代码示例教程)
下一步 >
如何用VB.NET打印PDF的分步教程

准备开始了吗? 版本: 2024.8 刚刚发布

免费NuGet下载 总下载量: 7,289 查看许可证 >