Free Support Forum - aspose.com

Inserting RTF preformatted text into a doc header

I am currently using Aspose.Word to create a word document with content as preformatted RTF. I am able to create multiple documents using the from the preformatted RTF content and finally merge them with below lines of code:


Dim encoding As New System.Text.ASCIIEncoding()
Dim dataBytes As Byte() = encoding.GetBytes(strRTFData)
Dim stream As New MemoryStream(dataBytes)
Dim loadOptions As New LoadOptions()
loadOptions.LoadFormat = LoadFormat.Rtf
Dim doc As New Document(stream, loadOptions)

doc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous
objDocument.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting)

The problem is the header and footer for the document is also preformatted RTF. Is there any way i can insert the preformatted RTF in header and footer while creating a word document.

Thanks!

Hi Rohit,


Thanks for your inquiry. What I understand, you’re merging multiple documents into one big document and you then want to create a header/footer in this final document from an Rtf string. Well, in case if your incoming Rtf string itself represents a Word document, you can load it inside a temporary Document object and copy the content from the Body area into the header/footer of your final document. Please see the following code that copies the Body content into the primary header of another document:

Document dstDoc = new
Document(@“C:\Temp\yourfinaldocument.docx”);

Document srcDoc = new Document(@"C:\Temp\DocumentRepresentingYourRtf.docx");

HeaderFooter header = new HeaderFooter(dstDoc, HeaderFooterType.HeaderPrimary);

dstDoc.FirstSection.HeadersFooters.Add(header);

foreach (Node srcNode in srcDoc.FirstSection.Body.ChildNodes)

{

Node dstNode = dstDoc.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);

header.AppendChild(dstNode);

}

dstDoc.Save(@"C:\Temp\out.docx");


Please let me know if I can be of any further assistance.

Best Regards,