Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Jan 15, 2018 7:07 pm

Hi, I'd like to loop through and entire doc and verify that no font size smaller than 10 and that only certain font type is used within the doc.

Need VB.Net script.

Kindly help - Thank you!

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Tue Jan 16, 2018 6:22 am

Hello,

Thanks for your inquiry. Please refer to the below code snippet to accomplish your task.
Code: Select all
      Shared Sub Main(ByVal args() As String)

         Dim fileName As String="sample.docx"
         Dim ressult = Verify(fileName)
      End Sub

      Private Shared Function Verify(ByVal fileName As String) As Boolean
         Dim result As Boolean = True
         Dim document As New Document()
         document.LoadFromFile(fileName)
         For Each section As Section In document.Sections
            'verify font in body
            For Each obj As DocumentObject In section.Body.ChildObjects
               If Not VerifyFont(obj, document) Then
                  result = False
                  Exit For
               End If
            Next obj

            'verify font in header footer
            For Each hf As HeaderFooter In section.HeadersFooters
               For Each hfobj As DocumentObject In hf.ChildObjects
                  If Not VerifyFont(hfobj, document) Then
                     result = False
                     Exit For
                  End If
               Next hfobj
            Next hf
         Next section
         Return result
      End Function
      Private Shared Function VerifyFont(ByVal obj As DocumentObject, ByVal document As Document) As Boolean
         Dim result As Boolean=True

         'verify font in tables
         If obj.DocumentObjectType = DocumentObjectType.Table Then
            For Each row As TableRow In (TryCast(obj, Table)).Rows
               For Each cell As TableCell In row.Cells
                  For Each [cobj] As DocumentObject In cell.ChildObjects
                     If Not VerifyFontInPara([cobj], document) Then
                        result = False
                        Exit For
                     End If
                  Next [cobj]
               Next cell
            Next row
         End If


         'verify font in paragraph
         If Not VerifyFontInPara(obj, document) Then
            result = False
         End If

         Return result
      End Function

      Private Shared Function VerifyFontInPara(ByVal obj As DocumentObject, ByVal document As Document) As Boolean
         If obj.DocumentObjectType = DocumentObjectType.Paragraph Then
            Dim objs = (TryCast(obj, Paragraph)).ChildObjects
            For i As Integer = 0 To objs.Count - 1
               If objs(i).DocumentObjectType = DocumentObjectType.TextRange Then
                  Dim tr As TextRange = TryCast(objs(i), TextRange)
                  If tr.CharacterFormat.FontSize < 10 OrElse tr.CharacterFormat.FontName <> "certain font1" OrElse tr.CharacterFormat.FontName <> "certain font2" Then
                     Return False
                  End If
               End If
            Next i
         End If
         Return True
      End Function


Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Tue Jan 16, 2018 8:02 am

Thank you for your quick reply! - I will check and let you know if it works as expected.

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Wed Jan 24, 2018 9:50 am

Hi, I checked the code but it looks like it doesn't function properly i.e. the results are wrong - I defined certain font types allowed in the code but it shows true when the document checked contains a different font type from these 3 types.

Kindly check the code again - Thank you!

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Thu Jan 25, 2018 3:56 am

Hello,

Thanks for your feedback. To help us check the code, would you please share your document and tell us which font you were verifying?

Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Tue Jan 30, 2018 6:16 am

See attached - I checked that font type is > 10 along the entire doc text and that font used is either Arial or Calibri or Times Roman along the entire doc text.

Thanks!

kdr13
 
Posts: 72
Joined: Fri Apr 15, 2016 4:35 pm

Tue Jan 30, 2018 10:22 am

Hello,

Thanks for your sharing. I verified your document and it always return false. In addition, I found the logic of my code was not correct.
Please change :
Code: Select all
if (tr.CharacterFormat.FontSize < 11 || tr.CharacterFormat.FontName != "Calibri" || tr.CharacterFormat.FontName != "Arial" || tr.CharacterFormat.FontName != "Times Roman")

into:
Code: Select all
if (tr.CharacterFormat.FontSize < 11 || !(tr.CharacterFormat.FontName == "Calibri" || tr.CharacterFormat.FontName == "Arial" || tr.CharacterFormat.FontName == "Times Roman"))


Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Return to Spire.Doc