如何在 C# 中設定 PDF 的字型

使用 IronPDF C# 在 PDF 中設定字體

This article was translated from English: Does it need improvement?
Translated
View the article in English

網頁字體是專為網站設計的字體。 這些字體託管在網站伺服器上,並透過網頁瀏覽器下載,以確保網站上的文字渲染效果一致且美觀,而無需考慮使用者本地字體的可用性。 此外,使用符號和字形的圖標字體經常用於網頁設計中,以創建可縮放、可自訂的圖標,並透過 CSS 操作保持視覺上一致的使用者介面。

CSS 包含網頁字體,可讓您指定在造訪您的網站時要下載的字體檔案。 IronPDF支援從HTML載入字體並將其渲染為PDF。

快速入門:在 PDF 生成中使用 Web 字體

使用 IronPDF 強大的 C# 庫,輕鬆將網頁字體和圖示字體融入您的 PDF 中。 本快速指南將向您展示如何使用自訂字體渲染 HTML 內容,從而確保產生一致且美觀的 PDF 文件。 只需使用 IronPDF 渲染 HTML,即可在幾秒鐘內儲存樣式化的文件。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronPDF

    PM > Install-Package IronPdf

  2. 複製並運行這段程式碼。

    new IronPdf.ChromePdfRenderer { RenderingOptions = { WaitFor = IronPdf.Rendering.WaitFor.AllFontsLoaded(2000) } }
        .RenderHtmlAsPdf("<link href=\"https://fonts.googleapis.com/css?family=Lobster\" rel=\"stylesheet\">" +
                         "<link href=\"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css\" rel=\"stylesheet\">" +
                         "<p style=\"font‑family:'Lobster', serif; font‑size:30px;\">Hello Google Font</p>" +
                         "<i class=\"fa fa‑coffee\" style=\"font‑size:40px; color:#b00;\"></i>")
        .SaveAs("webfonts‑icons.pdf");
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronPDF,免費試用!
    arrow pointer

使用 Web 字體和圖示範例

IronPDF 支援WebFonts (例如 Google Fonts 和 Adobe Web Font API)和圖示字體,例如 Bootstrap 和FontAwesome使用的字體。

字體通常需要延遲渲染才能正確載入。 如果字體載入不正確,可能會導致頁面空白,沒有任何文字。 您可以使用WaitFor.AllFontsLoaded方法,透過為其分配最大等待時間來等待所有字體載入完畢。 預設最大等待時間為 500 毫秒。

以下是如何在專案中使用名為 Lobster 的WebFont的小範例。

:path=/static-assets/pdf/content-code-examples/how-to/webfonts-webicons-render-webfont.cs
using IronPdf;

// HTML contains webfont
var html = @"<link href=""https://fonts.googleapis.com/css?family=Lobster"" rel=""stylesheet"">
<p style=""font-family: 'Lobster', serif; font-size:30px;"" > Hello Google Fonts</p>";

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Wait for font to load
renderer.RenderingOptions.WaitFor.AllFontsLoaded(2000);

// Render HTML to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

// Export the PDF
pdf.SaveAs("font-test.pdf");
Imports IronPdf

' HTML contains webfont
Private html = "<link href=""https://fonts.googleapis.com/css?family=Lobster"" rel=""stylesheet"">
<p style=""font-family: 'Lobster', serif; font-size:30px;"" > Hello Google Fonts</p>"

Private renderer As New ChromePdfRenderer()

' Wait for font to load
renderer.RenderingOptions.WaitFor.AllFontsLoaded(2000)

' Render HTML to PDF
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)

' Export the PDF
pdf.SaveAs("font-test.pdf")
$vbLabelText   $csharpLabel

探索更多WaitFor選項,例如字體、JavaScript、HTML 元素和網路空閒的 WaitFor 選項,請參閱" IronPDF WaitFor 類別文件"。


導入字型檔案範例

若要使用現有的字體文件,請在 CSS 樣式中套用@font-face規則。 當結合使用 @font-face 規則和嵌入 base64 編碼的 woff 檔案時,它也能正常運作。 在下面的範例中,我將使用Pixelify Sans 字體

:path=/static-assets/pdf/content-code-examples/how-to/webfonts-webicons-custom-font.cs
using IronPdf;

