如何在 C#| IronPDF 中從模板生成 PDF
從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上未列出/過時)。 它是winspool.Drv之上的一個薄P/Invoke包裝器,將字節流直接傳送到Windows的RAW型別資料列印緩衝區。 公共API很小:在PrintRawStream重載。 底層Win32調用(ClosePrinter)隱藏在該接口之後——在使用包時,您不需要自己編寫它們。
這使得RawPrint非常適合ESC/POS收據列印機、ZPL/EPL斑馬標籤列印機和您已經生成了列印機固件預期字節的舊PCL/PostScript作業。 對於針對Windows特定環境並需要直接列印機通信的開發人員來說,RawPrint提供了一種有效的途徑,繞過了驅動程式或圖形介面等中介層。
然而,RawPrint不是一個PDF程式庫——它只將資料推送到列印機。 如果您的應用程式目前使用其他程式庫建立PDF然後將其交給RawPrint,整個管道通常可以在Windows上整合為IronPDF。
核心問題:無PDF建立功能
RawPrint具有顯著的限制,使其不適合現代文件工作流程:
-
無PDF建立功能: RawPrint專注於資料傳輸,缺乏PDF建立、渲染或操作的功能。
-
非常底層: 由於直接處理原始字節,開發人員必須對列印機的命令語言有深入了解,這使得它不太適合簡單的文件列印任務。
-
平台特定: 依賴於Windows列印緩衝區,限制了其跨平台的適用性。
-
無文件處理: 僅僅是字節傳輸,沒有渲染功能。
- 控制限制: 列印作業配置選項極少。
RawPrint與IronPDF對比
| 功能 | RawPrint | IronPDF |
|---|---|---|
| 功能性 | 直接將原始列印資料發送到列印機 | 全面的PDF建立和操作 |
| 使用案例 | 專業化列印如標籤 | 通用文件管理和建立 |
| 平台依賴性 | Windows特定 | 跨平台 |
| 複雜性 | 底層,需要列印機命令知識 | 高層,使用者友好的API |
| PDF建立 | 否 | 是 |
| 從HTML建立PDF | 否 | 是 |
| 從URL建立PDF | 否 | 是 |
| 編輯/修改PDF | 否 | 是 |
| 合併/拆分PDF | 否 | 是 |
| 列印已有PDF | 是(原始字節) | 是(高層API) |
| 列印控制 | 基本 | 全選項 |
| 理想物件 | 直接列印機存取需求 | 網路和桌面應用中的PDF相關任務 |
| 靈活性 | 由於處理原始字節而限制 | 具有多種功能的廣泛靈活性 |
與RawPrint截然不同,IronPDF提供了一個強大而多才多藝的API來全面處理PDF。 作為.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" .
注意:手工撰寫的P/Invoke包裝器圍繞RawPrinterHelper,看起來與RawPrint用法表面上類似,但這不是包——它們直接調用Win32緩衝區。 包本身僅公開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生成PDF |
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
之後(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
之後(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
之後(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 | 是 | 否 |
| 通過操作系統列印系統列印 | 否 | 是 |
| 平台 | ||
| Windows | 是 | 是 |
| Linux | 否 | 是 |
| macOS | 否 | 是 |
| Docker | 否 | 是 |
| 其他 | ||
| 加密/安全 | 否 | 是 |
| 數位簽名 | 否 | 是 |
| 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授權金鑰來自ironpdf.com
程式碼更新
- 安裝IronPDF套件:
dotnet add package IronPdf - 僅在沒有剩餘原始字節通道的情況下才能刪除RawPrint:
dotnet remove package RawPrint - 將建立後再原始列印管道替換為
pdf.Print() - 將PDF建立和列印整合到Windows上的單一工作流程中
- 在應用程式啟動時新增授權初始化
測試
- 測試列印到目標列印機
- 驗證列印質量
- 測試多份副本
- 測試靜默列印
- 如有必要,進行跨平台驗證

