You could embed the content of a RTF
document into a OpenXML DOCX
file
by using the AltChunk
anchor for external content. The AltChunk
(w:altChunk
) element specifies
a location in your OpenXML WordprocessingML document to insert external content such as a RTF
document.
The code below uses the AltChunk
class in conjunction with the AlternativeFormatImportPart
class
to embed the content of a RTF
document into a DOCX
file after the last paragraph:
using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(@"your_docx_file.docx", true))
{
string altChunkId = "AltChunkId5";
MainDocumentPart mainDocPart = wordDocument.MainDocumentPart;
AlternativeFormatImportPart chunk = mainDocPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.Rtf, altChunkId);
// Read RTF document content.
string rtfDocumentContent = File.ReadAllText("your_rtf_document.rtf", Encoding.ASCII);
using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(rtfDocumentContent)))
{
chunk.FeedData(ms);
}
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
// Embed AltChunk after the last paragraph.
mainDocPart.Document.Body.InsertAfter(
altChunk, mainDocPart.Document.Body.Elements<Paragraph>().Last());
mainDocPart.Document.Save();
}
If you want to embed an Unicode RTF
string into a DOCX
file then you have to escape the Unicode characters. For an example please refer to the following stackoverflow answer.
When you encounter the error "the file is corrupt" then ensure that you Dispose()
or Close()
the WordprocessingDocument
. If you do not Close() the document then the releationship for the w:altchunk
is not stored in the Document.xml.rels file.