IRONSOFTWAREHOME

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

Curtis Chau
Curtis Chau
Updated: 2026年6月29日

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

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

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

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

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

  1. 1Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. 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");
    C#
  3. 3部署到您的生产环境中进行测试

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

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

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

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

using IronPdf;
using IronPdf.Fonts;
using System.Collections.Generic;

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

// Retreive font
PdfFontCollection fonts = pdf.Fonts;

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

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

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"];

如何在 PDF 中添加字体?

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

using IronPdf;
using IronPdf.Fonts;

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

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

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

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

Milan Jovanovic

微软MVP

查看案例研究

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

Brent Matzelle

首席技术官,OPYN

查看案例研究

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

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

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);

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

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

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

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();

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

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

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

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

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

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);

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 文档中用新字体轻松替换现有字体,这对于保持品牌一致性或用现代字体更新旧文档非常有用。

When is it beneficial to unembed fonts from a PDF?

Unembedding fonts from a PDF can be beneficial for reducing the file size, especially if the fonts are standard and widely available on most systems. However, ensure that the fonts have suitable fallbacks to avoid display issues post-unembedding, as visual rendering may be compromised otherwise.

What are the 14 standard PDF fonts supported by IronPDF?

The 14 standard PDF fonts, also known as Base 14 Fonts or Standard Type 1 Fonts, include Courier, Helvetica, Times-Roman, Symbol, and ZapfDingbats, among others. These fonts are universally supported in PDF viewers and do not require embedding, ensuring compatibility across various platforms.

How does IronPDF handle font replacement in PDFs?

IronPDF allows for font replacement where you can substitute one font for another while preserving the font's data structure, including styling and encoding. This is useful for standardizing fonts in a document, although slight visual differences may occur in some cases.

Can IronPDF manage web fonts and icon fonts in PDFs?

Yes, IronPDF provides comprehensive support for web fonts and icon fonts, accommodating modern typography needs. This is particularly advantageous when converting HTML to PDF, as IronPDF automatically handles web fonts incorporated in your HTML code.

How does IronPDF support PDF/A compliance concerning fonts?

IronPDF offers features to manage font embedding and consistency, critical for creating PDF/A compliant documents. By ensuring that fonts are properly embedded and displayed, IronPDF aids in meeting the standards required for long-term archiving of electronic documents.

Curtis Chau
技术作家

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

...
阅读更多

准备开始了吗?

Nuget Downloads 21,228,841版本:2026.9刚刚发布

免费获取

30天试用密钥 即刻获取。

bullet_checked无需信用卡或创建账户
bullet_test在生产环境中测试
且无水印
bullet_calendar30天完全
功能性产品
bullet_support试用期间提供
24/5技术支持
立即获取您的免费30 天试用密钥。
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择浏览并搜索 “IronPDF”
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

或在此处下载 Windows 安装程序。

  1. 下载并解压 IronPDF 到您的解决方案目录中的 ~/Libs 之类的位置
  2. 在 Visual Studio 解决方案资源管理器中,右键点击引用。选择浏览,“IronPDF.dll”

$999 起

Key in blue circle

立即获取免费的 30 天试用版密钥。

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

OR
bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥。
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择浏览并搜索 “IronPDF”
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

或在此处下载 Windows 安装程序。

  1. 下载并解压 IronPDF 到您的解决方案目录中的 ~/Libs 之类的位置
  2. 在 Visual Studio 解决方案资源管理器中,右键点击引用。选择浏览,“IronPDF.dll”

$999 起