- How to convert a Document Instance to a FileStream
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.Text;
- using Microsoft.Office.Interop.Word;
- using System.IO;
- namespace DropDownTemplate.Web.WebServices.Word
- {
- // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "WordGenerator" in code, svc and config file together.
- public class WordGenerator : IDocumentGenerator
- {
- FileStream IDocumentGenerator.GenerateDocument()
- {
- // For optional parameters create a missing object
- object missing = System.Reflection.Missing.Value;
- // open the document specified in the fileName variable
- Document adoc = new Document();
- adoc.InlineShapes.AddPicture(@"http://localhost:2014/Resources/MG.PNG", ref missing, ref missing, ref missing);
- using (StreamReader y = new StreamReader())
- {
- y.Read(adoc);
- }
- return adoc;
- }
- }
- }
- public class WordGenerator : IDocumentGenerator
- {
- FileStream IDocumentGenerator.GenerateDocument()
- {
- object missing = System.Reflection.Missing.Value;
- Document adoc = new Document();
- adoc.InlineShapes.AddPicture(@"http://localhost:2014/Resources/MG.PNG", ref missing, ref missing, ref missing);
- // save the doc file
- object fileName = Path.GetTempFileName();
- adoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
- // quit word and release COM objects
- object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
- adoc.Application.Quit(ref saveChanges, ref missing, ref missing);
- Marshal.ReleaseComObject(adoc);
- // return the stream
- return new FileStream((string)fileName, FileMode.Open);
- }
- }