一个RTF文件中嵌入的内容将使用的OpenXML SDK一个DOCX文件文件、内容、RTF、DOCX

2023-09-04 08:09:16 作者:陪伴到白头

在我们的旧的MSWord-97为基础的系统,我们使用COM与.doc文件进行交互,并嵌入OLE对象,所以嵌入式文件是可见的父(而不是作为一个图标)。

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).

我们正在使用的OpenXML SDK,因为它需要我们的服务器,其产生的.docx文件上有话语体系取代这一点。然而,我们仍然需要的RTF文件的内容嵌入到生成的DOCX ...具体来说,我们用文件的内容替换书签。

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.

我发现了一些例子在线,但他们都不同。当我在Word中创建一个简单的例子,并查看XML,有一个的很多东西应用于位置/显示嵌入对象的视觉重新presentation,而嵌入本身似乎并没有太可怕。什么是做到这一点的最简单的方法是什么?

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?

推荐答案

您可以嵌入 RTF 文件的内容转换为OpenXML的 DOCX 文件 通过使用 AltChunk 锚外部内容。该 AltChunk W:altChunk )元素指定 在你的OpenXML Wordpro​​cessingML文档的位置插入外部内容,如 RTF 文件。 下面的code使用 AltChunk AlternativeFor​​matImportPart 类一起班 嵌入 RTF 文件的内容转换为 DOCX 的最后一段文件:

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();
}

如果您希望嵌入统一code RTF 字符串转换成 DOCX 文件,那么你必须逃脱的Uni code字符。为一个例子,请参考以下stackoverflow回答。

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.

当您遇到错误的文件已损坏的,那么请确保您的Dispose()关闭( ) Wordpro​​cessingDocument 。如果没有关闭()的文件则releationship为 W:altchunk 不存储在 Document.xml.rels 的文件

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.