How to set font formatting of whole text in the document using VB.NET

Hi, Support:

Is there any way to format a doc quickly? such as set the each Paragraph LineSpace to 20 pound, Set the fontname of all the words to “Times New Roman”, Set the color of all the words to black,…
I use the method as following:

For Each Para As Global.Aspose.Words.Paragraph In doc.GetChildNodes(Global.Aspose.Words.NodeType.Paragraph, True)
Para.ParagraphFormat.LineSpacing = Global.Aspose.Words.ConvertUtil.InchToPoint(12 / 72)
next

For Each run As Global.Aspose.Words.Run In runs
run.Font.Size = Global.Aspose.Words.ConvertUtil.InchToPoint(TFontSize.Text / 72)
run.Font.Color = System.Drawing.Color.Black
next

The above code can make the goal, however it took a long time to complete the work.
Is there any way to do this quickly like MS Office Word?

Another question is that how to quickly search one or more special paragraphs or words, and then quickly format them (such as set the fontsize, fontname, fontcolor, paragraph line space, fontbold…and so on) . I used the method like the above, but it always takes a long time to finish the task.

Using the above codes, it will take about more than one minute to finish a doc, whereas it only takes about several second to finish the task in MS Office Word.

Thanks for your suggestion.

Ducaisoft

@ducaisoft

Please use the following code example to change the font name of text in whole document. Hope this helps you.

Private Sub SurroundingSub()
    Dim doc As Document = New Document(MyDir & "input.docx")
    Dim changer As FontChanger = New FontChanger()
    doc.Accept(changer)
    doc.Save(MyDir & "out.docx")
End Sub

Class FontChanger
    Inherits DocumentVisitor

    Public Overrides Function VisitFieldEnd(ByVal fieldEnd As FieldEnd) As VisitorAction
        ResetFont(fieldEnd.Font)
        Return VisitorAction.[Continue]
    End Function

    Public Overrides Function VisitFieldSeparator(ByVal fieldSeparator As FieldSeparator) As VisitorAction
        ResetFont(fieldSeparator.Font)
        Return VisitorAction.[Continue]
    End Function

    Public Overrides Function VisitFieldStart(ByVal fieldStart As FieldStart) As VisitorAction
        ResetFont(fieldStart.Font)
        Return VisitorAction.[Continue]
    End Function

    Public Overrides Function VisitFootnoteEnd(ByVal footnote As Footnote) As VisitorAction
        ResetFont(footnote.Font)
        Return VisitorAction.[Continue]
    End Function

    Public Overrides Function VisitFormField(ByVal formField As FormField) As VisitorAction
        ResetFont(formField.Font)
        Return VisitorAction.[Continue]
    End Function

    Public Overrides Function VisitParagraphEnd(ByVal paragraph As Paragraph) As VisitorAction
        ResetFont(paragraph.ParagraphBreakFont)
        Return VisitorAction.[Continue]
    End Function

    Public Overrides Function VisitRun(ByVal run As Run) As VisitorAction
        ResetFont(run.Font)
        Return VisitorAction.[Continue]
    End Function

    Public Overrides Function VisitSpecialChar(ByVal specialChar As SpecialChar) As VisitorAction
        ResetFont(specialChar.Font)
        Return VisitorAction.[Continue]
    End Function

    Private Sub ResetFont(ByVal font As Aspose.Words.Font)
        font.Name = mNewFontName
    End Sub

    Private mNewFontName As String = "Arial"
End Class

In ResetFont, you can set the font formatting of Run node.

In this case, we suggest you please implement IReplacingCallback interface. You can get the matched node in replacing method and set font formatting of text according to your requirement. The ReplacingArgs.MatchNode is Run node. Moreover, you can get the parent of Run node using Run.ParentParagraph property and format it.

Please read the following article.
Find and Replace

Thank you very much!
And could you please convert the C codes as VB.net codes for me and show some demo to work them out?

Task1: format the font size, font name, font color, font bold … of the words in whole document quickly

Task2: format the font size, font name, font color, font bold, line space, indent … of the paragraphs in whole document quickly

Task3: search and then replace/delete some special string or some special paragraphs in the document quickly

Task4: search some special string or some paragraphs and then format their styles such as font size, font name, font color, font bold … quickly

@ducaisoft

We have modified the code example in the previous post.

Could you please ZIP and attach your input and expected output Word documents? We will then provide you code examples according to your requirement.