// Import custom font
string html = @"<!DOCTYPE html>
<html>
<head>
<style>
@font-face {font-family: 'Pixelify';
src: url('fonts\PixelifySans-VariableFont_wght.ttf');
}
p {
    font-family: 'Pixelify';
    font-size: 70px;
}
</style>
</head>
<body>
<p>Custom font</p>
</body>
</html>";

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render HTML to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

// Export the PDF
pdf.SaveAs("customFont.pdf");
Imports IronPdf

' Import custom font
Private html As String = "<!DOCTYPE html>
<html>
<head>
<style>
@font-face {font-family: 'Pixelify';
src: url('fonts\PixelifySans-VariableFont_wght.ttf');
}
p {
    font-family: 'Pixelify';
    font-size: 70px;
}
</style>
</head>
<body>
<p>Custom font</p>
</body>
</html>"

Private renderer As New ChromePdfRenderer()

' Render HTML to PDF
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)

' Export the PDF
pdf.SaveAs("customFont.pdf")
$vbLabelText   $csharpLabel

Azure PDF 的局限性

Azure 託管平台不支援伺服器在其較低共用 Web 應用程式層載入 SVG 字型。 然而,Azure 的 VPS 和 Web 角色並非以相同的方式進行沙盒隔離,並且支援 Web 字體渲染。

常見問題解答

如何使用 .NET 函式庫在 PDF 中設定字型?

您可以使用 IronPDF 在 PDF 中設定字型,方法包括下載 IronPDF 函式庫、使用 HTML 匯入或請求字型、延遲渲染程序直到載入字型,以及在 CSS 中應用 @font-face 自訂字型的規則。

在 PDF 渲染中,WaitFor.AllFontsLoaded 方法的作用是什麼?

IronPDF 中的 WaitFor.AllFontsLoaded 方法可確保在呈現 PDF 之前,所有字型都已完全載入,避免出現未載入字型所造成的空白頁等問題。

您可以在 .NET 應用程式中使用 Google Fonts 來建立 PDF 嗎?

是的,您可以在 IronPDF for .NET 應用程式中使用 Google Fonts 來製作 PDF,方法是在 HTML 中匯入字型,並在將 HTML 渲染成 PDF 之前在 CSS 中指定字型。

如何在 PDF 文件中使用 FontAwesome 等圖示字型?

IronPDF 支援 FontAwesome 等圖示字型,讓您可以在 HTML 和 CSS 中加入圖示字型,將可調整大小、可自訂的圖示加入 PDF 文件中。

Azure 主機對 PDF 字型渲染有哪些限制?

在 Azure 主機平台上,較低的共用網頁應用程式層級不支援 SVG 字型載入,但 Azure 的 VPS 和 Web Role 則支援完整的網頁字型渲染,沒有這些限制。

如果我的自訂字型沒有出現在 PDF 中,該如何排除故障?

如果自訂字型沒有出現在 PDF 中,請確保字型檔案在 HTML 中有正確的連結,並使用 WaitFor.AllFontsLoaded 方法增加等待時間,讓字型在呈現之前能正常載入。

在 CSS 中使用 @font-face 規則進行 PDF 轉換的流程為何?

若要在 CSS 中使用 @font-face 規則進行 PDF 轉換,請在 CSS 中定義自訂字型及其來源,IronPDF 將會在 HTML 轉換為 PDF 時套用這些樣式,允許使用自訂字型。

在 PDF 轉換中使用 WebFonts 是否需要任何特定的設定?

要在 IronPDF 的 PDF 轉換中使用 WebFonts,請確保可以存取字型 URL,透過連結或匯入將字型包含在 HTML 中,並在 CSS 中指定字型。使用 WaitFor.AllFontsLoaded 方法來管理字型載入。

以 .NET 10 為目標時,IronPDF 是否支援 WebFonts 和圖示字型?

是的,IronPDF 與 .NET 10 完全相容,並支援 WebFonts(例如 Google Fonts 或 Adobe 網路字體)以及 FontAwesome 等圖示字體。API 在 .NET 10 下可以無縫運作,因此您可以使用 HTML/CSS 來匯入和渲染自訂字型和圖示字型,就像在早期的 .NET 版本中一樣。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 16,493,056 | Version: 2025.11 剛發表