Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 2.16 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to convert a Document Instance to a FileStream
  2. using System;
  3.  using System.Collections.Generic;
  4.  using System.Linq;
  5.  using System.Runtime.Serialization;
  6.  using System.ServiceModel;
  7.  using System.Text;
  8.  using Microsoft.Office.Interop.Word;
  9.  using System.IO;
  10.  
  11.  namespace DropDownTemplate.Web.WebServices.Word
  12.  {
  13.      // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "WordGenerator" in code, svc and config file together.
  14.      public class WordGenerator : IDocumentGenerator
  15.      {
  16.          FileStream IDocumentGenerator.GenerateDocument()
  17.          {
  18.              // For optional parameters create a missing object
  19.              object missing = System.Reflection.Missing.Value;
  20.              // open the document specified in the fileName variable
  21.              Document adoc = new Document();
  22.              adoc.InlineShapes.AddPicture(@"http://localhost:2014/Resources/MG.PNG", ref missing, ref missing, ref missing);
  23.              using (StreamReader y = new StreamReader())
  24.              {
  25.                  y.Read(adoc);  
  26.              }
  27.              return adoc;
  28.          }
  29.      }
  30.  }
  31.        
  32. public class WordGenerator : IDocumentGenerator
  33.  {
  34.          FileStream IDocumentGenerator.GenerateDocument()
  35.          {
  36.                 object missing = System.Reflection.Missing.Value;
  37.                 Document adoc = new Document();
  38.                 adoc.InlineShapes.AddPicture(@"http://localhost:2014/Resources/MG.PNG", ref missing, ref missing, ref missing);
  39.  
  40.                 // save the doc file
  41.                 object fileName = Path.GetTempFileName();
  42.                 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);
  43.  
  44.                 // quit word and release COM objects
  45.                 object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
  46.                 adoc.Application.Quit(ref saveChanges, ref missing, ref missing);
  47.                 Marshal.ReleaseComObject(adoc);
  48.  
  49.                 // return the stream
  50.                 return new FileStream((string)fileName, FileMode.Open);
  51.  
  52.          }
  53.  }