How to determine Font Family, Style for specify Run

1 Answer 133 Views
WordsProcessing
Omid
Top achievements
Rank 1
Omid asked on 02 Dec 2022, 09:04 AM

Hi 

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

Sort by
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());
    }
}

Hope this helps.

Regards, Yoan Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
WordsProcessing
Asked by
Omid
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Share this question
or