|
| 1 | +--- |
| 2 | +title: Customizing the Font of Numbered Lists with RadPdfProcessing |
| 3 | +description: Learn how to change the font style of numbered lists in PDF documents using RadPdfProcessing. |
| 4 | +type: how-to |
| 5 | +page_title: How to Change the Font Style of Numbered Lists in PDFs Using RadPdfProcessing |
| 6 | +slug: customize-font-numbered-lists-radpdfprocessing |
| 7 | +tags: radpdfprocessing, document processing, fonts, lists, pdf, customization |
| 8 | +res_type: kb |
| 9 | +ticketid: 1655319 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 2024.2.426| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +When creating a PDF and adding a numbered list to a set of blocks, the font of the numbers does not change according to the block's font and remains as Helvetica. This KB article shows how to customize the font of a numbered list in a PDF document. |
| 21 | + |
| 22 | +## Solution |
| 23 | + |
| 24 | +To customize the font of the list numbers to match the font of the block, specify the font by using the [Levels](https://docs.telerik.com/devtools/document-processing/api/telerik.windows.documents.fixed.model.editing.collections.listlevelcollection) collection and the [CharacterProperties](https://docs.telerik.com/devtools/document-processing/api/telerik.windows.documents.fixed.model.editing.flow.characterproperties) property of the respective level in the list. Below is an example demonstrating how to achieve this: |
| 25 | + |
| 26 | +1. Implement a custom [FontsProvider]({%slug pdfprocessing-implement-fontsprovider%}) class to supply the desired fonts in [Cross-platform scenarios]({%slug radpdfprocessing-cross-platform-fonts%}). This class should override the `GetFontData` method to return the font data for the specified `FontProperties`. |
| 27 | + |
| 28 | +2. Before creating the PDF document, set the custom `FontsProvider` as the fonts provider. |
| 29 | + |
| 30 | +3. Create the font instance for the list numbers using the `FontsRepository.TryCreateFont` method. |
| 31 | + |
| 32 | +4. Create a new [List]({%slug radpdfprocessing-editing-list%}) instance with `ListTemplateType.NumberedDefault` and set the font and size for the list's first level. |
| 33 | + |
| 34 | +5. Add blocks to the document and set their bullet to the customized list. |
| 35 | + |
| 36 | +6. [Export the document](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/formats-and-conversion/pdf/pdfformatprovider/pdfformatprovider#export) to a PDF file. |
| 37 | + |
| 38 | + |
| 39 | +```csharp |
| 40 | + static void Main(string[] args) |
| 41 | + { |
| 42 | + Telerik.Windows.Documents.Extensibility.FontsProviderBase fontsProvider = new FontsProvider(); |
| 43 | + Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.FontsProvider = fontsProvider; |
| 44 | + |
| 45 | + FontBase font; |
| 46 | + bool success = FontsRepository.TryCreateFont(new FontFamily("Verdana"), FontStyles.Italic ,FontWeights.Normal, out font); |
| 47 | + |
| 48 | + RadFixedDocument document = new RadFixedDocument(); |
| 49 | + RadFixedDocumentEditor editor = new RadFixedDocumentEditor(document); |
| 50 | + List list = new(ListTemplateType.NumberedDefault); |
| 51 | + list.Levels[0].CharacterProperties.Font = font; |
| 52 | + list.Levels[0].CharacterProperties.FontSize = 20; |
| 53 | + |
| 54 | + Block block = new Block(); |
| 55 | + block.TextProperties.TrySetFont(new Telerik.Documents.Core.Fonts.FontFamily("Calibri")); |
| 56 | + block.TextProperties.FontSize = 14; |
| 57 | + block.SetBullet(list, 0); |
| 58 | + block.InsertText("Calibri text block."); |
| 59 | + editor.InsertBlock(block); |
| 60 | + |
| 61 | + block = new Block(); |
| 62 | + block.TextProperties.TrySetFont(new Telerik.Documents.Core.Fonts.FontFamily("Century Gothic")); |
| 63 | + block.TextProperties.FontSize = 10; |
| 64 | + block.SetBullet(list, 0); |
| 65 | + block.InsertText("Century Gothic text block."); |
| 66 | + editor.InsertBlock(block); |
| 67 | + |
| 68 | + string outputFilePath = "sample.pdf"; |
| 69 | + File.Delete(outputFilePath); |
| 70 | + PdfFormatProvider provider = new PdfFormatProvider(); |
| 71 | + using (Stream output = File.OpenWrite(outputFilePath)) |
| 72 | + { |
| 73 | + provider.Export(document, output); |
| 74 | + } |
| 75 | + Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); |
| 76 | + } |
| 77 | + public class FontsProvider : Telerik.Windows.Documents.Extensibility.FontsProviderBase |
| 78 | + { |
| 79 | + public override byte[] GetFontData(Telerik.Windows.Documents.Core.Fonts.FontProperties fontProperties) |
| 80 | + { |
| 81 | + string fontFileName = fontProperties.FontFamilyName + ".ttf"; |
| 82 | + string fontFolder = Environment.GetFolderPath(Environment.SpecialFolder.Fonts); |
| 83 | + |
| 84 | + //The fonts can differ depending on the file |
| 85 | + if (fontProperties.FontFamilyName == "Calibri") |
| 86 | + { |
| 87 | + if (fontProperties.FontStyle == FontStyles.Italic && fontProperties.FontWeight == FontWeights.Bold) |
| 88 | + { |
| 89 | + fontFileName = $"calibriz.ttf"; |
| 90 | + } |
| 91 | + else if (fontProperties.FontStyle == FontStyles.Italic) |
| 92 | + { |
| 93 | + fontFileName = $"calibrii.ttf"; |
| 94 | + } |
| 95 | + else if (fontProperties.FontWeight == FontWeights.Normal) |
| 96 | + { |
| 97 | + fontFileName = "calibri.ttf"; |
| 98 | + } |
| 99 | + else if (fontProperties.FontWeight == FontWeights.Bold) |
| 100 | + { |
| 101 | + fontFileName = $"calibrib.ttf"; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + else if (fontProperties.FontFamilyName == "Century Gothic") |
| 106 | + { |
| 107 | + if (fontProperties.FontStyle == FontStyles.Italic && fontProperties.FontWeight == FontWeights.Bold) |
| 108 | + { |
| 109 | + fontFileName = $"gothicbi.ttf"; |
| 110 | + } |
| 111 | + else if (fontProperties.FontStyle == FontStyles.Italic) |
| 112 | + { |
| 113 | + fontFileName = $"gothici.ttf"; |
| 114 | + } |
| 115 | + else if (fontProperties.FontWeight == FontWeights.Normal) |
| 116 | + { |
| 117 | + fontFileName = "gothic.ttf"; |
| 118 | + } |
| 119 | + else if (fontProperties.FontWeight == FontWeights.Bold) |
| 120 | + { |
| 121 | + fontFileName = $"gothicb.ttf"; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + else if (fontProperties.FontFamilyName == "Verdana") |
| 126 | + { |
| 127 | + if (fontProperties.FontStyle == FontStyles.Italic) |
| 128 | + { |
| 129 | + fontFileName = $"verdanai.ttf"; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + //...add more fonts if needed... |
| 134 | +
|
| 135 | + DirectoryInfo directory = new DirectoryInfo(fontFolder); |
| 136 | + FileInfo[] fontFiles = directory.GetFiles(); |
| 137 | + |
| 138 | + var fontFile = fontFiles.FirstOrDefault(f => f.Name.Equals(fontFileName, StringComparison.InvariantCultureIgnoreCase)); |
| 139 | + if (fontFile != null) |
| 140 | + { |
| 141 | + var targetPath = fontFile.FullName; |
| 142 | + using (FileStream fileStream = File.OpenRead(targetPath)) |
| 143 | + { |
| 144 | + using (MemoryStream memoryStream = new MemoryStream()) |
| 145 | + { |
| 146 | + fileStream.CopyTo(memoryStream); |
| 147 | + return memoryStream.ToArray(); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return null; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | +``` |
| 157 | + |
| 158 | +The achieved result is illustrated in the below screenshot: |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | +This approach allows you to customize the font and font size of the numbers in a numbered list, ensuring they match the rest of the text in the PDF document. |
| 163 | + |
| 164 | +## See Also |
| 165 | + |
| 166 | +- [RadPdfProcessing - Using Lists with Block Class]({%slug radpdfprocessing-editing-list%}) |
| 167 | +- [Cross-platform scenarios]({%slug radpdfprocessing-cross-platform-fonts%}) |
| 168 | +- [How to Implement FontsProvider]({%slug pdfprocessing-implement-fontsprovider%}) |
| 169 | +- [Create Custom Image Bullets]({%slug create-custom-image-bullets%}) |
0 commit comments