简体   繁体   中英

Embed contents of a RTF file into a DOCX file using OpenXML SDK

In our old MSWord-97 based system we use COM to interact with a .doc file, and embed an OLE object, so the embedded document is visible in the parent (not as an icon).

We're replacing this with a system using OpenXML SDK since it requires having Word on our server, which generates .docx files. however we still need to embed the contents of RTF files into the generated DOCX... specifically we replace a bookmark with the contents of the file.

I found a few examples online but they all differ. When I create a simple example in Word and view the XML, there's a lot of stuff to position/display the embedded object's visual representation, while the embedding itself doesn't seem too horrific. What's the easiest way to do this?

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.

这个家伙似乎已经用他自己的问题和答案弄明白我如何使用OpenXml 2.0将任何文件类型嵌入到Microsoft Word中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM