We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to Save the format of text in Word document?

Hello,

I using Aspose.word.DocumentVisitor class to retreive text from a word file. Here is the function that is returning me the text:

Public Overrides Function VisitParagraphStart(ByVal paragraph As Aspose.Words.Paragraph) As Aspose.Words.VisitorAction

Return VisitorAction.Continue

End Function

Using this function I know I can save get the text of paragraph by using paragraph.range.text. But what I want is I want to save it in exact format, for example if the text is Bold and Italic, I want to save it as is.

Can you please give me an example or point me out to a document that shows how to do that ? thanks in advance

Regards,

Jerry

Hi Jerry.

What do you mean under “save it”? Saving text with any formatting implies using some file format understandable by corresponding software. Do you need to get one paragraph to a separate doc file?

Regards,

Yes, All I want is to be able to get the text from the paragraph and save it to a different doc file while preserving the format (Text Color, Bold, Italic, Underling etc.).

Hi

Try to use NodeImporter class to achieve this. See the following code.

Module Module1

Sub Main()

Dim doc As Aspose.Words.Document = New Aspose.Words.Document("in.doc")

Dim visitor As MyVisitor = New MyVisitor()

doc.Accept(visitor)

End Sub

End Module

Public Class MyVisitor

Inherits Aspose.Words.DocumentVisitor

Public Sub New()

End Sub

Public Overrides Function VisitParagraphStart(ByVal paragraph As Aspose.Words.Paragraph) As Aspose.Words.VisitorAction

Dim doc As Aspose.Words.Document = New Aspose.Words.Document()

Dim importer As Aspose.Words.NodeImporter = New Aspose.Words.NodeImporter(paragraph.Document, doc, Aspose.Words.ImportFormatMode.KeepSourceFormatting)

Dim par As Aspose.Words.Paragraph = importer.ImportNode(paragraph, True)

doc.FirstSection.Body.AppendChild(par)

doc.Save("paragraph_" + parNum.ToString() + ".doc")

parNum = parNum + 1

Return Aspose.Words.VisitorAction.Continue

End Function

Private parNum As Integer = 0

End Class

I hope that it will help you.

Best regards.