c# - Converting from 'WordOpenXML' to In-Memory System.IO.Packaging.Package -


when using vsto 2012 manipulate ms word document, see document has 'wordopenxml' string property, xml representation of files constituting .docx package saved disk when saving word document .docx.

i want convert string equivalent system.io.packaging.package object in memory.

the so question here similar. indeed, op mentions 'in memory' in question. however, answers given involve saving package disk using system.io.packaging.zippackage.open method. not want save package disk , have open again using wordprocessingdocument.open() method. rather, want done in memory , not involve file system @ all.

i see wordprocessingdocument.open() has overload takes stream. however, i'm not sure how prepare such stream wordopenxml string, although suspect post referenced above gives of answer.

you can use method in memory stream wordopenxml string:

/// <summary>     /// returns system.io.packaging.package stream given word open xml.     /// </summary>     /// <param name="wordopenxml">the word open xml.</param>     /// <returns></returns>     public static memorystream getpackagestreamfromwordopenxml(string wordopenxml)     {         xdocument doc = xdocument.parse(wordopenxml);         xnamespace pkg =            "http://schemas.microsoft.com/office/2006/xmlpackage";         xnamespace rel =             "http://schemas.openxmlformats.org/package/2006/relationships";         package inmemorypackage = null;         memorystream memstream = new memorystream();         using (inmemorypackage = package.open(memstream, filemode.create))         {             // add parts (but not relationships)             foreach (var xmlpart in doc.root                 .elements()                 .where(p =>                     (string)p.attribute(pkg + "contenttype") !=                     "application/vnd.openxmlformats-package.relationships+xml"))             {                 string name = (string)xmlpart.attribute(pkg + "name");                 string contenttype = (string)xmlpart.attribute(pkg + "contenttype");                 if (contenttype.endswith("xml"))                 {                     uri u = new uri(name, urikind.relative);                     packagepart part = inmemorypackage.createpart(u, contenttype,                         compressionoption.superfast);                     using (stream str = part.getstream(filemode.create))                     using (xmlwriter xmlwriter = xmlwriter.create(str))                         xmlpart.element(pkg + "xmldata")                             .elements()                             .first()                             .writeto(xmlwriter);                 }                 else                 {                     uri u = new uri(name, urikind.relative);                     packagepart part = inmemorypackage.createpart(u, contenttype,                         compressionoption.superfast);                     using (stream str = part.getstream(filemode.create))                     using (binarywriter binarywriter = new binarywriter(str))                     {                         string base64stringinchunks =                        (string)xmlpart.element(pkg + "binarydata");                         char[] base64chararray = base64stringinchunks                             .where(c => c != '\r' && c != '\n').toarray();                         byte[] bytearray =                             system.convert.frombase64chararray(base64chararray,                             0, base64chararray.length);                         binarywriter.write(bytearray);                     }                 }             }             foreach (var xmlpart in doc.root.elements())             {                 string name = (string)xmlpart.attribute(pkg + "name");                 string contenttype = (string)xmlpart.attribute(pkg + "contenttype");                 if (contenttype ==                     "application/vnd.openxmlformats-package.relationships+xml")                 {                     // add package level relationships                     if (name == "/_rels/.rels")                     {                         foreach (xelement xmlrel in                             xmlpart.descendants(rel + "relationship"))                         {                             string id = (string)xmlrel.attribute("id");                             string type = (string)xmlrel.attribute("type");                             string target = (string)xmlrel.attribute("target");                             string targetmode =                                 (string)xmlrel.attribute("targetmode");                             if (targetmode == "external")                                 inmemorypackage.createrelationship(                                     new uri(target, urikind.absolute),                                     targetmode.external, type, id);                             else                                 inmemorypackage.createrelationship(                                     new uri(target, urikind.relative),                                     targetmode.internal, type, id);                         }                     }                     else                     // add part level relationships                     {                         string directory = name.substring(0, name.indexof("/_rels"));                         string relsfilename = name.substring(name.lastindexof('/'));                         string filename =                             relsfilename.substring(0, relsfilename.indexof(".rels"));                         packagepart frompart = inmemorypackage.getpart(                             new uri(directory + filename, urikind.relative));                         foreach (xelement xmlrel in                             xmlpart.descendants(rel + "relationship"))                         {                             string id = (string)xmlrel.attribute("id");                             string type = (string)xmlrel.attribute("type");                             string target = (string)xmlrel.attribute("target");                             string targetmode =                                 (string)xmlrel.attribute("targetmode");                             if (targetmode == "external")                                 frompart.createrelationship(                                     new uri(target, urikind.absolute),                                     targetmode.external, type, id);                             else                                 frompart.createrelationship(                                     new uri(target, urikind.relative),                                     targetmode.internal, type, id);                         }                     }                 }             }             inmemorypackage.flush();         }         return memstream;     } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -