如何使用C#管理PDF中的字體
IronPDF使您能夠在C#中進行全面的字體管理,包括查找、新增、嵌入、解除嵌入和替換PDF文件中的字體,以確保跨平台保持一致的文字顯示。
字體是一組具有一致風格和設計的字元、符號和圖形。 它表示特定字體、大小、粗細和樣式(如常規、粗體、斜體等)的文字。 字體在排版中用於以視覺吸引人和一致的方式顯示文字。
IronPDF提供了一種方便的字體管理方式,提供如查找字體、獲取字體、嵌入字體、解除嵌入字體和替換字體等功能。 無論您是在建立新的PDF,還是編輯現有文件,正確的字體管理可以確保您的PDF在所有平台和裝置上正確顯示。
快速入門:管理和嵌入您的PDF中的字體
開始使用IronPDF以簡化您的PDF文件中的字體管理。 本指南展示如何在PDF中嵌入字體,以達到跨平台的視覺一致性。 只需幾行程式碼,您就可以增強文件外觀並保持相容性。
-
使用NuGet套件管理器安裝https://www.nuget.org/packages/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步)
- Download the IronPDF C# Library
- 使用
Add方法將字體新增到集合中 - 使用
Embed方法嵌入持續顯示的字體 - 使用
Unembed方法減少文件大小 - 使用
Replace方法輕鬆替換字體
如何在PDF中查找和檢索字體?
如何從PDF中檢索所有字體?
存取PdfFontCollection物件。 PdfFontCollection物件直接存取。 這在處理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種標準字體(基本14字體或標準Type 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時的相容性。
映射到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
常見問題
如何使用C#在PDF文件中嵌入自訂字體?
使用IronPDF,您可以透過對字體集合使用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文件中的新字體,這對於維持品牌一致性或用現代字體更新舊式文件非常有用。

