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

How to Manage Fonts in PDFs

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

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

IronPDF 提供了一种方便的方式来管理字体,提供的功能包括查找字体、获取字体、嵌入字体、取消嵌入字体和替换字体。

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

开始使用 IronPDF 来简化 PDF 文档中的字体管理。 本快速指南演示了如何在 PDF 中嵌入所有字体,以确保在不同平台上的视觉一致性。 只需几行代码,您就可以增强 PDF 文档的外观,并在无需额外字体安装的情况下保持兼容性。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    var pdf = PdfDocument.FromHtml("<p style='font‑family:MyCustomFont;'>Hello world!</p>");
    pdf.Fonts.Add("MyCustomFont", File.ReadAllBytes("MyCustomFont.ttf"))
      .Embed()
      .SaveAs("withCustomFont.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小化工作流程(5 步)

  1. 下载 IronPDF C# 库
  2. 利用 Add 方法将字体添加到字体集合中
  3. 利用 Embed 方法嵌入字体以确保持久的视觉效果
  4. 使用 Unembed 方法取消嵌入字体以减小大小
  5. 使用 Replace 方法轻松替换字体

查找和检索字体

检索字体

访问 Fonts 属性将返回 PdfFontCollection 对象,其中包含文档中所有字体的列表。 Fonts 属性可以通过迭代 PdfFontCollection 对象直接访问。

: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

查找字体

使用 IronPDF 查找特定字体很简单。 使用 PdfFontCollection 对象,您可以指定字体名称。 此示例展示了如何通过名称访问字体对象并检查其属性。

: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

添加字体

使用 Add 方法添加标准字体和作为字节数据的字体文件。 仅接受字体名称的方法只接受 14种标准字体之一。 添加标准字体并不嵌入它,因为标准字体已经保证在操作系统上可用。

: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

嵌入字体

嵌入字体意味着将字体的字节流数据包含在 PDF 文档本身中。 这样就不需要在系统上安装字体来正确查看 PDF 文档。 虽然这通常会增加 PDF 文档的文件大小,但对于视觉一致性有益,并且不需要额外安装字体。

: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

取消嵌入字体

取消嵌入字体意味着删除包括在 PDF 文档中的嵌入字体字节流数据。 目标是减少 PDF 文档的文件大小。使用 Unembed 方法实现这一目标。

: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 中也出现,这是预期的行为。 但是,如果没有,请报告给我们的支持团队以便进一步调查。 损坏的字体显示如下:

class="content-img-align-center">
class="center-image-wrapper"> 损坏的字体 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

标准字体

PDF中的14种标准字体,也称为“基本14字体”或“标准Type 1字体”,是PDF查看器广泛支持的字体集,不需要嵌入文档中。 标准字体定义了14种字体,这些字体在处理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

标准字体 Mapping

为了方便引用标准字体,多个字符串名称指向相同的字体。

映射到 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

常见问题解答

我如何才能找到并列出PDF文档中使用的字体?

您可以使用IronPDF来访问PdfDocument对象的Fonts属性,该属性提供PdfFontCollection。遍历此集合可以找到并列出文档中使用的所有字体。

有哪些方法可用于将字体添加到PDF中?

IronPDF允许您使用Add方法添加字体,其中可以包括标准字体或作为字节数据的自定义字体文件。此方法使您可以扩展PDF文档中可用的字体范围。

如何在PDF中嵌入字体以确保视觉一致性?

要使用IronPDF嵌入字体,您可以从PdfFontCollection中检索所需字体并应用Embed方法。这确保了字体的字节流数据包含在PDF中,在不同系统之间保持视觉一致性。

为何需要在PDF文档中取消嵌入字体?

使用IronPDF取消嵌入字体涉及删除嵌入的字节流数据以减少文件大小。当文件大小是优先事项且字体在大多数系统上普遍可用时,这非常有用。

替换PDF中的字体的过程是什么?

您可以在PdfFontCollection中使用IronPDF的Replace方法替换PDF中的字体。这样可以用另一种字体替换现有字体,确保其尽可能接近原始字体的样式和编码。

为什么Base 14 Fonts在PDF中很重要?

Base 14 Fonts或标准Type 1 Fonts,如Courier、Helvetica和Times-Roman非常重要,因为它们被PDF查看器普遍支持。它们不需要嵌入,从而确保兼容性并减小文件大小。

是否可以在PDF文档中使用自定义字体?

是的,IronPDF支持包括自定义字体。通过使用带有字体文件数据的Add方法,您可以在标准选项之外将任何字体整合到您的PDF文档中。

使用IronPDF管理PDF文档字体的优点是什么?

IronPDF提供了强大的字体管理功能,允许您查找、添加、嵌入、取消嵌入和替换字体。这种灵活性增强了文档的定制化,并确保在各个平台间的视觉一致性。

IronPDF 是否完全兼容 .NET 10 的字体管理操作?

是的。IronPDF 与 .NET 10 完全兼容,支持所有字体管理操作(包括AddEmbedUnembedReplace和访问标准字体),无需任何变通方法或特殊配置。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布