如何在 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. 複製並運行這段程式碼。

    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. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronPDF

    arrow pointer

如何在PDF文件中尋找和提取字體?

如何從PDF文件中提取所有字體?

存取 Fonts 屬性會傳回包含所有文件字型的 PdfFontCollection 物件。 可以透過遍歷 PdfFontCollection 物件直接存取 Fonts 屬性。 這在處理PDF 表單或分析文件結構時尤其有用。

: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;
$vbLabelText   $csharpLabel

如何透過名稱尋找特定字體?

使用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"];
$vbLabelText   $csharpLabel

如何為PDF文件中添加字體?

使用 Add 方法以位元組資料的形式新增標準字型或字型檔。 接受字體名稱的方法只接受14 種標準字體中的一種。 添加標準字體並不會將其嵌入,因為作業系統保證會提供這些字體。 將 HTML 轉換為 PDF時, IronPDF會自動處理 HTML 中的 Web 字型。

: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");
$vbLabelText   $csharpLabel

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);
$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();
$vbLabelText   $csharpLabel

如果非常用字體在解嵌入後沒有備用字體,則在輸出的 PDF 中可能會出現字體損壞的情況。 檢查 Adob​​e 是否也存在同樣的問題——如果存在,則屬於預期行為。 如果問題仍然存在,請聯絡客服進行調查。 損壞的字體顯示如下:

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);
$vbLabelText   $csharpLabel

14種標準PDF字體是什麼?

14 種標準字體(基本 14 種字體或標準 1 型字體)在 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時具有相容性。

地圖到快遞

  • StandardFont.Courier
    • 導遊
    • 導遊New
    • 導遊NewPSMT
    • 快遞標準

前往 Courier-Bold 的地圖

  • StandardFont.CourierBold
    • 導遊,Bold
    • 導遊-Bold
    • 導遊Bold
    • 導遊New,大膽
    • 導遊New-Bold
    • 導遊NewBold
    • 導遊NewPS-BoldMT
    • 快遞標準-Bold

前往信差斜線的地圖

  • StandardFont.CourierOblique
    • 快遞員,斜體
    • 斜向信使
    • 導遊Italic
    • 導遊New,斜體
    • 導遊New-Italic
    • 導遊NewItalic
    • 導遊NewPS-ItalicMT
    • 快遞標準-Oblique

地圖到 Courier-BoldOblique

  • StandardFont.CourierBoldOblique
    • 導遊,粗斜體
    • 導遊-BoldOblique
    • 導遊BoldItalic
    • 導遊New,粗斜體
    • 導遊New-BoldItalic
    • 導遊NewBoldItalic
    • 導遊NewPS-BoldItalicMT
    • 快遞標準-BoldOblique

前往 Helvetica 的地圖

  • StandardFont.Helvetica
    • Arial
    • ArialMT
    • Helvetica

映射到 Helvetica-Bold

  • StandardFont.HelveticaBold
    • Arial,粗體
    • Arial-粗體
    • Arial-粗體MT
    • Arial粗體
    • ArialMT,粗體
    • ArialRoundedMTBold
    • Helvetica,粗體
    • Helvetica-Bold
    • Helvetica粗體

映射到 Helvetica-Oblique

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

映射到 Helvetica-BoldOblique

  • StandardFont.HelveticaBoldOblique
    • Arial、粗斜體
    • Arial-粗斜體
    • Arial-粗斜體MT
    • Arial粗斜體
    • ArialMT,粗斜體
    • Helvetica,粗斜體
    • Helvetica-BoldItalic
    • Helvetica-BoldOblique
    • Helvetica粗斜體

地圖轉 Times Roman 字體

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

地圖至時代粗體

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

地圖到 Times-Italic

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

地圖到 Times-粗斜體

  • StandardFont.TimesBoldOblique
    • Times-粗斜體
    • TimesBoldItalic
    • TimesNewRoman,粗斜體
    • TimesNewRoman-粗體Italic
    • TimesNewRoman粗體Italic TimesNewRomanPS-BoldItalic TimesNewRomanPS-BoldItalicMT TimesNewRomanPSMT,粗斜體

映射到符號

  • StandardFont.Symbol
    • 象徵
    • 象徵MT

前往 ZapfDingbats 的地圖

  • StandardFont.Dingbats
    • ZapfDingbats

常見問題解答

如何使用 C# 在 PDF 文件中嵌入自訂字型?

使用 IronPDF,您可以在 Fonts 集合上使用 Add 方法,然後再使用 Embed 方法來嵌入自訂字體。只需將您的字型檔案載入為一個位元組陣列,並將其加入 PDF 中:pdf.Fonts.Add("MyCustomFont", File.ReadAllBytes("MyCustomFont.ttf")).Embed().這可確保您的 PDF 在所有平台上都能正確顯示。

如何檢索現有 PDF 文件中的所有字型?

IronPDF 透過 Fonts 屬性提供對所有文件字型的存取,而 Fonts 屬性會回傳一個 PdfFontCollection 物件。您可以遍歷這個集合來擷取字型資訊,包括字型名稱、嵌入狀態和字型類型,讓您可以輕鬆分析文件結構和字型使用情況。

在 PDF 中根據名稱查找特定字型的最佳方法是什麼?

IronPDF 允許您使用 PdfFontCollection 物件來尋找特定的字型。您可以透過指定字型名稱來搜尋字型,這樣就可以存取字型物件及其屬性。當您需要替換或修改 PDF 文件中的特定字型時,此功能尤其有用。

我可以移除內嵌字型以縮小 PDF 檔案大小嗎?

是的,IronPDF 提供了 Unembed 方法,可讓您移除 PDF 文件中的內嵌字體。這可以在保持文件結構的同時大幅減少文件大小,不過這可能會影響 PDF 在未安裝所需字體的系統上的顯示效果。

如何替換現有 PDF 文件中的字型?

IronPDF 提供的 Replace 方法可讓字型更換變得簡單直接。您可以輕鬆地將 PDF 文件中的現有字體更換為新字體,這對於保持品牌一致性或使用現代字體更新舊文件非常有用。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 17,803,474 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在滾動嗎?

想快速取得證據? PM > Install-Package IronPdf
運行範例看著你的HTML程式碼變成PDF檔。