IronPDF 操作指南 字体(网络和图标) How to Set Fonts in PDFs Curtis Chau 已更新:七月 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。 快速入门:在PDF生成中使用WebFonts 使用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个步骤) 下载用于在PDF中设置字体的C#库IronPDF 利用HTML从外部来源导入或请求字体 延迟渲染过程以确保正确加载字体 像往常一样将HTML渲染为PDF 在CSS样式中使用@font-face规则导入字体文件 使用网络字体和图标示例 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 探索更多WaitFor选项,如用于字体、JavaScript、HTML元素和网络空闲的选项,请在'IronPDF WaitFor Class Documentation'中查阅。 <hr 导入字体文件示例 要使用现有字体文件,请在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 <hr Azure PDF的局限性 Azure托管平台在其较低的共享网络应用程序层不支持服务器加载SVG字体。 然而,Azure的VPS和Web角色并不以相同方式沙盒处理,并支持网络字体渲染。 常见问题解答 如何使用 .NET 库在 PDF 中设置字体? 您可以通过下载 IronPDF 库、使用 HTML 导入或请求字体、延迟渲染过程直到字体加载完成,以及在 CSS 中应用 @font-face 规则自定义字体来在 PDF 中设置字体。 WaitFor.AllFontsLoaded 方法在 PDF 渲染中的作用是什么? IronPDF 中的 WaitFor.AllFontsLoaded 方法确保所有字体在渲染 PDF 之前完全加载,防止因字体未加载而导致的空白页问题。 在 .NET 应用程序中可以使用 Google Fonts 来创建 PDF 吗? 是的,您可以在 .NET 应用程序中使用 Google Fonts 通过在您的 HTML 中导入字体并在渲染 HTML 到 PDF 之前在您的 CSS 中指定它们来创建 PDF。 如何在 PDF 文档中使用类似 FontAwesome 的图标字体? IronPDF 支持图标字体,例如 FontAwesome,允许您通过在 HTML 和 CSS 中包含图标字体,将可缩放且可定制的图标纳入您的 PDF 文档中。 Azure 主机平台对于 PDF 字体渲染的限制是什么? 在 Azure 主机平台上,较低的共享 Web 应用层不支持 SVG 字体加载,但 Azure 的 VPS 和 Web Role 支持完整的 Web 字体渲染,没有此类限制。 如果我的自定义字体未在 PDF 中显示,我该如何排查? 如果自定义字体未在 PDF 中显示,请确保字体文件在您的 HTML 中正确链接,并使用 WaitFor.AllFontsLoaded 方法增加等待时间,以便在渲染之前字体能正确加载。 使用 CSS 的 @font-face 规则进行 PDF 转换的过程是什么? 要在 PDF 转换中使用 @font-face 规则,请在 CSS 中定义自定义字体及其来源,IronPDF 将在转换 HTML 为 PDF 时应用这些样式,从而允许使用自定义字体。 在使用 WebFonts 进行 PDF 转换时是否需要任何特定设置? 为使用 IronPDF 进行 WebFonts 的 PDF 转换,请确保字体 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 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 准备开始了吗? Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:16,154,058 查看许可证