|
| 1 | +--- |
| 2 | +title: Preserving Original Bold, Italic and Regular Fonts When Exporting PDF Documents Using PdfProcessing in .NET Standard |
| 3 | +description: Learn how to ensure the original bold, italic and regular fonts in a PDF remain unchanged while embedding specific fonts using PdfProcessing in .NET Standard. |
| 4 | +type: how-to |
| 5 | +page_title: Preserving Original Bold, Italic and Regular Fonts When Exporting PDF Documents Using PdfProcessing in .NET Standard |
| 6 | +meta_title: Prevent Font Conversion While Embedding Fonts Using PdfProcessing |
| 7 | +slug: pdfprocessing-prevent-font-conversion-embedding-fonts |
| 8 | +tags: pdf, processing, telerik, document, embed, font,provider, net, standard, bold, italic, normal, style |
| 9 | +res_type: kb |
| 10 | +ticketid: 1701737 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +| Version | Product | Author | |
| 16 | +| ---- | ---- | ---- | |
| 17 | +| 2025.3.806| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +When using the [PdfProcessing]({%slug radpdfprocessing-overview%}) library in .NET Standard to embed fonts into a PDF, the behavior can lead to unintended font substitution for text content and fields that are already using a different font. This happens because the .NET Standard environment requires the font data to be explicitly provided for embedding, unlike the .NET Framework which can access system fonts directly. Without providing the required font files, the library substitutes missing fonts, potentially altering the PDF's appearance. This unexpected result commonly arises when the font chosen for embedding replaces existing fonts in the document, such as converting Arial text to Courier New, resulting in illegible text in certain languages. |
| 22 | + |
| 23 | +This article provides steps to prevent such undesired font conversion when embedding fonts in a PDF using PdfProcessing. |
| 24 | + |
| 25 | +This knowledge base article also answers the following questions: |
| 26 | +- How can I prevent font substitution in PdfProcessing while embedding fonts? |
| 27 | +- How do I embed fonts without altering the text appearance in PdfProcessing? |
| 28 | +- How to manage fonts dynamically when embedding them in PdfProcessing? |
| 29 | + |
| 30 | +## Solution |
| 31 | + |
| 32 | +To prevent font substitution for text content and fields that should retain their original fonts and style during the embedding process, implement a custom **FontsProvider**. This provider dynamically supplies font data based on the font name and properties found in your PDF. |
| 33 | + |
| 34 | +Follow these steps: |
| 35 | + |
| 36 | +1. Create a custom [FontsProvider]({%slug radpdfprocessing-cross-platform-fonts%}) by inheriting from **FontsProviderBase**. |
| 37 | +2. Implement the **GetFontData** method to dynamically return font data based on the font properties. |
| 38 | +3. Optionally, maintain a repository of commonly used fonts to automate font discovery. |
| 39 | + |
| 40 | +Example implementation of a dynamic **FontsProvider**: |
| 41 | + |
| 42 | +```csharp |
| 43 | +public class FontsProvider : Telerik.Windows.Documents.Extensibility.FontsProviderBase |
| 44 | +{ |
| 45 | + public override byte[] GetFontData(Telerik.Windows.Documents.Core.Fonts.FontProperties fontProperties) |
| 46 | + { |
| 47 | + string fontFolder = Environment.GetFolderPath(Environment.SpecialFolder.Fonts); |
| 48 | + string fontFileName = GetFontFileName(fontProperties); |
| 49 | + |
| 50 | + string fontPath = Path.Combine(fontFolder, fontFileName); |
| 51 | + if (File.Exists(fontPath)) |
| 52 | + { |
| 53 | + return File.ReadAllBytes(fontPath); |
| 54 | + } |
| 55 | + |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + private string GetFontFileName(Telerik.Windows.Documents.Core.Fonts.FontProperties fontProperties) |
| 60 | + { |
| 61 | + string family = fontProperties.FontFamilyName.ToLowerInvariant(); |
| 62 | + bool isBold = fontProperties.FontWeight == FontWeights.Bold; |
| 63 | + bool isItalic = fontProperties.FontStyle == FontStyles.Italic; |
| 64 | + |
| 65 | + // Mapping for known fonts |
| 66 | + if (family == "courier new") |
| 67 | + { |
| 68 | + if (isBold && isItalic) return "courbi.ttf"; |
| 69 | + if (isBold) return "courbd.ttf"; |
| 70 | + if (isItalic) return "couri.ttf"; |
| 71 | + return "cour.ttf"; |
| 72 | + } |
| 73 | + |
| 74 | + // Generic fallback: try to construct filename from family and style |
| 75 | + string suffix = ""; |
| 76 | + if (isBold && isItalic) suffix = "bi"; |
| 77 | + else if (isBold) suffix = "bd"; |
| 78 | + else if (isItalic) suffix = "i"; |
| 79 | + |
| 80 | + return $"{family}{suffix}.ttf"; |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +#### Applying the Fonts Provider |
| 86 | + |
| 87 | +```csharp |
| 88 | +Telerik.Windows.Documents.Extensibility.FontsProviderBase fontsProvider = new FontsProvider(); |
| 89 | +Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.FontsProvider = fontsProvider; |
| 90 | +``` |
| 91 | + |
| 92 | +### Notes: |
| 93 | +- Ensure the required font files are installed on your machine or stored locally in a specified directory. |
| 94 | +- For automation, maintain a repository of commonly used fonts and expand it as new fonts are encountered. |
| 95 | +- This implementation handles font data dynamically based on the document's font properties, avoiding hardcoding specific font names wherever possible. |
| 96 | + |
| 97 | +## See Also |
| 98 | + |
| 99 | +- [Implementing a FontsProvider]({%slug radpdfprocessing-concepts-fonts%}) |
0 commit comments