none
How to get all the fonts used in a document using interop c#? RRS feed

  • Question

  • I need to make an application which list all the fonts used in a document.

    by using this code var val = document.Content.Font.Name; I am able to find the font name only if only one font is used through out the document. if multiple fonts were used the the value of val  becomes blank.

    https://1drv.ms/w/s!Ah-Jh2Ok5SuHbpH5J6dEs4coHd0 here I uploaded a sample file, in the document I used 3 type of font (Algerian,castellar, and Calibri) I need to find all three. I checked with Openxml there is showing the font name and I think i can get that but I have to use Interop only

    XML from OpenXml

    <w:r w:rsidRPr="004E2690">
    <w:rPr>
    <w:rFonts w:ascii="Algerian" w:hAnsi="Algerian"/>
    </w:rPr>
    <w:t>Madan mohan Mishra</w:t>
    </w:r>

     

    Thursday, October 20, 2016 12:59 PM

Answers

  • Hi,

    You could try the code below.

                var wdApp = new Microsoft.Office.Interop.Word.Application();
                wdApp.Visible = false;
                var doc = wdApp.Documents.Open(@"C:\Downloads\Font.docx");
                foreach (Word.Paragraph para in doc.Paragraphs)
                {
                        System.Windows.Forms.MessageBox.Show(para.Range.Font.Name);
                }
                wdApp.Quit();


    • Marked as answer by Madan Mishra Monday, October 24, 2016 12:48 PM
    Friday, October 21, 2016 7:15 AM
    Moderator

All replies

  • Hi,

    You could try the code below.

                var wdApp = new Microsoft.Office.Interop.Word.Application();
                wdApp.Visible = false;
                var doc = wdApp.Documents.Open(@"C:\Downloads\Font.docx");
                foreach (Word.Paragraph para in doc.Paragraphs)
                {
                        System.Windows.Forms.MessageBox.Show(para.Range.Font.Name);
                }
                wdApp.Quit();


    • Marked as answer by Madan Mishra Monday, October 24, 2016 12:48 PM
    Friday, October 21, 2016 7:15 AM
    Moderator
  • Thank you so much, I get the desired result.  I posted another question ( https://social.msdn.microsoft.com/Forums/en-US/30d96d70-4258-493b-b15a-d5d410e46c1f/how-to-find-the-detected-language-from-word-file?forum=worddev) Can you please help me to find the answer.


    Friday, October 21, 2016 7:29 AM
  • Hi,

    I am glad that you have found your desired method. We would appreciate if you could you share it with us, so that others could refer to your code if they have similar issues.

    Thanks for your understanding.

    Regards,

    Celeste

    Friday, October 21, 2016 9:31 AM
    Moderator
  • Do note that your code will only report a font if it's the only font used in the paragraph concerned. It will also not return anything about fonts used in headers, footers, textboxes and the like.

    The following macro will list all installed fonts used anywhere in the document. A progress report appears on the status bar, since there are typically many fonts to test.

    Sub ListInstalledFontsInUse()
    Dim SBar As Boolean, Fnt As Variant, Rng As Range, StrFnts As String
    Application.ScreenUpdating = False
    ' Store current Status Bar status, then switch on
    SBar = Application.DisplayStatusBar
    Application.DisplayStatusBar = True
    For Each Fnt In FontNames
      Application.StatusBar = "Now testing: " & Fnt
      For Each Rng In ActiveDocument.StoryRanges
        With Rng.Find
          .Format = True
          .Forward = True
          .Wrap = wdFindContinue
          .Font.Name = Fnt.Name
          .Execute
          If .Found = True Then
            StrFnts = StrFnts & vbCr & Fnt.Name
            Exit For
          End If
          .Font.Name = Fnt.Name & Fnt.NameAscii
          .Execute
          If .Found = True Then
            StrFnts = StrFnts & vbCr & Fnt.Name
            Exit For
          End If
          .Font.NameAscii = Fnt.NameAscii
          .Execute
          If .Found = True Then
            StrFnts = StrFnts & vbCr & Fnt.Name
            Exit For
          End If
        End With
      Next
    Next
    Application.ScreenUpdating = True
    Application.StatusBar = ""
    Application.StatusBar = SBar
    MsgBox "The installed fonts used in this document are:" & StrFnts
    End Sub

    I'll leave it to you to do the C# conversion

    A different method would be required for in-use fonts that are not installed on the current system. See, for example, the code I posted at: http://answers.microsoft.com/en-us/msoffice/forum/msoffice_word-mso_mac/how-can-i-find-if-fonts-are-missing-from-a-word/57d6c1f9-9c8f-4d95-b94e-adfbfa47f4dd. Even that code, however, only examines the document body. Although integrating both approaches into a single macro wouldn't be a lot of extra work, I haven't done so as the second one makes changes to the document being tested and isn't necessary for documents you create on the system you're testing on.


    Cheers
    Paul Edstein
    [MS MVP - Word]



    • Proposed as answer by Chenchen LiModerator Monday, October 24, 2016 4:46 AM
    • Marked as answer by Madan Mishra Monday, October 24, 2016 12:48 PM
    • Unmarked as answer by Madan Mishra Monday, October 24, 2016 12:48 PM
    • Edited by macropodMVP Sunday, April 30, 2017 11:58 PM More comprehensive checking
    Friday, October 21, 2016 9:55 PM
  • Hi,

    You could try the code below.

                var wdApp = new Microsoft.Office.Interop.Word.Application();
                wdApp.Visible = false;
                var doc = wdApp.Documents.Open(@"C:\Downloads\Font.docx");
                foreach (Word.Paragraph para in doc.Paragraphs)
                {
                        System.Windows.Forms.MessageBox.Show(para.Range.Font.Name);
                }
                wdApp.Quit();



    That approach will not work for content outside the document body (e.g. text boxes, headers, footers, footnotes, endnotes, etc.), or for any paragraph that uses mixed fonts.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, May 1, 2017 12:02 AM