如何使用 C# 管理 PDF 中的字型
IronPDF 支援在 C# 中進行全面的字型管理,包括在 PDF 文件中搜尋、新增、嵌入、解除嵌入及替換字型,以確保文字在所有平台上都能一致顯示。
字體是一組具有一致風格與設計的字元、符號及字形。 它代表文字的特定字型、大小、粗細及樣式(例如常體、粗體、斜體等)。 字型在排版中用於以視覺上吸引人且連貫的方式呈現文字。
IronPDF 提供了一種便捷的字型管理方式,具備搜尋字型、取得字型、嵌入字型、解除嵌入字型以及替換字型等功能。 無論您是建立新的 PDF 檔案,還是編輯現有文件,妥善的字型管理都能確保您的 PDF 檔案在所有平台和裝置上正確顯示。
快速入門:在 PDF 中管理與嵌入字型
立即開始使用 IronPDF,簡化您 PDF 文件中的字型管理。 本指南說明如何將字型嵌入 PDF 檔案,以確保跨平台的視覺一致性。 只需幾行程式碼,即可提升文件外觀並維持相容性。
-
using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronPdf
PM > Install-Package IronPdf -
請複製並執行此程式碼片段。
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"); -
部署至您的生產環境進行測試
立即透過免費試用,在您的專案中開始使用 IronPDF
簡化工作流程(5 個步驟)
- 下載 IronPDF C# 函式庫
- 使用
Add方法將字型新增至集合 - 使用
Embed方法嵌入字型以實現視覺效果的持久化 - 使用
Unembed方法縮小檔案大小 - 使用
Replace方法輕鬆替換字型
如何在 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;
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
如何根據名稱查找特定字型?
使用 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"];
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")
如何將字型新增至 PDF 檔案?
請使用 Add 方法,將標準字型或字型檔案作為位元組資料加入。 該僅接受字型名稱的方法,僅支援 14 種標準字型中的其中一種。 新增標準字型並不會將其嵌入,因為這些字型在作業系統上已保證可用。 在將 HTML 轉換為 PDF 時,IronPDF 會自動處理 HTML 中的網頁字型。
: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")
針對包含網頁字型與圖示字型在內的高階字型管理,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);
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)
字型嵌入對於國際語言及 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();
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()
若不常見的字型在解除嵌入後缺乏備用字型,它們可能會在輸出 PDF 中顯示為損壞。 請確認 Adobe 是否出現相同問題——若然,此為預期行為。 若非如此,請聯絡支援部門進行調查。 損壞的字型顯示如下:
如何在 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)
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
- Times New Roman
- Times New RomanPS
- Times New RomanPSMT
對應至 Times-Bold
StandardFont.TimesBold- Times-Bold
- TimesBold
- Times New Roman, 粗體
- Times New Roman 粗體
- Times New RomanBold
- Times New Roman PS-Bold
- Times New Roman PS-BoldMT
- Times New RomanPSMT,Bold
對應至 Times-Italic
StandardFont.TimesOblique- Times-Italic
- TimesItalic
- Times New Roman, 斜體
- Times New Roman-Italic
- Times New Roman 斜體
- Times New Roman PS-Italic
- Times New Roman PS-ItalicMT
- Times New RomanPSMT,Italic
對應至 Times-BoldItalic
StandardFont.TimesBoldOblique- Times-BoldItalic
- TimesBoldItalic
- Times New Roman, 粗體斜體
- Times New Roman 粗體Italic
- Times New RomanBoldItalic
- Times New Roman PS 粗體斜體
- Times New Roman PS 粗體斜體MT
- Times New RomanPSMT,BoldItalic
對應至符號
StandardFont.Symbol- 符號
- 符號MT
對應至 ZapfDingbats
StandardFont.Dingbats- ZapfDingbats
常見問題
如何使用 C# 將自訂字型嵌入 PDF 文件中?
透過 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 提供「Replace」方法,讓字型替換變得簡單直觀。您可以輕鬆地在整個 PDF 文件中將現有字型替換為新字型,這對於維持品牌一致性,或使用現代字體更新舊版文件非常有用。

