c# - Embed contents of a RTF file into a DOCX file using OpenXML SDK -
In our old MSWord-97 based system, we want to interact with COM with a .doc file and embed an OLE object Uses embedded document appears in the parent (not in the form of an icon).
We are changing it with a system using OpenXml SDK because it requires words on our server, which generates .docx files. However, we still need to embed the contents of the RTF files into the DOCX ... In particular, we change a bookmark with the contents of the file.
I get some examples online but they are all different when I make a simple example in Word and look at XML, to display / display the embedded object's visual presentation, a < Em> Too many , while embedding does not seem very terrible to itself. What is the easiest way to do this?
You RTF
AltChunk
for external content Document using an anchor in an openXML DOCX
file. The AltChunk
( w: altChunk
) element to insert external content such as RTF
document specifies a location in your OpenXML wordprocingmail document below AltFunk
class with AlternativeFormatImportPart
class to embed the content of the document DOCX
Uses code> file after the last paragraph:
using (WordprocessingDocument w OrdDocument = WordprocessingDocument.Open (@ "your_docx_file.docx", true)) {string altChunkId = "AltChunkId5"; MainDocumentPart Main Docpart = Word document.mandocumentpart; Alternative Formats Export Chart = Main DockPart.AdidateFormatEmortortPort (OptionalFormatExtertyType. RTF, AltChunIID); // Read the RTF document content string rtfDocumentContent = File.ReadAllText ("your_rtf_document.rtf", encoding.ASCII); (Chunk Fadedata (MS);} AltChunk altChunk = New AltChunk (); AltChunk.Id = altChunkId; // The last paragraph using the memorystream MS = new memorystream (encoding.accii.getbites (RTF document content) After that, add AltChunk to MainDocPart.Document.Body.InsertAfter (altChunk, mainDocPart.Document.Body.Elements & lt; paragraph & gt; (). Last ()); mainDocPart.Document.Save ();}
If you want to embed a Unicode RTF
string in a DOCX
file For example, please refer to the following:
When you type the error "File is corrupt " then make sure that you dissection ()
or close ()
to the wordprocessing document
. If you do not close the document, then rectilible w: altchunk
Document.xml.rels is not stored in the file.
Comments
Post a Comment