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

Free Support Forum - aspose.com

Insert RTF text into document

Hi, I read in this forum, that in 2007 was made method in Aspose.Word how is possible import or insert RTF text into word document, but i can’t find how is possible to do that. Can anyone help me? How can i add some RTF string into word document?

Hi there,

Thanks for your inquiry.

There is still no direct way to insert RTF into a document. You can however easily achieve this by loading your RTF string into a new document and inserting that into your exisiting document. Please see the code implementation below. It also uses the InsertDocument method found in this article here.

// Load the base document.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Document doc = new Document("Document.doc");

// Define the RTF string snippet to insert into the document

string rtfString = @"{\pard Test {\b content}.\par";

// Insert the string at the first paragraph in the document.

InsertRTFIntoDocument(doc.FirstSection.Body.FirstParagraph, rtfString);

// Save the output.

doc.Save("Document out.doc");

///

/// Inserts RTF formatted string into the document at the specified node.

///

/// The node where to insert the content

/// The string containg the RTF formatted content.

public static void InsertRTFIntoDocument(Node insertAfterNode, string rtfString)

{

// Check for the format identifier at the begining of the string. If this does not exist

// add it as it is needed for the content to be loaded into a new document.

const string magicNumber = "{\\rtf1";

if (!rtfString.StartsWith(magicNumber))

rtfString = string.Concat(magicNumber, rtfString);

// Convert the string into an array of bytes and pass it into a new memory stream.

UTF8Encoding encoding = new UTF8Encoding();

MemoryStream stream = new MemoryStream(encoding.GetBytes(rtfString));

// Check this content stream to see if it is recognized as valid RTF content.

FileFormatInfo info = FileFormatUtil.DetectFileFormat(stream);

if (info.LoadFormat != LoadFormat.Rtf)

throw new ArgumentException("The RTF string passed is invalid.");

// Load RTF stream into a new document.

Document rtfDoc = new Document(stream);

// Insert the loaded content into the document.

InsertDocument(insertAfterNode, rtfDoc);

}

If you have any further queries, please feel free to ask.

Thanks,

Thanks this help me…

Hi,

Is there a direct way to insert Editable rtf text document into Word document? Can any one help me?

Hi

Thanks for your request. There is no direct way to insert RTF, the method suggested by Adam is the only way you can insert RTF into your document. Please let us know if you need more assistance, we will be glad to help you.

Best regards,

Hi,

I am using Aspose words for Java. Can I write a similar program to insert Rtf document into word document.

Hi Kalyani,


Thanks for your inquiry.

Sure, the code example above can be ported directly to Java. Please let us know if you have any problems during translation.

Thanks,