Skip to content

Commit 2cb8d19

Browse files
committed
addressed feedback
1 parent b071463 commit 2cb8d19

File tree

1 file changed

+123
-44
lines changed

1 file changed

+123
-44
lines changed

knowledge-base/customize-font-numbered-lists-radpdfprocessing.md

Lines changed: 123 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,64 +21,142 @@ When creating a PDF and adding a numbered list to a set of blocks, the font of t
2121

2222
## Solution
2323

24-
To customize the font of the list numbers to match the font of the block, specify the font by using the `Levels` collection and the `CharacterProperties` property of the respective level in the list. Below is an example demonstrating how to achieve this:
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:
2525

26-
1. Implement a custom `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-
```csharp
29-
public class FontsProvider : Telerik.Windows.Documents.Extensibility.FontsProviderBase
30-
{
31-
public override byte[] GetFontData(Telerik.Windows.Documents.Core.Fonts.FontProperties fontProperties)
32-
{
33-
// Implementation of the GetFontData method
34-
// Example code not reproduced here for brevity
35-
}
36-
}
37-
```
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`.
3827

3928
2. Before creating the PDF document, set the custom `FontsProvider` as the fonts provider.
4029

41-
```csharp
42-
Telerik.Windows.Documents.Extensibility.FontsProviderBase fontsProvider = new FontsProvider();
43-
Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.FontsProvider = fontsProvider;
44-
```
45-
4630
3. Create the font instance for the list numbers using the `FontsRepository.TryCreateFont` method.
4731

48-
```csharp
49-
FontBase font;
50-
bool success = FontsRepository.TryCreateFont(new FontFamily("Verdana"), FontStyles.Italic, FontWeights.Normal, out font);
51-
```
52-
53-
4. Create a new `List` instance with `ListTemplateType.NumberedDefault` and set the font and size for the list's first level.
54-
55-
```csharp
56-
List list = new(ListTemplateType.NumberedDefault);
57-
list.Levels[0].CharacterProperties.Font = font;
58-
list.Levels[0].CharacterProperties.FontSize = 20;
59-
```
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.
6033

6134
5. Add blocks to the document and set their bullet to the customized list.
6235

63-
```csharp
64-
Block block = new Block();
65-
block.TextProperties.TrySetFont(new Telerik.Documents.Core.Fonts.FontFamily("Calibri"));
66-
block.TextProperties.FontSize = 14;
67-
block.SetBullet(list, 0);
68-
block.InsertText("Custom text block.");
69-
```
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.
7037

71-
6. Export the document to a PDF file.
7238

7339
```csharp
74-
string outputFilePath = "sample.pdf";
75-
PdfFormatProvider provider = new PdfFormatProvider();
76-
using (Stream output = File.OpenWrite(outputFilePath))
77-
{
78-
provider.Export(document, output);
79-
}
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+
80156
```
81157

158+
The achieved result is illustrated in the below screenshot:
159+
82160
![List Number's Font](images/pdf-list-number-font.png)
83161

84162
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.
@@ -88,3 +166,4 @@ This approach allows you to customize the font and font size of the numbers in a
88166
- [RadPdfProcessing - Using Lists with Block Class]({%slug radpdfprocessing-editing-list%})
89167
- [Cross-platform scenarios]({%slug radpdfprocessing-cross-platform-fonts%})
90168
- [How to Implement FontsProvider]({%slug pdfprocessing-implement-fontsprovider%})
169+
- [Create Custom Image Bullets]({%slug create-custom-image-bullets%})

0 commit comments

Comments
 (0)