How to use OpenXML SDK 2.5 to copy formulas from a Word document?

I need to use the OpenXML SDK 2.5 with C # to copy formulas from one word of a document, and then add them to another document. I tried the code below, it worked successfully, but when I tried to open the file, it said that something was wrong with the contents. I opened it, ignoring the warning, but these formulas did not appear. These are just empty blocks.

My code is:

private void CreateNewWordDocument(string document, Exercise[] exercices)
        {
            using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(document, WordprocessingDocumentType.Document))
            {
                // Set the content of the document so that Word can open it.
                MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();

                SetMainDocumentContent(mainPart);
                foreach (Exercise ex in exercices)
                {
                    wordDoc.MainDocumentPart.Document.Body.AppendChild(ex.toParagraph().CloneNode(true));
                }
                wordDoc.MainDocumentPart.Document.Save();
            }
        }

        // Set content of MainDocumentPart.
        private void SetMainDocumentContent(MainDocumentPart part)
        {
            string docXml =
         @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> 
 <w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
     <w:body><w:p><w:r><w:t>Exercise list!</w:t></w:r></w:p></w:body>
 </w:document>";

            using (Stream stream = part.GetStream())
            {
                byte[] buf = (new UTF8Encoding()).GetBytes(docXml);
                stream.Write(buf, 0, buf.Length);
            }

        }
+4
source share
1 answer

, , , . Word XML , . , , .

OpenXML Productivity Tool . .

, . , Word, :

enter image description here

, , , , . , : document.xml : Document XML from original

.rels rels of original

document.xml enter image description here

.rels enter image description here

, rId5, .

, , , .

. - ( , , ..), . , ,

wordDoc.MainDocumentPart.Document.Body.AppendChild(ex.toParagraph().CloneNode(true));

Paragraph para = wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(ex.toParagraph().InnerText));

(, , ) - . , , , , http://blogs.msdn.com/b/ericwhite/archive/2009/02/05/move-insert-delete-paragraphs-in-word-processing-documents-using-the-open-xml-sdk.aspx.

Powertools for OpenXML, .

+4

Source: https://habr.com/ru/post/1546387/


All Articles