如何在HTML到PDF转换中管理字体

如何使用 C# 管理 PDF 中的字体

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

IronPDF 可在 C# 中实现全面的字体管理,包括在 PDF 文档中查找、添加、嵌入、解嵌入和替换字体,以确保在所有平台上显示一致的文本。

字体是一组具有一致风格和设计的字符、符号和字形。 它代表特定的字体、大小、粗细和样式(如常规、粗体、斜体等)。 字体用于排版,以视觉上吸引人的一致方式展示文本。

IronPDF 提供了一种方便的方式来管理字体,提供的功能包括查找字体、获取字体、嵌入字体、取消嵌入字体和替换字体。 无论您是创建新的 PDF 还是编辑现有文档,正确的字体管理都能确保您的 PDF 在所有平台和设备上正确显示。

快速入门:管理和嵌入 PDF 中的字体

开始使用 IronPDF 来简化 PDF 文档中的字体管理。 本指南介绍了如何在 PDF 中嵌入字体,以实现跨平台的视觉一致性。 只需几行代码,您就可以增强文档外观并保持兼容性。

  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronPdf

    PM > Install-Package IronPdf
  2. 复制并运行这段代码。

    ChromePdfRenderer renderer = new ChromePdfRenderer();
    PdfDocument pdf = renderer.RenderHtmlAsPdf("<p style='font-family:MyCustomFont;'>Hello world!</p>");
    pdf.Fonts.Add(File.ReadAllBytes("MyCustomFont.ttf")).Embed();
    pdf.SaveAs("withCustomFont.pdf");
  3. 部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronPDF

    arrow pointer

如何在 PDF 中查找和检索字体?

如何检索 PDF 中的所有字体?

访问PdfFontCollection对象。 可以通过遍历Fonts属性。 这在使用 IronPDF 表单或分析文档结构时尤其有用。

:path=/static-assets/pdf/content-code-examples/how-to/manage-font-retrieve-font.cs
using IronPdf;
using IronPdf.Fonts;
using System.Collections.Generic;

// Import PDF
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Retreive font
PdfFontCollection fonts = pdf.Fonts;
Imports IronPdf
Imports IronPdf.Fonts
Imports System.Collections.Generic

' Import PDF
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Retreive font
Private fonts As PdfFontCollection = pdf.Fonts
$vbLabelText   $csharpLabel

如何按名称查找特定字体?

using IronPDF 可以直接查找特定字体。 使用PdfFontCollection对象,指定字体名称以访问字体对象和检查属性。 当您需要替换 PDF 文档中的文本,同时保持字体一致性时,这一功能就显得至关重要。

:path=/static-assets/pdf/content-code-examples/how-to/manage-font-find-font.cs
using IronPdf;
using IronPdf.Fonts;
using System.Collections.Generic;
using System.Linq;

// Import PDF
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Find font
PdfFont font = pdf.Fonts["SpecialFontName"];
Imports IronPdf
Imports IronPdf.Fonts
Imports System.Collections.Generic
Imports System.Linq

' Import PDF
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Find font
Private font As PdfFont = pdf.Fonts("SpecialFontName")
$vbLabelText   $csharpLabel
Icon Quote related to 如何按名称查找特定字体?

我最喜欢的这种库是 IronPDF。它允许快速高效地操作 PDF 文件。它还具有许多有价值的功能,比如导出为 PDF/A 格式和数字签名 PDF 文档。

Milan Jovanovic related to 如何按名称查找特定字体?

Milan Jovanovic

微软MVP

查看案例研究
Icon Quote related to 如何按名称查找特定字体?

IronOCR 意味着我们每年可以节省 $40,000 的人工处理成本,同时提高生产力,并释放资源用于高影响任务。我强烈推荐它。

Brent Matzelle related to 如何按名称查找特定字体?

Brent Matzelle

首席技术官,OPYN

查看案例研究
Icon Quote related to 如何按名称查找特定字体?

Iron Suite 在我们的运营中起着至关重要的作用。这些工具提高了业务各方面的效率,包括创建平面图和改善库存管理。

David Jones related to 如何按名称查找特定字体?

David Jones

首席软件工程师,Agorus Build

查看案例研究

如何在 PDF 中添加字体?

使用Add方法将标准字体或字体文件作为字节数据添加。 接受字体名称的方法只接受 14 种标准字体中的一种。 添加标准字体并不会嵌入这些字体,因为它们保证在操作系统中可用。 将 HTML 转换为 PDF 时,IronPDF 会自动处理 HTML 中的网络字体。

:path=/static-assets/pdf/content-code-examples/how-to/manage-font-add-font.cs
using IronPdf;
using IronPdf.Fonts;

// Import PDF
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Add font
pdf.Fonts.Add("Helvetica");
Imports IronPdf
Imports IronPdf.Fonts

' Import PDF
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Add font
pdf.Fonts.Add("Helvetica")
$vbLabelText   $csharpLabel

对于包括 web 字体和图标字体在内的高级字体管理,IronPDF 可为现代排版需求提供全面支持。

为什么要在 PDF 中嵌入字体?

嵌入字体包括 PDF 文档中的字体字节流数据。 这样可以确保正确显示,而无需在查看系统上安装字体。 虽然这会增加文件的大小,但却能保证视觉上的一致性。 这在创建 PDF/A 兼容文档或确保文档可移植性时至关重要。

:path=/static-assets/pdf/content-code-examples/how-to/manage-font-embed-font.cs
using IronPdf;
using System.Linq;

// Import PDF
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Add the font
byte[] fontData = System.IO.File.ReadAllBytes("dir/to/font.ttf");

// Embed the font
pdf.Fonts.Last().Embed(fontData);
Imports IronPdf
Imports System.Linq

' Import PDF
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Add the font
Private fontData() As Byte = System.IO.File.ReadAllBytes("dir/to/font.ttf")

' Embed the font
pdf.Fonts.Last().Embed(fontData)
$vbLabelText   $csharpLabel

字体嵌入对于国际语言和 UTF-8 支持尤为重要,以确保字符在所有系统中正确显示。

何时应将字体从 PDF 文件中解嵌?

取消嵌入从PDF中移除嵌入的字体字节流数据以减小文件大小。使用Unembed方法实现此目的。 这种技术与 PDF 压缩策略配合使用效果很好,可以最大限度地减小文件大小。

:path=/static-assets/pdf/content-code-examples/how-to/manage-font-unembed-font.cs
using IronPdf;
using IronPdf.Fonts;

// Import PDF
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Get fonts
PdfFontCollection fonts = pdf.Fonts;

// Unembed a font
pdf.Fonts[0].Unembed();
Imports IronPdf
Imports IronPdf.Fonts

' Import PDF
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Get fonts
Private fonts As PdfFontCollection = pdf.Fonts

' Unembed a font
pdf.Fonts(0).Unembed()
$vbLabelText   $csharpLabel

如果不常用的字体在解嵌入后缺乏回退功能,它们可能会在输出的 PDF 中出现损坏。 检查 Adobe 是否显示相同的问题--如果是,这是预期行为。 如果不符合要求,请联系支持部门进行调查。 损坏的字体显示如下:

Adobe Acrobat 字体错误对话框显示缺少 AAAAAA+Impact 字体警告,显示乱码文本

如何替换 PDF 文档中的字体?

字体替换保留原始字体数据结构,包括样式和字符编码,同时替换指定字体。 确保新字体与原字体保持一致。 在编辑 PDF 文件或跨文档标准化字体时,此功能非常有用。

在极少数情况下,视觉效果可能无法完全匹配。 这是目前字体替换方法的局限性。

:path=/static-assets/pdf/content-code-examples/how-to/manage-font-replace-font.cs
using IronPdf;
using IronPdf.Fonts;
using System.Linq;

// Import PDF
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

byte[] fontData = System.IO.File.ReadAllBytes("dir/to/font.ttf");
// Get and replace Font
pdf.Fonts["Courier"].ReplaceWith(fontData);
Imports IronPdf
Imports IronPdf.Fonts
Imports System.Linq

' Import PDF
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

Private fontData() As Byte = System.IO.File.ReadAllBytes("dir/to/font.ttf")
' Get and replace Font
pdf.Fonts("Courier").ReplaceWith(fontData)
$vbLabelText   $csharpLabel

14 种标准 PDF 字体是什么?

14 种标准字体(Base 14 Fonts 或 Standard Type 1 Fonts)在 PDF 阅读器中得到广泛支持,无需嵌入。 PDF 标准保证了这些字体在处理 PDF 文档时可用。 在从各种来源创建 PDF 时,这些字体对于保持兼容性至关重要。

  • Courier
  • Courier-Bold
  • Courier-Oblique
  • Courier-BoldOblique
  • Helvetica
  • Helvetica-Bold
  • Helvetica-Oblique
  • Helvetica-BoldOblique
  • Times-Roman
  • Times-Bold
  • Times-Italic
  • Times-BoldItalic
  • Symbol
  • ZapfDingbats

哪些字体名称可以映射到标准字体?

为方便起见,多个字符串名称指向同一标准字体。 在使用不同的 PDF 工具以及 合并或拆分 PDF 时,该映射系统可确保兼容性。

映射到 Courier

  • StandardFont.Courier
    • Courier
    • CourierNew
    • CourierNewPSMT
    • CourierStd

映射到 Courier-Bold

  • StandardFont.CourierBold
    • Courier,Bold
    • Courier-Bold
    • CourierBold
    • CourierNew,Bold
    • CourierNew-Bold
    • CourierNewBold
    • CourierNewPS-BoldMT
    • CourierStd-Bold

映射到 Courier-Oblique

  • StandardFont.CourierOblique
    • Courier,Italic
    • Courier-Oblique
    • CourierItalic
    • CourierNew,Italic
    • CourierNew-Italic
    • CourierNewItalic
    • CourierNewPS-ItalicMT
    • CourierStd-Oblique

