c# - asp.net Microsoft.Office.Interop.Word -


i trying rid of empty pages merge when users views , prints document. using dev express editor user inserts text make page on flow other page (rtf) , creats empty space. way stop happening>?

here code:

     using system;      using system.collections.generic;      using system.drawing.imaging;      using system.linq;      using system.io;      using system.text;      using system.text.regularexpressions;      using system.threading.tasks;      using system.xml;      using system.xml.linq;      using documentformat.openxml.packaging;        using microsoft.office.interop.word;      using wordapplication = microsoft.office.interop.word.application;        namespace documentmapper      {      public class xmldocumentmapper      {     /// <summary>     /// use replace text elements included in template text.     /// </summary>     /// <param name="mergedocinfo"></param>     /// <param name="html"></param>     /// <returns></returns>     public static string mergehtml(mergedocinfo mergedocinfo, string html)     {         return mergetext(mergedocinfo.mergevalues, html);     }      public static string mergehtml(dictionary<string, string> mergevalues, string html)     {         return mergetext(mergevalues, html);     }      public static string mergehtml(mergedocinfo mergedocinfo)     {         string htmltext = null;         string template = string.format("{0}{1}", mergedocinfo.templatepath, mergedocinfo.templatename.replace(".docx", ".html"));         using (stream stream = new filestream(template, filemode.open, fileaccess.read, fileshare.readwrite))         {             using (streamreader sr = new streamreader(stream))             {                 htmltext = sr.readtoend();             }         }          return mergetext(mergedocinfo.mergevalues, htmltext);     }      public static void searchandreplace(mergedocinfo mergedocinfo)     {         string template = string.format("{0}{1}", mergedocinfo.templatepath, mergedocinfo.templatename);         string document = string.format("{0}{1}", mergedocinfo.mergedocpath, mergedocinfo.mergedocname);         using (stream stream = new filestream(template, filemode.open, fileaccess.read, fileshare.readwrite))         using (wordprocessingdocument wordtemplate = wordprocessingdocument.open(stream, false))         using (wordprocessingdocument worddocument = wordprocessingdocument.create(document, documentformat.openxml.wordprocessingdocumenttype.document))         {             foreach (var part in wordtemplate.parts)                 worddocument.addpart(part.openxmlpart, part.relationshipid);              string doctext = null;             using (streamreader sr = new streamreader(wordtemplate.maindocumentpart.getstream()))             {                 doctext = sr.readtoend();             }              doctext = mergetext(mergedocinfo.mergevalues, doctext);              using (streamwriter sw = new streamwriter(worddocument.maindocumentpart.getstream(filemode.create)))             {                 sw.write(doctext);             }         }          fileinfo tempfile = new fileinfo(document);         fileinfo mergefile = new fileinfo(document.replace(".docx", ".pdf"));          doit(tempfile, mergefile);     }      private static string mergetext(dictionary<string, string> mergevalues, string text)     {         var datevalues = new dictionary<string, string>();         foreach (keyvaluepair<string, string> pair in mergevalues)         {             datetime dt;             if (datetime.tryparse(pair.value, out dt))             {                 datevalues.add(pair.key, pair.value);             }         }          foreach (keyvaluepair<string, string> pair in datevalues)         {             mergevalues[pair.key] = datetime.parse(pair.value).toshortdatestring();         }          foreach (keyvaluepair<string, string> pair in mergevalues)         {             if (!string.isnullorempty(pair.key))             {                 text = new regex(pair.key).replace(text, string.isnullorempty(pair.value) ? "_____" : pair.value);             }         }          return text;     }      /// <summary>     /// obsolete     /// </summary>     /// <param name="tempfile"></param>     /// <param name="mergefile"></param>     private static void doit(fileinfo tempfile, fileinfo mergefile)     {         wordapplication word = new wordapplication()         {             visible = false,             screenupdating = false         };          object omissing = system.reflection.missing.value;         object filename = (object)tempfile.fullname;          document doc = word.documents.open(ref filename, ref omissing,             ref omissing, ref omissing, ref omissing, ref omissing, ref omissing,             ref omissing, ref omissing, ref omissing, ref omissing, ref omissing,             ref omissing, ref omissing, ref omissing, ref omissing);         doc.activate();          object outputfilename = (object)mergefile.fullname;         object fileformat = wdsaveformat.wdformatpdf;          doc.saveas(ref outputfilename,             ref fileformat, ref omissing, ref omissing,             ref omissing, ref omissing, ref omissing, ref omissing,             ref omissing, ref omissing, ref omissing, ref omissing,             ref omissing, ref omissing, ref omissing, ref omissing);          object savechanges = wdsaveoptions.wddonotsavechanges;         ((_document)doc).close(ref savechanges, ref omissing, ref omissing);         doc = null;          ((_application)word).quit(ref omissing, ref omissing, ref omissing);         word = null;          tempfile.delete();     } }   } 

office interop not supported non user sessions (for example asp.net service), may explain strange behavior seeing.

you need switch supported solution manipulating documents non user session open xml sdk before attempt correct other problems.

update: re-reading question, using open xml (the code in searchandreplace using it) need replace code in doit use open xml too.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -