如何管理 PDF 中的字體

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;
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
VB   C#

尋找字體

使用 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"];
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")
VB   C#

添加字體

使用 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");
Imports IronPdf
Imports IronPdf.Fonts

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

' Add font
pdf.Fonts.Add("Helvetica")
VB   C#

嵌入字型

將字型嵌入意味著在PDF文件本身中包含字型的位元組流數據。 這樣一來,查看PDF文件時就無需在系統上安裝字體。 雖然這通常會增加 PDF 文件的檔案大小,但對於視覺一致性而言有益,且無需安裝字體的額外要求。

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

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

// Select which font to embed
PdfFont targetFont = pdf.Fonts["MyCustomFont"];

// 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);
Imports IronPdf
Imports IronPdf.Fonts
Imports System.Linq

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

' Select which font to embed
Private targetFont As PdfFont = pdf.Fonts("MyCustomFont")

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

' Embed the font
pdf.Fonts.Last().Embed(fontData)
VB   C#

取消嵌入字體

解除嵌入字體意味著移除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()
VB   C#

替換字體

字體替換操作將保留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)
VB   C#

標準字體

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
  • 符號

    ZapfDingbats

標準字體映射

為了方便參考標準字型,多個字符串名稱指向同一個字型。

映射到快遞

  • StandardFont.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,斜體

    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, 斜體

    Arial-Italic

    Arial-ItalicMT

    ArialItalic

    • ArialMT, 斜體

    • Helvetica, 斜體

    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-斜體

    TimesNewRomanItalic

    • TimesNewRomanPS-斜体

    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 工單,並支持產品開發、文件撰寫和行銷,以提升客戶的整體體驗。不在公司時,他通常在學習機器學習、寫程式和徒步旅行。