I am working on code to read DOCX from user, then I will replace text in DOCX by our data.
Anyway, I want to validate all font that is used by uploaded document. Can you provide me code snippet?
I have debugged Run, StyleRepository, Paragrah but no luck. it just display default Verdana.
1 Answer, 1 is accepted
0
Yoan
Telerik team
answered on 06 Dec 2022, 04:19 PM
Hello, Omid,
You can get the document Fonts and Styles by iterating through all document Runs and accessing their properties. Use the EnumerateChildrenOfType<Run>() method of the document to get a collection of all the Runs and access their FontFamily, FontStyle, and other properties. You can read more about working with the text properties in the Modifying a Run topic.
The following example shows how to obtain the font family and style applied to each Run in the document and store them in separate collections:
DocxFormatProvider provider = new DocxFormatProvider();
//All fonts
List<ThemableFontFamily> fonts = new List<ThemableFontFamily>();
//All styles
List<Style> styles = new List<Style>();
using (Stream input = File.OpenRead("test.docx"))
{
RadFlowDocument document = provider.Import(input);
IEnumerable<Run> documentRuns = document.EnumerateChildrenOfType<Run>();
foreach (var run in documentRuns)
{
//Add style to styles collection
styles.Add(document.StyleRepository.GetStyle(run.StyleId));
//Add font to fonts collection
fonts.Add(run.Properties.FontFamily.GetActualValue());
}
}