Note that there are some explanatory texts on larger screens.

plurals
  1. POTurning a stream into WordDoc without referencing the SDK
    primarykey
    data
    text
    <p>So I've created a C# library that manipulates and edits Word Documents. This of course references the OpenXML SDK. Where I use the library however I do not wan't to have to reference the .dll as well.</p> <p>One method that I might use from another project has a <code>WordprocessingDocument</code> parameter and has this profile:</p> <pre><code>public bool FillTemplate(ref WordprocessingDocument document, XElement data) { //EDIT the document and return True if succesful. } </code></pre> <p>problem here is of course that I would have to create a <code>WordprocessingDocument</code> inside the other project instead of just passing a stream.</p> <p>Ok I guess the simplest solution would be a different profile on the method:</p> <pre><code>public bool FillTemplate(Stream document, XElement data) { WordprocessingDocument doc = WordprocessingDocument.Open(document, true); return FillTemplate(doc, data); } </code></pre> <p>But I got what I thought would be a brilliant idea just to create a extension method for Stream:</p> <pre><code>public static WordprocessingDocument ConvertToWordDocument(this Stream stream, bool isEditable) { return WordprocessingDocument.Open(stream, isEditable); } </code></pre> <p>and use it like this:</p> <pre><code>FileStream fStream = new FileStream(@"C:\Users\Me\Desktop\SomeDoc.docx", FileMode.Open); var doc = fStream.ConvertToWordDocument(true); filler.FillTemplate(ref doc, getXmlDataFor(42)); fStream.Flush(); fStream.Close(); </code></pre> <p>However this doesn't work for some reason (Doc changes but it doesn't seem to get returned to the stream) and I got a little skeptical about the whole idea of how I'm using Streams and the <code>WordprocessingDocument</code> package/wrapper thingy.</p> <p>What would be a optimal solution so I'm not going into a whole lot of trouble? How actually does the <code>WordprocessingDocument</code> class work in relation to passing it around as a parameter and such? Why didn't the stream change the originally opened document?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload