如何用 C# 設置 PDF 的自訂紙張大小 | IronPDF
從Scryber.Core遷移到IronPDF將您的PDF生成工作從自定義XHTML+CSS佈局引擎轉移到支持全CSS3和JavaScript的基於Chromium的渲染器。 本指南提供了一個逐步的遷移路徑,涵蓋了LGPL v3授權條款的考量、Scryber的XHTML嚴格模板模型,以及兩個引擎之間的渲染差異。
為什麼要從Scryber.Core遷移到IronPDF
理解Scryber.Core
Scryber.Core(由Richard Hewitson維護,目前版本為9.3.1)是一個開源程式庫,使用其自身的佈局引擎解析XHTML+CSS模板——不是一個瀏覽器。 它支持CSS樣式、通過doc.Params,以及SVG,這使得它對希望使用HTML驅動PDF且不依賴於瀏覽器的開發者具有吸引力。
儘管Scryber.Core是一個基於HTML模板的PDF生成的可行選擇,然而一些限制驅使團隊進行遷移。
遷移的主要原因
- LGPL v3授權問題: 如果您修改Scryber自身,則需要披露源程式碼; 在閉源應用中允許靜態連結,但條款仍然限制您如何發佈修改。
- XHTML嚴格輸入: 模板必須是有效的XML——大多數現實世界的HTML在Scryber解析之前需要清理。
- 非瀏覽器渲染器: 自定義XHTML/CSS佈局引擎——現代CSS(網格,完整的Flexbox,容器查詢)和任何JavaScript都不會執行。
- 社區較小: 工作範例少於主流程式庫。
- 無JavaScript執行: 僅靜態渲染——圖表、SPAs和動態小部件必須預渲染。
- 沒有原生URL到PDF: 您必須自己抓取HTML(例如,使用
HttpClient),並將其轉入。 - 社區支持: 商業支持選項有限。
Scryber.Core與IronPDF的比較
| 方面 | Scryber.Core | IronPDF |
|---|---|---|
| 授權 | LGPL v3 | 商業 |
| 渲染引擎 | 自定義XHTML/CSS佈局引擎 | Chromium |
| CSS支持 | 子集(無網格,部分Flexbox) | 完整支持 CSS3 |
| JavaScript | 不執行 | 完整(Chromium V8) |
| 模板綁定 | Handlebars風格{{ }} + <template> |
標準(Razor,RazorLight等) |
| 輸入嚴格性 | 需要格式正確的XHTML | 接受真實世界的HTML |
| 異步支援 | PDFAsync MVC幫助器; sync core |
完整的異步API |
| 文件 | 閱讀文件網站+樣本 | 廣泛的 |
| 社群支持 | 小 | 更大 |
| 商業支持 | 社區支持 | 提供商業支援 |
IronPDF提供商業支持、文件和比Scryber.Core更大的使用者基礎。 該程式庫授權供商業使用,無需履行LGPL義務。
開始之前
前提條件
- .NET環境:.NET Framework 4.6.2+ 或.NET Core 3.1+ / .NET 5/6/7/8/9+
- NuGet存取: 有能力安裝NuGet套件
- IronPDF授權: 從ironpdf.com獲取您的授權金鑰
NuGet包變更
# Remove Scryber.Core
dotnet remove package Scryber.Core
# Install IronPDF
dotnet add package IronPdf
# Remove Scryber.Core
dotnet remove package Scryber.Core
# 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"
完整API參考
名稱空間變更
// Before: Scryber.Core
using Scryber.Components; // Document, Page, etc.
using Scryber.Drawing; // Units, colours
using Scryber.PDF; // RenderOptions / OutputCompressionType
using Scryber.Styles; // Style elements (older XML template usage)
// After: IronPDF
using IronPdf;
using IronPdf.Rendering;
// Before: Scryber.Core
using Scryber.Components; // Document, Page, etc.
using Scryber.Drawing; // Units, colours
using Scryber.PDF; // RenderOptions / OutputCompressionType
using Scryber.Styles; // Style elements (older XML template usage)
// After: IronPDF
using IronPdf;
using IronPdf.Rendering;
Imports Scryber.Components ' Document, Page, etc.
Imports Scryber.Drawing ' Units, colours
Imports Scryber.PDF ' RenderOptions / OutputCompressionType
Imports Scryber.Styles ' Style elements (older XML template usage)
' After: IronPDF
Imports IronPdf
Imports IronPdf.Rendering
核心API對應
| Scryber.Core | IronPDF |
|---|---|
Document.ParseDocument(reader, "", ParseSourceType.DynamicContent) |
renderer.RenderHtmlAsPdf(html) |
Document.ParseDocument(path) |
renderer.RenderHtmlFileAsPdf(path) |
doc.SaveAsPDF(path) |
pdf.SaveAs(path) |
doc.SaveAsPDF(stream) |
pdf.BinaryData (byte[]) |
doc.Info.Title |
pdf.MetaData.Title |
doc.Info.Author |
pdf.MetaData.Author |
@page CSS在模板中 |
renderer.RenderingOptions.PaperSize / 邊距 |
doc.RenderOptions |
renderer.RenderingOptions |
Handlebars {{value}} + doc.Params |
Razor / 字串插值 |
程式碼遷移範例
範例1:基本HTML到PDF轉換
之前(Scryber.Core):
// NuGet: Install-Package Scryber.Core
using Scryber.Components;
using System.IO;
class Program
{
static void Main()
{
// Scryber requires well-formed XHTML wrapped in an html root with the xhtml namespace
string html = @"<html xmlns='http://www.w3.org/1999/xhtml'>
<body><h1>Hello World</h1><p>This is a PDF document.</p></body>
</html>";
using (var reader = new StringReader(html))
using (var doc = Document.ParseDocument(reader, string.Empty, ParseSourceType.DynamicContent))
using (var stream = new FileStream("output.pdf", FileMode.Create))
{
doc.SaveAsPDF(stream);
}
}
}
// NuGet: Install-Package Scryber.Core
using Scryber.Components;
using System.IO;
class Program
{
static void Main()
{
// Scryber requires well-formed XHTML wrapped in an html root with the xhtml namespace
string html = @"<html xmlns='http://www.w3.org/1999/xhtml'>
<body><h1>Hello World</h1><p>This is a PDF document.</p></body>
</html>";
using (var reader = new StringReader(html))
using (var doc = Document.ParseDocument(reader, string.Empty, ParseSourceType.DynamicContent))
using (var stream = new FileStream("output.pdf", FileMode.Create))
{
doc.SaveAsPDF(stream);
}
}
}
Imports Scryber.Components
Imports System.IO
Class Program
Shared Sub Main()
' Scryber requires well-formed XHTML wrapped in an html root with the xhtml namespace
Dim html As String = "<html xmlns='http://www.w3.org/1999/xhtml'>" &
"<body><h1>Hello World</h1><p>This is a PDF document.</p></body>" &
"</html>"
Using reader As New StringReader(html),
doc As Document = Document.ParseDocument(reader, String.Empty, ParseSourceType.DynamicContent),
stream As New FileStream("output.pdf", FileMode.Create)
doc.SaveAsPDF(stream)
End Using
End Sub
End Class
之後(IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
string html = "<html><body><h1>Hello World</h1><p>This is a PDF document.</p></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
string html = "<html><body><h1>Hello World</h1><p>This is a PDF document.</p></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
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><p>This is a PDF document.</p></body></html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")
End Sub
End Class
Scryber.Core將XHTML字串包裹在SaveAsPDF將PDF寫入ChromePdfRenderer。 XHTML命名空間聲明是必需的——Scryber將拒絕不是格式正確的XML的HTML。
IronPDF使用SaveAs()保存。 接受真實世界的HTML5。 參見HTML到PDF文件獲取更多範例。
範例2:URL轉PDF轉換
之前(Scryber.Core):
// NuGet: Install-Package Scryber.Core
// Scryber has no native URL-to-PDF method — fetch the HTML yourself, then parse.
using Scryber.Components;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
using var client = new HttpClient();
string html = await client.GetStringAsync("https://www.example.com");
// Most live HTML is not valid XHTML; expect to clean it
// (e.g., HtmlAgilityPack) before Scryber will parse it.
using (var reader = new StringReader(html))
using (var doc = Document.ParseDocument(reader, string.Empty, ParseSourceType.DynamicContent))
using (var stream = new FileStream("webpage.pdf", FileMode.Create))
{
doc.SaveAsPDF(stream);
}
}
}
// NuGet: Install-Package Scryber.Core
// Scryber has no native URL-to-PDF method — fetch the HTML yourself, then parse.
using Scryber.Components;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
using var client = new HttpClient();
string html = await client.GetStringAsync("https://www.example.com");
// Most live HTML is not valid XHTML; expect to clean it
// (e.g., HtmlAgilityPack) before Scryber will parse it.
using (var reader = new StringReader(html))
using (var doc = Document.ParseDocument(reader, string.Empty, ParseSourceType.DynamicContent))
using (var stream = new FileStream("webpage.pdf", FileMode.Create))
{
doc.SaveAsPDF(stream);
}
}
}
Imports Scryber.Components
Imports System.IO
Imports System.Net.Http
Imports System.Threading.Tasks
Module Program
Async Function Main() As Task
Using client As New HttpClient()
Dim html As String = Await client.GetStringAsync("https://www.example.com")
' Most live HTML is not valid XHTML; expect to clean it
' (e.g., HtmlAgilityPack) before Scryber will parse it.
Using reader As New StringReader(html)
Using doc As Document = Document.ParseDocument(reader, String.Empty, ParseSourceType.DynamicContent)
Using stream As New FileStream("webpage.pdf", FileMode.Create)
doc.SaveAsPDF(stream)
End Using
End Using
End Using
End Using
End Function
End Module
之後(IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://www.example.com");
pdf.SaveAs("webpage.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://www.example.com");
pdf.SaveAs("webpage.pdf");
}
}
Imports IronPdf
Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderUrlAsPdf("https://www.example.com")
pdf.SaveAs("webpage.pdf")
End Sub
End Class
Scryber.Core沒有原生的URL到PDF入口點。 您使用Document.ParseDocument()。 因為Scryber需要格式正確的XHTML,所以大多數現場頁面將需要清理(例如通過HtmlAgilityPack)才能解析。 JavaScript不會執行,因此任何動態渲染的內容將丟失。
IronPDF的RenderUrlAsPdf()通過其Chromium引擎在單次調用中處理抓取、JavaScript執行和CSS渲染。更多資訊請參見我們的教程。
範例3:自定義頁面設置和邊距
之前(Scryber.Core):
// NuGet: Install-Package Scryber.Core
// Page size and margins are normally declared in the XHTML/CSS template;
// the runtime RenderOptions object exposes output-side settings such as compression.
using Scryber.Components;
using Scryber.PDF;
using System.IO;
class Program
{
static void Main()
{
// Page size and margins go in CSS @page; this is the Scryber idiom.
string html = @"<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<style>
@page { size: A4 portrait; margin: 40pt; }
</style>
</head>
<body><h1>Custom PDF</h1><p>With custom margins and settings.</p></body>
</html>";
using (var reader = new StringReader(html))
using (var doc = Document.ParseDocument(reader, string.Empty, ParseSourceType.DynamicContent))
using (var stream = new FileStream("custom.pdf", FileMode.Create))
{
// Output-side options (compression / conformance) are on RenderOptions.
doc.RenderOptions.Compression = OutputCompressionType.FlateDecode;
doc.SaveAsPDF(stream);
}
}
}
// NuGet: Install-Package Scryber.Core
// Page size and margins are normally declared in the XHTML/CSS template;
// the runtime RenderOptions object exposes output-side settings such as compression.
using Scryber.Components;
using Scryber.PDF;
using System.IO;
class Program
{
static void Main()
{
// Page size and margins go in CSS @page; this is the Scryber idiom.
string html = @"<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<style>
@page { size: A4 portrait; margin: 40pt; }
</style>
</head>
<body><h1>Custom PDF</h1><p>With custom margins and settings.</p></body>
</html>";
using (var reader = new StringReader(html))
using (var doc = Document.ParseDocument(reader, string.Empty, ParseSourceType.DynamicContent))
using (var stream = new FileStream("custom.pdf", FileMode.Create))
{
// Output-side options (compression / conformance) are on RenderOptions.
doc.RenderOptions.Compression = OutputCompressionType.FlateDecode;
doc.SaveAsPDF(stream);
}
}
}
Imports Scryber.Components
Imports Scryber.PDF
Imports System.IO
Module Program
Sub Main()
' Page size and margins go in CSS @page; this is the Scryber idiom.
Dim html As String = "<html xmlns='http://www.w3.org/1999/xhtml'>" &
"<head>" &
"<style>" &
"@page { size: A4 portrait; margin: 40pt; }" &
"</style>" &
"</head>" &
"<body><h1>Custom PDF</h1><p>With custom margins and settings.</p></body>" &
"</html>"
Using reader As New StringReader(html),
doc As Document = Document.ParseDocument(reader, String.Empty, ParseSourceType.DynamicContent),
stream As New FileStream("custom.pdf", FileMode.Create)
' Output-side options (compression / conformance) are on RenderOptions.
doc.RenderOptions.Compression = OutputCompressionType.FlateDecode
doc.SaveAsPDF(stream)
End Using
End Sub
End Module
之後(IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Rendering;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.MarginTop = 40;
renderer.RenderingOptions.MarginBottom = 40;
string html = "<html><body><h1>Custom PDF</h1><p>With custom margins and settings.</p></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("custom.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Rendering;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.MarginTop = 40;
renderer.RenderingOptions.MarginBottom = 40;
string html = "<html><body><h1>Custom PDF</h1><p>With custom margins and settings.</p></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("custom.pdf");
}
}
Imports IronPdf
Imports IronPdf.Rendering
Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4
renderer.RenderingOptions.MarginTop = 40
renderer.RenderingOptions.MarginBottom = 40
Dim html As String = "<html><body><h1>Custom PDF</h1><p>With custom margins and settings.</p></body></html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("custom.pdf")
End Sub
End Class
在Scryber.Core中,頁面大小和邊距通過@page CSS在模板中聲明,而OutputCompressionType.FlateDecode)。
IronPDF將頁面幾何移到渲染器上的MarginBottom以毫米計。 頁面幾何位於API表面而不是模板內部。
模板遷移模式
從Scryber綁定遷移到標準模板
Scryber.Core使用XHTML與Handebars風格的<template data-bind='...'>構造重複段。 要遷移,請在標準.NET模板中重新表達這些內容:
Scryber綁定(XHTML + Handlebars):
<p>{{model.Name}}</p>
<p>Total: {{model.Total}}</p>
<template data-bind='{{model.Items}}'>
<p>{{.Name}}: {{.Price}}</p>
</template>
<p>{{model.Name}}</p>
<p>Total: {{model.Total}}</p>
<template data-bind='{{model.Items}}'>
<p>{{.Name}}: {{.Price}}</p>
</template>
IronPDF與C#字串插值:
var items = model.Items.Select(i => $"<li>{i.Name}: {i.Price:C}</li>");
var html = $@"
<p>{model.Name}</p>
<p>Total: {model.Total:C}</p>
<ul>
{string.Join("", items)}
</ul>";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
var items = model.Items.Select(i => $"<li>{i.Name}: {i.Price:C}</li>");
var html = $@"
<p>{model.Name}</p>
<p>Total: {model.Total:C}</p>
<ul>
{string.Join("", items)}
</ul>";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(html);
Dim items = model.Items.Select(Function(i) $"<li>{i.Name}: {i.Price:C}</li>")
Dim html = $"
<p>{model.Name}</p>
<p>Total: {model.Total:C}</p>
<ul>
{String.Join("", items)}
</ul>"
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf(html)
因為IronPDF接受普通HTML,您可以使用任何.NET模板引擎——Razor,RazorLight,Handlebars.Net——而不是Scryber的{{ }} + <template>構造。
頁眉和頁腳遷移
Scryber.Core(XHTML + @page 邊距盒):
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<style>
@page {
@top-center { content: 'Company Report'; }
@bottom-center { content: 'Page ' counter(page) ' of ' counter(pages); }
}
</style>
</head>
<body>
<h1>Content Here</h1>
</body>
</html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<style>
@page {
@top-center { content: 'Company Report'; }
@bottom-center { content: 'Page ' counter(page) ' of ' counter(pages); }
}
</style>
</head>
<body>
<h1>Content Here</h1>
</body>
</html>
IronPDF(HTML頁眉/頁腳):
using IronPdf;
var renderer = new ChromePdfRenderer();
// HTML header with full CSS support
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
HtmlFragment = @"
<div style='width: 100%; text-align: center; font-size: 12pt; border-bottom: 1px solid #ccc;'>
Company Report
</div>",
MaxHeight = 30
};
// HTML footer with page numbers
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter
{
HtmlFragment = @"
<div style='width: 100%; text-align: center; font-size: 10pt;'>
Page {page} of {total-pages}
</div>",
MaxHeight = 25
};
var pdf = renderer.RenderHtmlAsPdf("<h1>Content Here</h1>");
pdf.SaveAs("report.pdf");
using IronPdf;
var renderer = new ChromePdfRenderer();
// HTML header with full CSS support
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
HtmlFragment = @"
<div style='width: 100%; text-align: center; font-size: 12pt; border-bottom: 1px solid #ccc;'>
Company Report
</div>",
MaxHeight = 30
};
// HTML footer with page numbers
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter
{
HtmlFragment = @"
<div style='width: 100%; text-align: center; font-size: 10pt;'>
Page {page} of {total-pages}
</div>",
MaxHeight = 25
};
var pdf = renderer.RenderHtmlAsPdf("<h1>Content Here</h1>");
pdf.SaveAs("report.pdf");
Imports IronPdf
Dim renderer As New ChromePdfRenderer()
' HTML header with full CSS support
renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter With {
.HtmlFragment = "
<div style='width: 100%; text-align: center; font-size: 12pt; border-bottom: 1px solid #ccc;'>
Company Report
</div>",
.MaxHeight = 30
}
' HTML footer with page numbers
renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter With {
.HtmlFragment = "
<div style='width: 100%; text-align: center; font-size: 10pt;'>
Page {page} of {total-pages}
</div>",
.MaxHeight = 25
}
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Content Here</h1>")
pdf.SaveAs("report.pdf")
Scryber.Core通過CSS的counter(pages)表示頁碼。 IronPDF使用全HTML/CSS的{total-pages}。
遷移後的新能力
遷移到IronPDF後,您獲得了Scryber.Core無法提供的功能:
PDF合併
var pdf1 = PdfDocument.FromFile("chapter1.pdf");
var pdf2 = PdfDocument.FromFile("chapter2.pdf");
var pdf3 = PdfDocument.FromFile("chapter3.pdf");
var merged = PdfDocument.Merge(pdf1, pdf2, pdf3);
merged.SaveAs("complete_book.pdf");
var pdf1 = PdfDocument.FromFile("chapter1.pdf");
var pdf2 = PdfDocument.FromFile("chapter2.pdf");
var pdf3 = PdfDocument.FromFile("chapter3.pdf");
var merged = PdfDocument.Merge(pdf1, pdf2, pdf3);
merged.SaveAs("complete_book.pdf");
Dim pdf1 = PdfDocument.FromFile("chapter1.pdf")
Dim pdf2 = PdfDocument.FromFile("chapter2.pdf")
Dim pdf3 = PdfDocument.FromFile("chapter3.pdf")
Dim merged = PdfDocument.Merge(pdf1, pdf2, pdf3)
merged.SaveAs("complete_book.pdf")
安全性和元資料
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Confidential</h1>");
// Metadata
pdf.MetaData.Title = "My Document";
pdf.MetaData.Author = "John Doe";
pdf.MetaData.Subject = "Annual Report";
pdf.MetaData.Keywords = "report, annual, confidential";
// Security
pdf.SecuritySettings.OwnerPassword = "owner123";
pdf.SecuritySettings.UserPassword = "user456";
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights;
pdf.SaveAs("protected.pdf");
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Confidential</h1>");
// Metadata
pdf.MetaData.Title = "My Document";
pdf.MetaData.Author = "John Doe";
pdf.MetaData.Subject = "Annual Report";
pdf.MetaData.Keywords = "report, annual, confidential";
// Security
pdf.SecuritySettings.OwnerPassword = "owner123";
pdf.SecuritySettings.UserPassword = "user456";
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights;
pdf.SaveAs("protected.pdf");
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Confidential</h1>")
' Metadata
pdf.MetaData.Title = "My Document"
pdf.MetaData.Author = "John Doe"
pdf.MetaData.Subject = "Annual Report"
pdf.MetaData.Keywords = "report, annual, confidential"
' Security
pdf.SecuritySettings.OwnerPassword = "owner123"
pdf.SecuritySettings.UserPassword = "user456"
pdf.SecuritySettings.AllowUserCopyPasteContent = False
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights
pdf.SaveAs("protected.pdf")
功能比較總結
| 功能 | Scryber.Core | IronPDF |
|---|---|---|
| HTML轉PDF | XHTML+CSS通過自定義佈局引擎 | 完整的Chromium |
| URL到PDF | 僅手動提取 | 原生RenderUrlAsPdf |
| CSS網格 | 不支持 | 全面支持 |
| Flexbox | 部分 | 全面支持 |
| JavaScript | 不執行 | 完整(V8) |
| 資料綁定 | {{ }} Handlebars + doc.Params |
Razor / RazorLight / 插值 |
| 頁眉/頁腳 | @page 邊距盒 / 模板段 |
{page} {total-pages} |
| 合併PDF | 不支持 | 內建的PdfDocument.Merge |
| 拆分PDF | 不支持 | 是 |
| 浮水印 | 通過模板中的重疊HTML | 原生ApplyStamp / ApplyWatermark |
| 數位簽名 | 不支持 | 是 |
| PDF/A | 不支持 | 是的(PDF/A-1b,PDF/A-3) |
| 密碼保護 | 不支持 | 完整(使用者/所有者+權限) |
| 異步支援 | PDFAsync MVC幫助器; sync core |
完整的異步表面 |
| 跨平臺 | 是的(.NET Std 2.0/2.1, .NET 8/9/10) | 是 |
遷移檢查表
遷移前
- 審核所有Scryber模板以獲得XHTML/綁定模式
- 記錄使用的資料綁定模式(
{{model.Property}}) - 確定需要CSS轉換的自定義樣式
- 獲取IronPDF授權金鑰來自ironpdf.com
程式碼更新
- 移除
Scryber.CoreNuGet包 - 安裝
IronPdfNuGet包 - 更新命名空間引入(
using Scryber.Components;/Scryber.Drawing;/Scryber.PDF;→using IronPdf;) - 用
Document.ParseDocument(reader, "", ParseSourceType.DynamicContent) - 用
doc.SaveAsPDF(stream) - 將XHTML模板轉換為普通HTML5(無需命名空間要求)
- 用標準模板(Razor / 字串插值)替換
{{ }}Handlebars綁定 - 將
@pageCSS移到renderer.RenderingOptions(PaperSize, MarginTop等) - 將
{total-pages}佔位符 - 在應用程式啟動時新增授權初始化
測試
- 測試所有文件模板
- 驗證樣式是否匹配(利用完整的CSS支持)
- 測試新的模板中的資料綁定
- 驗證分頁斷點
- 使用頁碼佔位符測試頁眉/頁腳
- 性能比較
[{(Scryber是其相關所有者的註冊商標。 本網站未與PerceiveIT Limited關聯、未經其認可或贊助。 所有產品名稱、標誌和品牌均為其各自所有者的財產。 比較僅供參考,反映撰寫時公開可用的資訊。)}]

