c# - How can I use OpenXML SDK 2.5 to copy formulas from a word document? -


i have use openxml sdk 2.5 c# copy formulas 1 word document append them word document. tried below code, ran when tried open file, said there's wrong content. opened ignoring warning formulas not displayed. blank blocks.

my code:

private void createnewworddocument(string document, exercise[] exercices)         {             using (wordprocessingdocument worddoc = wordprocessingdocument.create(document, wordprocessingdocumenttype.document))             {                 // set content of document 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);             }          } 

this happens because not can referenced in paragraph copied when clone paragraph. word xml format consists of multiple files of reference each other. if copy paragraph 1 document need copy relationships may exist.

the openxml productivity tool useful diagnosing errors these. can open document tool , ask validate document.

i created test document contained hyperlink , ran code copy contents document. got error when attempted load using word opened in productivity tool , saw following output:

enter image description here

this shows hyperlink stored relationship rather inline in paragraph , new file references relationship doesn't exist. unzipping original file , new file , comparing 2 shows going on: document.xml original: document xml original

.rels of original rels of original

document.xml of generated file enter image description here

.rels of generated file enter image description here

note in generated file hyperlink references relationship rid5 doesn't exist in generated documents relationship file.

it's worth noting simple source documents code worked without issue there no relationships require copying.

there 2 ways can solve this. easiest way copy text of paragraph (you'll lose styles, images, hyperlinks etc) simple. need change

worddoc.maindocumentpart.document.body.appendchild(ex.toparagraph().clonenode(true)); 

for

paragraph para = worddoc.maindocumentpart.document.body.appendchild(new paragraph()); run run = para.appendchild(new run()); run.appendchild(new text(ex.toparagraph().innertext)); 

the more complex (and perhaps proper) way of achieving find relationships , copy them new document well. code doing beyond scope of can write here there interesting article on subject here http://blogs.msdn.com/b/ericwhite/archive/2009/02/05/move-insert-delete-paragraphs-in-word-processing-documents-using-the-open-xml-sdk.aspx.

essentially author of blog post using powertools openxml find relationships , copy them 1 document another.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -