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.
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.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.