映射到 Courier-BoldOblique

  • StandardFont.CourierBoldOblique
    • Courier,BoldItalic
    • Courier-BoldOblique
    • CourierBoldItalic
    • CourierNew,BoldItalic
    • CourierNew-BoldItalic
    • CourierNewBoldItalic
    • CourierNewPS-BoldItalicMT
    • CourierStd-BoldOblique

映射到 Helvetica

  • StandardFont.Helvetica
    • Arial
    • ArialMT
    • Helvetica

映射到 Helvetica-Bold

  • StandardFont.HelveticaBold
    • Arial,Bold
    • Arial-Bold
    • Arial-BoldMT
    • ArialBold
    • ArialMT,Bold
    • ArialRoundedMTBold
    • Helvetica,Bold
    • Helvetica-Bold
    • HelveticaBold

映射到 Helvetica-Oblique

  • StandardFont.HelveticaOblique
    • Arial,Italic
    • Arial-Italic
    • Arial-ItalicMT
    • ArialItalic
    • ArialMT,Italic
    • Helvetica,Italic
    • Helvetica-Italic
    • Helvetica-Oblique
    • HelveticaItalic

映射到 Helvetica-BoldOblique

  • StandardFont.HelveticaBoldOblique
    • Arial,BoldItalic
    • Arial-BoldItalic
    • Arial-BoldItalicMT
    • ArialBoldItalic
    • ArialMT,BoldItalic
    • Helvetica,BoldItalic
    • Helvetica-BoldItalic
    • Helvetica-BoldOblique
    • HelveticaBoldItalic

映射到 Times-Roman

  • StandardFont.Times
    • Times-Roman
    • TimesNewRoman
    • TimesNewRomanPS
    • TimesNewRomanPSMT

映射到 Times-Bold

  • StandardFont.TimesBold
    • Times-Bold
    • TimesBold
    • TimesNewRoman,Bold
    • TimesNewRoman-Bold
    • TimesNewRomanBold
    • TimesNewRomanPS-Bold
    • TimesNewRomanPS-BoldMT
    • TimesNewRomanPSMT,Bold

映射到 Times-Italic

  • StandardFont.TimesOblique
    • Times-Italic
    • TimesItalic
    • TimesNewRoman,Italic
    • TimesNewRoman-Italic
    • TimesNewRomanItalic
    • TimesNewRomanPS-Italic
    • TimesNewRomanPS-ItalicMT
    • TimesNewRomanPSMT,Italic

映射到 Times-BoldItalic

  • StandardFont.TimesBoldOblique
    • Times-BoldItalic
    • TimesBoldItalic
    • TimesNewRoman,BoldItalic
    • TimesNewRoman-BoldItalic
    • TimesNewRomanBoldItalic
    • TimesNewRomanPS-BoldItalic
    • TimesNewRomanPS-BoldItalicMT
    • TimesNewRomanPSMT,BoldItalic

映射到 Symbol

  • StandardFont.Symbol
    • Symbol
    • SymbolMT

映射到 ZapfDingbats

  • StandardFont.Dingbats
    • ZapfDingbats

常见问题解答

如何使用 C# 在 PDF 文档中嵌入自定义字体?

using IronPDF 时,您可以通过在 Fonts 集合上调用 Add 方法,随后调用 Embed 方法来嵌入自定义字体。只需将字体文件加载为字节数组并添加到 PDF 中:pdf.Fonts.Add(File.ReadAllBytes("MyCustomFont.ttf")).Embed()。这可确保您的 PDF 在所有平台上都能正确显示。

如何检索现有 PDF 文档中的所有字体?

IronPDF 通过 Fonts 属性访问所有文档字体,该属性返回一个 PdfFontCollection 对象。您可以遍历该集合以检索字体信息,包括字体名称、嵌入状态和字体类型,从而轻松分析文档结构和字体使用情况。

在 PDF 中按名称查找特定字体的最佳方法是什么?

IronPDF 允许您使用 PdfFontCollection 对象查找特定字体。您可以通过指定字体名称来搜索字体,从而访问字体对象及其属性。当您需要替换或修改 PDF 文档中的特定字体时,这一功能尤其有用。

能否移除嵌入的字体以减小 PDF 文件的大小?

是的,IronPDF 提供了 Unembed 方法,允许您从 PDF 文档中移除嵌入的字体。这可以在保持文档结构的同时大幅减小文件大小,但可能会影响未安装所需字体的系统上的 PDF 显示效果。

如何替换现有 PDF 文档中的字体?

IronPDF 提供的 "替换 "方法让字体替换变得简单明了。您可以在 PDF 文档中用新字体轻松替换现有字体,这对于保持品牌一致性或用现代字体更新旧文档非常有用。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 20,296,129 | 版本: 2026.7 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronPdf
运行示例看着你的HTML代码变成PDF文件。