Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerating a TOC in Word filled with html source in ASP.NET
    text
    copied!<p>I've been assigned to create a TOC (Table of contents) for Word document which it's filled with HTML code dynamically base on several information obtained from database and with a specific design, and finally this operation has to be done in a ASP. NET server side using 3.5 .NET Framework programmed in C# and it has to be sent to the client side with the TOC generated and in Word format.</p> <p>I can tell you too that the OS of the server is Windows 2008 Server, and it has Microsoft Office 2010 installed.</p> <p>So, firstly I try using Microsoft.Interoffice.Word integrating it in the server side code, but afterwards I realised that Microsoft didn't support this sort of things because of <a href="http://support.microsoft.com/kb/257757" rel="nofollow">security matters</a>. Then I try to create a console application which make this function (using Microsoft.Office.Interop.Word) and exporting the HTML code to doc document, both of them located in the same folder, and IIS_USERS group have full access to this location. But, when I try to launch the console application which creates the table of contents in the website code, it shows me an error related to the console application.</p> <p>I have tested the console application with doc generated filled with HTML code and it runs smooth, so I don't understand what's going on...does IIS detects that the console application uses Microsoft.Office.Interop.Word, and it doesn't allow it to execute? The idea was that when the console application finish, I would bring back the whole document again to website and generate an HTTP response to the client side so it would show the typical Open/Save popup dialog.</p> <p>I have been trying to use external libraries too without results like GemBox (FileFormat problem with HTML code), NPOI (its HWPF library it's in alpha version), OpenSDK (cannot evalute paging so it can't create a TOC wih the doc filled with info), AsPosed Words (quite expensive library, i can't try this kind of functions in free license)</p> <p>I show you know the console application code:</p> <pre><code> ArrayList arrValoresIndices = new ArrayList(); Application wordApp = new Application(); object missing = System.Type.Missing; try { Document wordDocument = wordApp.Documents.Open(docFileSource); //Applying style to headers System.Drawing.Color colorNecesario = System.Drawing.ColorTranslator.FromHtml("#1F497D"); WdColor coloraplicado = (Microsoft.Office.Interop.Word.WdColor)(colorNecesario.R + 0x100 * colorNecesario.G + 0x10000 * colorNecesario.B); wordApp.ActiveDocument.Styles[WdBuiltinStyle.wdStyleHeading1].Font.Name = "Calibri"; wordApp.ActiveDocument.Styles[WdBuiltinStyle.wdStyleHeading1].Font.Size = 14; wordApp.ActiveDocument.Styles[WdBuiltinStyle.wdStyleHeading1].Font.Bold = -1; wordApp.ActiveDocument.Styles[WdBuiltinStyle.wdStyleHeading1].Font.Color = coloraplicado; int numPalabras = wordDocument.Words.Count; for (int i = 1; i &lt; numPalabras; i++) { string texto = wordDocument.Words[i].Text; if (texto.Equals("") == false &amp;&amp; wordDocument.Words[i].Font.Size == 14 &amp;&amp; wordDocument.Words[i].Font.Name == "Calibri" &amp;&amp; wordDocument.Words[i].Font.Bold == -1 &amp;&amp; wordDocument.Words[i].Font.Color == coloraplicado) { wordDocument.Words[i].set_Style(WdBuiltinStyle.wdStyleHeading1); } } object gotoPage = WdGoToItem.wdGoToPage; object gotoNext = WdGoToDirection.wdGoToNext; object gotoCount = null; object gotoName = "2"; wordApp.Selection.GoTo(ref gotoPage, ref gotoNext, ref gotoCount, ref gotoName); wordApp.Selection.InsertBreak(WdBreakType.wdPageBreak); gotoName = "2"; Range indexPage= wordApp.Selection.GoTo(ref gotoPage, ref gotoNext, ref gotoCount, ref gotoName); object oTrue = true; TableOfContents toc = wordDocument.TablesOfContents.Add(indexPage, ref oTrue, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref oTrue); wordDocument.TablesOfContents.Format = WdTocFormat.wdTOCModern; toc.Update(); wordDocument.SaveAs2(docFileSource,WdSaveFormat.wdFormatFilteredHTML); wordDocument.Close(); wordApp.Quit(); } catch (COMException ce) { wordApp.Application.Quit(ref missing, ref missing, ref missing); throw new COMException("Process has failed ...\n"); } } </code></pre> <p>Web site code which launches the code:</p> <pre><code>String nombreDoc = "C:\\tempDocs\\JGA.doc"; //i will store the doc in this folder, in this folder IIS_USRS have full access File.WriteAllText(nombreDoc, strCabecera.ToString()); //strCabecera contains the whole html code Process p = new Process(); p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.StartInfo.FileName = @"C:\\tempDocs\\TOCIndexer\\TOCIndexer.exe -nombreDoc"; p.StartInfo.UseShellExecute = false; p.StartInfo.Verb = "runas"; using (Process exeProcess = Process.Start(p)) { exeProcess.WaitForExit(); } //When the process has finished I import againg the content to a new string builder StringBuilder strCabeceraVolcado = new StringBuilder(); using (var sr = new StreamReader(nombreDoc)) { strCabeceraVolcado.Append(sr.ReadToEnd()); } if (File.Exists(@nombreDoc)) { File.Delete(@nombreDoc); } strCabecera = strCabeceraVolcado; HttpContext.Current.Response.Write(strCabecera); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); </code></pre> <p>Could you give me some advice? </p> <p>Thanks</p>
 

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