IronPDF 操作指南 字體(網頁和圖示) How to Set Fonts in PDFs Curtis Chau 更新日期:7月 22, 2025 Download IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English 網字體是專為網站使用而設計的特殊字體。 這些字體托管在網絡伺服器上,並由網絡瀏覽器下載,以確保在網站上呈現一致且具有視覺吸引力的文字,不論使用者的本地字體可用性如何。 此外,使用符號和字形的圖標字體通常用於網頁設計中,以創建可擴展、可定制的圖標,並通過 CSS 操作保持視覺一致的用戶界面。 CSS 包含網字體,使您能夠在訪問網站時指定要下載的字體文件。 IronPDF 支援從 HTML 加載和渲染字體到 PDF。 作為標題:2(快速入門:在 PDF 生成中使用網字體) 使用 IronPDF 強大的 C# 函式庫,輕鬆將網字體和圖標字體融入您的 PDF 中。 這個快速指南教您如何使用自定義字體渲染 HTML 內容,確保 PDF 的一致性和視覺吸引力。 只需使用 IronPDF 呈現 HTML,並在幾秒鐘內保存您設計的文檔。 Get started making PDFs with NuGet now: Install IronPDF with NuGet Package Manager PM > Install-Package IronPdf Copy and run this code snippet. 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"); Deploy to test on your live environment Start using IronPDF in your project today with a free trial Free 30 day Trial class="hsg-featured-snippet"> 簡易工作流程 (5 步驟) 下載 C# 函式庫 IronPDF 以在 PDF 中設置字體 利用 HTML 從外部來源導入或請求字體 延遲渲染過程以確保正確加載字體 如常將 HTML 渲染為 PDF 在 CSS 樣式中使用 @font-face 規則導入字體文件 使用 WebFonts 和圖標的範例 IronPDF supports WebFonts (such as Google Fonts and the Adobe web font API) and icon fonts, such as those used by Bootstrap and 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 在 'IronPDF WaitFor 類文件' 上探索更多 WaitFor 選項,例如字體、JavaScript、HTML 元素和網絡空閒的選項。 導入字體文件範例 要使用現有字體文件,請在 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 托管平台不支援在其較低的共享網絡應用程式層中加載 SVG 字體的伺服器。 然而,Azure 的 VPS 和 Web Role 並不以相同方式沙箱處理,並支援網字體渲染。 常見問題解答 如何使用 .NET 函式庫在 PDF 中設定字型? 您可以使用 IronPDF 在 PDF 中設定字體,方法是下載 IronPDF 庫,使用 HTML 匯入或請求字體,延遲渲染過程直到字體加載完畢,並在 CSS 中應用@font-face規則來設定自訂字體。 WaitFor.AllFontsLoaded 方法在 PDF 渲染中扮演什麼角色? IronPDF 中的 WaitFor.AllFontsLoaded 方法可確保在渲染 PDF 之前所有字體都已完全加載,從而防止因字體未加載而導致空白頁等問題。 能否在用於建立 PDF 的 .NET 應用程式中使用 Google Fonts? 是的,您可以在 .NET 應用程式中使用 Google Fonts 和 IronPDF 建立 PDF,方法是在 HTML 中匯入字體,並在將 HTML 渲染為 PDF 之前在 CSS 中指定它們。 如何在 PDF 文件中使用 FontAwesome 等圖示字體? IronPDF 支援 FontAwesome 等圖示字體,可讓您透過在 HTML 和 CSS 中包含圖示字體,將可縮放、可自訂的圖示新增至 PDF 文件中。 Azure託管對PDF字體渲染有哪些限制? 在 Azure 託管平台上,較低的共用 Web 應用層不支援 SVG 字體加載,但 Azure 的 VPS 和 Web 角色支援完整的 Web 字體渲染,沒有此類限制。 如果我的自訂字體沒有顯示在 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` 方法來管理字體載入。 IronPDF 在 .NET 10 導向時是否支援 WebFonts 和圖示字體? 是的,IronPDF 完全相容於 .NET 10,並支援 Web 字體(例如 Google Fonts 或 Adobe Web Fonts)和圖示字體(例如 FontAwesome)。該 API 在 .NET 10 下可以無縫運行,因此您可以像在早期 .NET 版本中一樣,使用 HTML/CSS 匯入和渲染自訂字體和圖示字體。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 準備好開始了嗎? Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布 免費 NuGet 下載 總下載量:16,154,058 查看許可證