如何在 C# 中从模板生成 PDF | IronPDF
从RawPrint迁移到IronPDF与其说是替换,不如说是重新调整:RawPrint和IronPDF解决不同的问题,对于大多数团队来说,更适合的说法是"补充"而非"替换"。本指南探讨IronPDF确实取代RawPrint管道的情况(在Windows上创建PDF然后推送的工作流程)以及您应该保留RawPrint的情况(ESC/POS,ZPL,EPL,原始PCL)。
为什么要从RawPrint迁移到 IronPDF?
了解 RawPrint
RawPrint 是 NuGet 包 RawPrint(frogmorecs/RawPrint,v0.5.0,最后发布于2019年9月,现在在nuget.org上列为未列出/遗留)。 它是一个轻量级的 P/Invoke 包装器,通过 winspool.Drv 将字节流直接发送到视窗打印后台处理程序中,使用 RAW 数据类型。 公共 API 很小:一个 Printer 类在 RawPrint 命名空间中实现 IPrinter,公开 PrintRawFile 和 PrintRawStream 重载。 底层 Win32 调用(ClosePrinter)在该接口后面隐藏——使用该包时,您不需要自己编写它们。
这使得RawPrint非常适合ESC/POS收据打印机、ZPL/EPL斑马标签打印机,以及在您已经产生打印机固件所期望的字节的情况下适合于遗留PCL/PostScript作业。 对于针对视窗特定环境并需要直接进行打印机通信的开发人员,RawPrint 提供了绕过驱动程序或图形界面等中间层的高效途径。
然而,RawPrint不是PDF库——它只是将数据推送到打印机。 如果您的应用程序当前在其他库中构建PDF然后交给RawPrint,那么整个管道通常可以在Windows上合并到IronPDF中。
核心问题:无法创建 PDF 文件
RawPrint 有明显的局限性,不适合现代文档工作流程:
1.不支持 PDF 创建:RawPrint仅专注于数据传输,缺乏 PDF 创建、渲染或操作功能。
2.非常底层:由于直接处理原始字节,开发人员必须深入了解打印机的命令语言,因此不太适合简单的文档打印任务。
3.平台特定:它依赖于视窗打印后台处理程序,限制了其跨平台适用性。
4.无文档处理:仅进行字节传输,不具备渲染功能。
5.控制有限:打印作业配置选项最少。
RawPrint与IronPDF对比
| 特征 | RawPrint | IronPDF |
|---|---|---|
| 功能性 | 直接向打印机发送原始打印数据 | 全面的 PDF 创建和操作 |
| 使用案例 | 标签等专业印刷 | 一般文件管理和创建 |
| 平台依赖性 | 针对 Windows | 跨平台 |
| 复杂性 | 低级,需要打印机命令知识 | 高级、用户友好的 API |
| PDF 创建 | 否 | 是 |
| 从 HTML 创建 PDF | 否 | 是 |
| 从 URL 创建 PDF | 否 | 是 |
| 编辑/修改 PDF 文件 | 否 | 是 |
| 合并/拆分 PDF 文件 | 否 | 是 |
| 打印现有 PDF 文件 | 是(原始字节) | 是(高级应用程序接口) |
| 打印控制 | 基本的 | 全部选项 |
| 适用于 | 直接访问打印机的需求 | 网络和桌面应用程序中与 PDF 相关的任务 |
| 灵活性 | 受原始字节处理的限制 | 内容广泛,功能多样 |
与RawPrint形成鲜明对比的是,IronPDF 提供了用于全面处理 PDF 的强大而通用的 API。 作为 .NET 环境中的知名品牌,IronPDF 使开发人员能够跨平台毫不费力地创建、编辑和转换 PDF。
对于目标是现代.NET的团队,IronPDF提供跨平台兼容性,而RawPrint则设计上不具备这种功能。
开始之前
前提条件
- .NET 环境: .NET Framework 4.6.2+ 或 .NET Core 3.1+ / .NET 5/6/7/8/9+
- NuGet 访问权限:能够安装 NuGet 包
- IronPDF 许可证:请从ironpdf.com获取您的许可证密钥。
NuGet 软件包变更
# Remove RawPrint
dotnet remove package RawPrint
# Install IronPDF
dotnet add package IronPdf
# Remove RawPrint
dotnet remove package RawPrint
# Install IronPDF
dotnet add package IronPdf
许可配置
// Add at application startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// Add at application startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
' Add at application startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
查找RawPrint使用情况
# Identify allRawPrintusage
grep -r "using RawPrint" --include="*.cs" .
grep -r "PrintRawFile\|PrintRawStream" --include="*.cs" .
# Identify allRawPrintusage
grep -r "using RawPrint" --include="*.cs" .
grep -r "PrintRawFile\|PrintRawStream" --include="*.cs" .
注意:围绕 winspool.Drv 手动滚动的 P/Invoke 包装器有时使用类名 RawPrinterHelper,看起来与RawPrint的用法表面上相似,但它们不是该包——它们直接调用 Win32 打印后台处理程序。 该包本身只公开 IPrinter,带有 PrintRawFile 和 PrintRawStream。
完整的 API 参考
命名空间变更
// Before: RawPrint
using RawPrint;
// After: IronPDF
using IronPdf;
// Before: RawPrint
using RawPrint;
// After: IronPDF
using IronPdf;
Imports IronPdf
核心 API 映射
| RawPrint(真正的公共API) | IronPDF | 备注 |
|---|---|---|
new Printer() / IPrinter |
new ChromePdfRenderer() / PdfDocument |
RawPrint推送字节; IronPDF生成PDFs |
printer.PrintRawFile(name, path, paused) |
PdfDocument.FromFile(path).Print() |
IronPDF重新渲染到操作系统打印系统; 不是RAW |
printer.PrintRawStream(name, stream, doc, paused) |
new PdfDocument(stream).Print() |
相同的警告 |
printer.OnJobCreated 事件 |
不适用 | 改用IronPDF打印选项 |
| 不适用 | ChromePdfRenderer.RenderHtmlAsPdf() |
创建 PDF |
| 不适用 | PdfDocument.Merge() |
合并 PDF |
| 不适用 | pdf.ApplyWatermark() |
添加水印 |
IronPDF通过将文档交给操作系统进行打印,而不是将原始字节流发送到假脱机程序。 如果您的打印机需要RAW数据类型(ESC/POS,ZPL,某些PCL工作流程),那是RawPrint的领域,IronPDF无法替代。
代码迁移示例
示例 1:HTML 到 PDF 的转换
之前(RawPrint):
// NuGet: Install-Package RawPrint
using System;
using System.IO;
using System.Text;
using RawPrint;
class Program
{
static void Main()
{
//RawPrintcannot convert HTML to PDF. Sending raw HTML bytes to a printer just
// prints the HTML source as text — the markup is not rendered.
string html = "<html><body><h1>Hello World</h1></body></html>";
byte[] data = Encoding.ASCII.GetBytes(html);
IPrinter printer = new Printer();
using (var stream = new MemoryStream(data))
{
// PrintRawStream(printerName, stream, documentName, paused)
printer.PrintRawStream("Microsoft Print to PDF", stream, "HTML Document", false);
}
Console.WriteLine("Raw HTML bytes sent to spooler (not rendered).");
}
}
// NuGet: Install-Package RawPrint
using System;
using System.IO;
using System.Text;
using RawPrint;
class Program
{
static void Main()
{
//RawPrintcannot convert HTML to PDF. Sending raw HTML bytes to a printer just
// prints the HTML source as text — the markup is not rendered.
string html = "<html><body><h1>Hello World</h1></body></html>";
byte[] data = Encoding.ASCII.GetBytes(html);
IPrinter printer = new Printer();
using (var stream = new MemoryStream(data))
{
// PrintRawStream(printerName, stream, documentName, paused)
printer.PrintRawStream("Microsoft Print to PDF", stream, "HTML Document", false);
}
Console.WriteLine("Raw HTML bytes sent to spooler (not rendered).");
}
}
Imports System
Imports System.IO
Imports System.Text
Imports RawPrint
Class Program
Shared Sub Main()
' RawPrint cannot convert HTML to PDF. Sending raw HTML bytes to a printer just
' prints the HTML source as text — the markup is not rendered.
Dim html As String = "<html><body><h1>Hello World</h1></body></html>"
Dim data As Byte() = Encoding.ASCII.GetBytes(html)
Dim printer As IPrinter = New Printer()
Using stream As New MemoryStream(data)
' PrintRawStream(printerName, stream, documentName, paused)
printer.PrintRawStream("Microsoft Print to PDF", stream, "HTML Document", False)
End Using
Console.WriteLine("Raw HTML bytes sent to spooler (not rendered).")
End Sub
End Class
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
string html = "<html><body><h1>Hello World</h1></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF created successfully");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
string html = "<html><body><h1>Hello World</h1></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF created successfully");
}
}
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim renderer = New ChromePdfRenderer()
Dim html As String = "<html><body><h1>Hello World</h1></body></html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")
Console.WriteLine("PDF created successfully")
End Sub
End Class
即使使用该包自身友好的API,架构不匹配也是显而易见的:RawPrint使用RAW数据类型将字节发送到Windows假脱机程序,因此传递HTML只会导致打印机打印源文本。IronPDF的 ChromePdfRenderer 实际上呈现了标记。请参阅 HTML 到 PDF 文档 以获取全面的示例。
示例 2:URL 到 PDF 的转换
之前(RawPrint):
// NuGet: Install-Package RawPrint
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using RawPrint;
class Program
{
static void Main()
{
//RawPrintcannot fetch a URL or render a web page; downloading the HTML and
// shipping it to the spooler just prints the raw HTML source.
using (var client = new HttpClient())
{
string htmlSource = client.GetStringAsync("https://example.com").GetAwaiter().GetResult();
byte[] data = Encoding.UTF8.GetBytes(htmlSource);
IPrinter printer = new Printer();
using (var stream = new MemoryStream(data))
{
printer.PrintRawStream("Microsoft Print to PDF", stream, "Web Page", false);
}
Console.WriteLine("Raw HTML sent to spooler (not a rendered PDF).");
}
}
}
// NuGet: Install-Package RawPrint
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using RawPrint;
class Program
{
static void Main()
{
//RawPrintcannot fetch a URL or render a web page; downloading the HTML and
// shipping it to the spooler just prints the raw HTML source.
using (var client = new HttpClient())
{
string htmlSource = client.GetStringAsync("https://example.com").GetAwaiter().GetResult();
byte[] data = Encoding.UTF8.GetBytes(htmlSource);
IPrinter printer = new Printer();
using (var stream = new MemoryStream(data))
{
printer.PrintRawStream("Microsoft Print to PDF", stream, "Web Page", false);
}
Console.WriteLine("Raw HTML sent to spooler (not a rendered PDF).");
}
}
}
Imports System
Imports System.IO
Imports System.Net.Http
Imports System.Text
Imports RawPrint
Module Program
Sub Main()
' RawPrint cannot fetch a URL or render a web page; downloading the HTML and
' shipping it to the spooler just prints the raw HTML source.
Using client As New HttpClient()
Dim htmlSource As String = client.GetStringAsync("https://example.com").GetAwaiter().GetResult()
Dim data As Byte() = Encoding.UTF8.GetBytes(htmlSource)
Dim printer As IPrinter = New Printer()
Using stream As New MemoryStream(data)
printer.PrintRawStream("Microsoft Print to PDF", stream, "Web Page", False)
End Using
Console.WriteLine("Raw HTML sent to spooler (not a rendered PDF).")
End Using
End Sub
End Module
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
// Render a live website directly to PDF with full CSS, JavaScript, and images
var pdf = renderer.RenderUrlAsPdf("https://example.com");
pdf.SaveAs("webpage.pdf");
Console.WriteLine("Website rendered to PDF successfully");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
// Render a live website directly to PDF with full CSS, JavaScript, and images
var pdf = renderer.RenderUrlAsPdf("https://example.com");
pdf.SaveAs("webpage.pdf");
Console.WriteLine("Website rendered to PDF successfully");
}
}
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim renderer = New ChromePdfRenderer()
' Render a live website directly to PDF with full CSS, JavaScript, and images
Dim pdf = renderer.RenderUrlAsPdf("https://example.com")
pdf.SaveAs("webpage.pdf")
Console.WriteLine("Website rendered to PDF successfully")
End Sub
End Class
RawPrint无法渲染网页——它只将字节发送到假脱机程序。 下载HTML并通过结果推送导致打印机打印源文本,而非渲染内容。IronPDF的 RenderUrlAsPdf() 捕获完整渲染的网页,包括 CSS、JavaScript 和图像。 在我们的教程中了解更多信息。
示例 3:文档格式
之前(RawPrint):
// NuGet: Install-Package RawPrint
using System;
using System.IO;
using System.Text;
using RawPrint;
class Program
{
static void Main()
{
//RawPrinthas no concept of fonts, margins, or layout — formatting must be
// expressed in the printer's own command language (PCL, PostScript, ESC/POS, ZPL).
// Example: a tiny PCL5 stream that selects portrait and a 16.66cpi font.
string pclCommands = "\x1B&l0O\x1B(s0p16.66h8.5v0s0b3T";
string text = "Plain text document - formatting must be expressed in printer command language";
byte[] data = Encoding.ASCII.GetBytes(pclCommands + text);
IPrinter printer = new Printer();
using (var stream = new MemoryStream(data))
{
printer.PrintRawStream("HP LaserJet", stream, "Raw Document", false);
}
}
}
// NuGet: Install-Package RawPrint
using System;
using System.IO;
using System.Text;
using RawPrint;
class Program
{
static void Main()
{
//RawPrinthas no concept of fonts, margins, or layout — formatting must be
// expressed in the printer's own command language (PCL, PostScript, ESC/POS, ZPL).
// Example: a tiny PCL5 stream that selects portrait and a 16.66cpi font.
string pclCommands = "\x1B&l0O\x1B(s0p16.66h8.5v0s0b3T";
string text = "Plain text document - formatting must be expressed in printer command language";
byte[] data = Encoding.ASCII.GetBytes(pclCommands + text);
IPrinter printer = new Printer();
using (var stream = new MemoryStream(data))
{
printer.PrintRawStream("HP LaserJet", stream, "Raw Document", false);
}
}
}
Imports System
Imports System.IO
Imports System.Text
Imports RawPrint
Module Program
Sub Main()
' RawPrint has no concept of fonts, margins, or layout — formatting must be
' expressed in the printer's own command language (PCL, PostScript, ESC/POS, ZPL).
' Example: a tiny PCL5 stream that selects portrait and a 16.66cpi font.
Dim pclCommands As String = ChrW(&H1B) & "&l0O" & ChrW(&H1B) & "(s0p16.66h8.5v0s0b3T"
Dim text As String = "Plain text document - formatting must be expressed in printer command language"
Dim data As Byte() = Encoding.ASCII.GetBytes(pclCommands & text)
Dim printer As IPrinter = New Printer()
Using stream As New MemoryStream(data)
printer.PrintRawStream("HP LaserJet", stream, "Raw Document", False)
End Using
End Sub
End Module
After (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
string html = @"
<html>
<head>
<style>
body { font-family: Arial; margin: 40px; }
h1 { color: #2c3e50; font-size: 24px; }
p { line-height: 1.6; color: #34495e; }
.highlight { background-color: yellow; font-weight: bold; }
</style>
</head>
<body>
<h1>Formatted Document</h1>
<p>This is a <span class='highlight'>beautifully formatted</span> document with CSS styling.</p>
<p>Complex layouts, fonts, colors, and images are fully supported.</p>
</body>
</html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("formatted.pdf");
Console.WriteLine("Formatted PDF created successfully");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
string html = @"
<html>
<head>
<style>
body { font-family: Arial; margin: 40px; }
h1 { color: #2c3e50; font-size: 24px; }
p { line-height: 1.6; color: #34495e; }
.highlight { background-color: yellow; font-weight: bold; }
</style>
</head>
<body>
<h1>Formatted Document</h1>
<p>This is a <span class='highlight'>beautifully formatted</span> document with CSS styling.</p>
<p>Complex layouts, fonts, colors, and images are fully supported.</p>
</body>
</html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("formatted.pdf");
Console.WriteLine("Formatted PDF created successfully");
}
}
Imports IronPdf
Imports System
Module Program
Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim renderer As New ChromePdfRenderer()
Dim html As String = "
<html>
<head>
<style>
body { font-family: Arial; margin: 40px; }
h1 { color: #2c3e50; font-size: 24px; }
p { line-height: 1.6; color: #34495e; }
.highlight { background-color: yellow; font-weight: bold; }
</style>
</head>
<body>
<h1>Formatted Document</h1>
<p>This is a <span class='highlight'>beautifully formatted</span> document with CSS styling.</p>
<p>Complex layouts, fonts, colors, and images are fully supported.</p>
</body>
</html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("formatted.pdf")
Console.WriteLine("Formatted PDF created successfully")
End Sub
End Module
使用RawPrint时,超出普通字节的格式化意味着需要手工编写打印机命令序列,例如 "\x1B&l0O\x1B(s0p16.66h8.5v0s0b3T" 并了解目标打印机的 PCL 或 PostScript 方言。 IronPDF使用标准HTML和CSS——复杂布局、字体、颜色和图像均得到完全支持,无需打印机特定知识。
功能对比摘要
| 特征 | RawPrint | IronPDF |
|---|---|---|
| PDF 创建 | ||
| HTML 至 PDF | 否 | 是 |
| URL 至 PDF | 否 | 是 |
| 从零开始 | 否 | 是 |
| PDF 操作 | ||
| 合并 PDF | 否 | 是 |
| 拆分 PDF | 否 | 是 |
| 添加水印 | 否 | 是 |
| 编辑现有内容 | 否 | 是 |
| 打印 | ||
| 推送RAW字节到假脱机程序 | 是 | 否 |
| ESC/POS,ZPL,原始PCL | 是 | 否 |
| 通过操作系统打印系统打印 | 否 | 是 |
| 平台 | ||
| 视窗 | 是 | 是 |
| Linux | 否 | 是 |
| MacOS | 否 | 是 |
| 多克 | 否 | 是 |
| 其他 | ||
| 加密 / 安全 | 否 | 是 |
| 数字签名 | 否 | 是 |
| PDF/A | 否 | 是 |
迁移后的新功能
迁移到IronPDF后,您将获得RawPrint无法提供的功能:
PDF 合并
var pdfs = pdfFiles.Select(f => PdfDocument.FromFile(f)).ToList();
var merged = PdfDocument.Merge(pdfs);
merged.SaveAs("complete.pdf");
var pdfs = pdfFiles.Select(f => PdfDocument.FromFile(f)).ToList();
var merged = PdfDocument.Merge(pdfs);
merged.SaveAs("complete.pdf");
Dim pdfs = pdfFiles.Select(Function(f) PdfDocument.FromFile(f)).ToList()
Dim merged = PdfDocument.Merge(pdfs)
merged.SaveAs("complete.pdf")
使用设置打印
var pdf = PdfDocument.FromFile("report.pdf");
pdf.Print(new PrintOptions
{
PrinterName = "HP LaserJet",
NumberOfCopies = 2,
DPI = 300,
GrayScale = false
});
var pdf = PdfDocument.FromFile("report.pdf");
pdf.Print(new PrintOptions
{
PrinterName = "HP LaserJet",
NumberOfCopies = 2,
DPI = 300,
GrayScale = false
});
Dim pdf = PdfDocument.FromFile("report.pdf")
pdf.Print(New PrintOptions With {
.PrinterName = "HP LaserJet",
.NumberOfCopies = 2,
.DPI = 300,
.GrayScale = False
})
在一个工作流程中创建和打印
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(@"
<h1>Invoice #12345</h1>
<p>Customer: John Doe</p>
<p>Amount: $150.00</p>
");
// Print directly
pdf.Print("HP LaserJet");
// Or save first
pdf.SaveAs("invoice.pdf");
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(@"
<h1>Invoice #12345</h1>
<p>Customer: John Doe</p>
<p>Amount: $150.00</p>
");
// Print directly
pdf.Print("HP LaserJet");
// Or save first
pdf.SaveAs("invoice.pdf");
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("
<h1>Invoice #12345</h1>
<p>Customer: John Doe</p>
<p>Amount: $150.00</p>
")
' Print directly
pdf.Print("HP LaserJet")
' Or save first
pdf.SaveAs("invoice.pdf")
迁移清单
迁移前
- 识别所有RawPrint用法(
PrintRawStream) - 对每个调用站点进行分类:发送到办公打印机的PDF是IronPDF的候选; ESC/POS,ZPL,EPL或手工构建的PCL/PostScript字节应保留在RawPrint上
- 记录您的应用程序中使用的打印机名称——IronPDF通过操作系统打印系统进行打印,因此名称必须能解析
- 注意任何可以合并的外部 PDF 创建代码
- 从ironpdf.com获取IronPDF许可证密钥
代码更新
- 安装IronPDF包:
dotnet add package IronPdf - 仅在没有剩余的原始字节通道时删除 RawPrint:
dotnet remove package RawPrint - 将 create-then-RawPrint 管道替换为
ChromePdfRenderer加上pdf.Print() - 将PDF创建和打印合并到Windows上的单一工作流程中
- 在应用程序启动时添加许可证初始化
测试
- 测试打印目标打印机
- 验证打印质量
- 测试多个副本
- 测试静音打印
- 如有需要,进行跨平台验证

