如何管理 PDF 中的字体

Chaknith related to 如何管理 PDF 中的字体
查克尼特·宾
2023年十二月14日
更新 2025年一月8日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

字体是一组具有一致样式和设计的字符、符号和字形。 它代表了文本的一种特定字体、大小、粗细和样式(例如常规、加粗、斜体等)。 字体在排版中用于以视觉上吸引人且连贯的方式显示文本。

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

查找和检索字体

检索字体

访问 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;

查找字体

使用 IronPDF 可以直接查找特定字体。 使用PdfFontCollection对象,我们可以将字体名称指定为用方括号括起来的字符串。 例如:字体 ["SpecialFontName"]。 这将返回一个PdfFont对象,我们可以使用它来检查属性和执行其他方法。

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

添加字体

使用Add方法添加标准字体和字体文件的字节数据。 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");

嵌入字体

将字体嵌入意味着将字体的字节流数据包含在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");
pdf.Fonts.Add(fontData);

// Embed the font
pdf.Fonts.Last().Embed(fontData);

取消嵌入字体

取消嵌入字体意味着从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();

替换字体

字体替换操作将保留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);

标准字体

PDF中的14种标准字体,也被称为“基本14字体”或“标准类型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
  • 符号
  • ZapfDingbats

标准字体映射

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

映射到快递

  • 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-斜体

    • 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,斜体

    • 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,加粗

    • 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, 斜体

    映射到 Times-BoldItalic

  • StandardFont.TimesBoldOblique

    • Times-BoldItalic

    • TimesBoldItalic

    • TimesNewRoman,BoldItalic

    • TimesNewRoman-BoldItalic

    • TimesNewRomanBoldItalic

    • TimesNewRomanPS-BoldItalic

    • TimesNewRomanPS-BoldItalicMT

    • TimesNewRomanPSMT,BoldItalic

    映射到符号

  • StandardFont.Symbol

    • 符号

    • SymbolMT

    映射到ZapfDingbats

  • StandardFont.Dingbats

    • ZapfDingbats
Chaknith related to 标准字体映射
软件工程师
Chaknith 是开发者中的福尔摩斯。他第一次意识到自己可能在软件工程方面有前途,是在他出于乐趣做代码挑战的时候。他的重点是 IronXL 和 IronBarcode,但他为能帮助客户解决每一款产品的问题而感到自豪。Chaknith 利用他从直接与客户交谈中获得的知识,帮助进一步改进产品。他的轶事反馈不仅仅局限于 Jira 票据,还支持产品开发、文档编写和市场营销,从而提升客户的整体体验。当他不在办公室时,他可能会在学习机器学习、编程或徒步旅行。