//tsk.BeginInvoke(new AsyncCallback(cbGetProcessedHtml), tsk);
#endif//with_async
        /// <summary>
        /// Put links and terms
        /// </summary>
        /// <param name="b">clear all links from the source before setting</param>
        /// <returns>true or false depending on the success</returns>
        public bool PutLinksAndTerms(bool b)
        {
            bool success = false;
            if (CurrentDoc == null)
                return false;
#if with_LinkHint
#if with_LinkCache
            LinkCache.Clear();
#endif//with_LinkCache
            if (bUseHtml)
            {/*+*/
                Application.System.Cursor = Word.WdCursorType.wdCursorWait;
                //RemoveLinksAndTerms();
                string output = "";
                PutLinksAndTermsHtml(sHtml(), ref output);
                string fn = System.IO.Directory.GetCurrentDirectory() + fn_HtmlRes;// @"c:\result-html.html";
                object ofn = fn;
                WordApp.Documents.Open(ofn);
                WordApp.ActiveWindow.Application.WindowBeforeRightClick += new Word.ApplicationEvents4_WindowBeforeRightClickEventHandler(application_WindowBeforeRightClick);
                goto l_exit;
                //Application.System.Cursor = Word.WdCursorType.wdCursorNormal;
                //return true;
                //Word.Document ddd = WordApp.Documents.Open(ofn);
                //ddd.ActiveWindow.Application.WindowBeforeRightClick -= MyHandler;
                //ddd.ActiveWindow.Application.WindowBeforeRightClick += MyHandler;                       
                //new Word.ApplicationEvents4_WindowBeforeRightClickEventHandler(application_WindowBeforeRightClick);
            }/*+*/
            Application.System.Cursor = Word.WdCursorType.wdCursorWait;
            RemoveLinksAndTerms();
            string txt_cl = CurrentDoc.Content.Text;//Get The whole document text
            //CurrentDoc.ActiveWindow.Selection.WholeStory();
            //CurrentDoc.SelectAllEditableRanges();
            //WordApp.Selection.Text = txt_cl;
            CurrentDoc.Content.Text = txt_cl;
            ServiceReference.LinkingServiceClient cl = new ServiceReference.LinkingServiceClient();
            EUCases.EULinksCheckerWordAddIn.ServiceReference.TextPosWrapper res_cl = cl.PutLinksText(txt_cl);
            Word.Document cd = CurrentDoc;
            foreach (EUCases.EULinksCheckerWordAddIn.ServiceReference.LinkTextPos a in res_cl.LinksTextPos)
            {
                int pos = a.Pos,
                    len = a.Length;
                string id = a.Celex;
#if with_LinkHint//20150616
                string doc_hint = GetDocHint(a.Language, id, a.ReferrenceInfo),
                    plain = StripHtml(doc_hint);
#endif//with_LinkHint
                //                ShowStrInBrowser(doc_hint);
                //Word.Document cd = CurrentDoc;
                Word.Range rng = cd.Range(pos, pos + len);
                string rngTxt = rng.Text,
                    eurlex_uri = "http://eur-lex.europa.eu/legal-content/" + a.Language + "/TXT/?uri=CELEX:" + id;
                //if (a.ReferrenceInfo != "")eurlex_uri += "/" + a.ReferrenceInfo;
                var o = cd.Hyperlinks.Add(
                    Anchor: rng,
                    Address: eurlex_uri,
#if with_LinkHint
                    ScreenTip: plain,
#endif//with_LinkHint
 SubAddress: a.ReferrenceInfo
                    //,TextToDisplay: plain
                    );
#if with_LinkCache
                LinkCache.Add(o, new rUrl(a, a.Language));
#endif//with_LinkCache
                rng.Font.ColorIndex = Word.WdColorIndex.wdBlue;//wdGreen;
            }
        l_exit:
            Application.System.Cursor = Word.WdCursorType.wdCursorNormal;
            if (b) MessageBox.Show(Resources.Resource.cSuccess, Resources.Resource.cInformation);
#else //with_LinkHint
#if with_LinkCache
            LinkCache.Clear();
#endif//with_LinkCache
            if (bUseHtml)
            {/*+*/
                Application.System.Cursor = Word.WdCursorType.wdCursorWait;
                SetUtf8();
                //ShowBusyTM();
                //RemoveLinksAndTerms();
#if with_async
                try { ShowBusyTM(); }
                catch { }
                htmlPcsSuccess = false;
                htmlPcsResOut = "";
                htmlPcsResIn = sHtml();
                try { HideBusyTM(); }
                catch { }
                //GetProcessedHtml 
                tsk = new GetProcessedHtml(tskGetProcessedHtml);
                cbtsk = new AsyncCallback(cbGetProcessedHtml);
                tsk.BeginInvoke(cbtsk, tsk);
                //tsk.BeginInvoke(new AsyncCallback(cbGetProcessedHtml), tsk);
                goto l_exit1;
#else//with_async
                string output = "";
                success = PutLinksAndTermsHtml(sHtml(), ref output);
                if (!success) goto l_exit;
                string fn = System.IO.Directory.GetCurrentDirectory() + fn_HtmlRes;// @"c:\result-html.html";
                object ofn = fn;
                WordApp.Documents.Open(ofn);
                WordApp.ActiveWindow.Application.WindowBeforeRightClick += new Word.ApplicationEvents4_WindowBeforeRightClickEventHandler(application_WindowBeforeRightClick);
                goto l_exit;
#endif//with_async
                //Application.System.Cursor = Word.WdCursorType.wdCursorNormal;
                //return true;
                //Word.Document ddd = WordApp.Documents.Open(ofn);
                //ddd.ActiveWindow.Application.WindowBeforeRightClick -= MyHandler;
                //ddd.ActiveWindow.Application.WindowBeforeRightClick += MyHandler;                       
                //new Word.ApplicationEvents4_WindowBeforeRightClickEventHandler(application_WindowBeforeRightClick);
            }/*+*/
            Application.System.Cursor = Word.WdCursorType.wdCursorWait;
            RemoveLinksAndTerms();
            string txt_cl = CurrentDoc.Content.Text;//Get The whole document text
            //CurrentDoc.ActiveWindow.Selection.WholeStory();
            //CurrentDoc.SelectAllEditableRanges();
            //WordApp.Selection.Text = txt_cl;
            CurrentDoc.Content.Text = txt_cl;
            //ServiceReference.LinkingServiceClient cl = new ServiceReference.LinkingServiceClient();
            //EUCases.EULinksCheckerWordAddIn.ServiceReference.TextPosWrapper res_cl = cl.PutLinksText(txt_cl);
            Word.Document cd = CurrentDoc;
            cd.WebOptions.Encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;
            JsonResp jsonResult = this.PostRequestPlainText(txt_cl);
            foreach (Entity a in jsonResult.results.entity)
            {
                int pos = a.begin,
                    len = a.len;
                string id = a.value;
#if with_LinkHint//20150616
                string doc_hint = GetDocHint(a.language, id, a.toPar),
                    plain = StripHtml(doc_hint);
#endif//with_LinkHint
                //                ShowStrInBrowser(doc_hint);
                //Word.Document cd = CurrentDoc;
                Word.Range rng = cd.Range(pos, pos + len);
                string rngTxt = rng.Text,
                    eurlex_uri = "http://eur-lex.europa.eu/legal-content/" + a.language + "/TXT/?uri=CELEX:" + id;
                //if (a.ReferrenceInfo != "")eurlex_uri += "/" + a.ReferrenceInfo;
                var o = cd.Hyperlinks.Add(
                    Anchor: rng,
                    Address: eurlex_uri,
#if with_LinkHint
                    ScreenTip: plain,
#endif//with_LinkHint
 SubAddress: a.toPar
                    //,TextToDisplay: plain
                    );
#if with_LinkCache
                LinkCache.Add(o, new rUrl(a, a.Language));
#endif//with_LinkCache
                Word.WdColorIndex ci;
                ci = a.label == "reference" ? Word.WdColorIndex.wdGreen : Word.WdColorIndex.wdRed;
                rng.Font.ColorIndex = ci;
            }
        l_exit:
            HideBusyTM();
            Application.System.Cursor = Word.WdCursorType.wdCursorNormal;
            SetPrintView();
            string msg = success ? Resources.Resource.cSuccess : Resources.Resource.cErrorOccured;
            if (b) MessageBox.Show(msg, Resources.Resource.cInformation);
#endif//with_LinkHint
#if with_async
        l_exit1:
#endif//with_async
            return true;
#if comm_v1
            string txt1 = CurrentDoc.Content.Text;//Get The whole document text
            MessageBox.Show(txt1);
            EuLinksResponse e = PostRequestPlainText(txt1);
#else
            #region _Old1
            //Word._Document od = Application.ActiveDocument as Word._Document;
            //object start = 0;
            //object end = 0;
            //Application.System.Cursor = Word.WdCursorType.wdCursorWait;
            //try
            //{
            //    FrontendWS.LinksFuncs lf = new FrontendWS.LinksFuncs();
            //    string theText = CurrentDoc.Content.Text;
            //    object missing = System.Reflection.Missing.Value;
            //    Word.Document doc = Application.Documents.Add(ref missing, ref missing, ref missing, ref missing);
            //    doc.Activate();
            //    doc.Content.Text = theText;
            //    theText = doc.Content.Text;
            //    //return true;
            //    byte[] compressedArray = EucasesLinkingService.Tools.Compression.Compress(System.Text.Encoding.UTF8.GetBytes(theText));
            //    var lLinks = lf.PutLinks(compressedArray);
            //    Word.Range rng;
            //    foreach (var l in lLinks)
            //    {
            //        int pos;
            //        int len;
            //        if (l is EUCases.EULinksCheckerWordAddIn.FrontendWS.EurovocTextPos)
            //        {
            //            pos = l.Pos;
            //            len = l.Length;
            //            string id = (l as EUCases.EULinksCheckerWordAddIn.FrontendWS.EurovocTextPos).Id;
            //            rng = doc.Range(pos, pos + len);
            //            string txt = System.Web.HttpUtility.UrlEncode(rng.Text);
            //            doc.Hyperlinks.Add(
            //                Anchor: rng,
            //                Address: "http://eur-lex.europa.eu/search.html?APIS=1&instInvStatus=ALL&DTC=false&DTS_DOM=ALL&orEUROVOC=DC_DECODED%3D%22" + txt + "%22,DC_ALTLABEL%3D%22" + txt + "%22&type=advanced&lang=" + l.Language.ToLower() + "&SUBDOM_INIT=ALL_ALL&DTS_SUBDOM=ALL_ALL",
            //                ScreenTip: rng.Text
            //                );
            //            rng.Font.ColorIndex = Word.WdColorIndex.wdGreen;
            //            //WordApp.HelpTool();
            //            ////////
            //            //object txt_cmnt=rng.Text;
            //            //doc.Comments.Add(rng,ref txt_cmnt);
            //            ////////
            //        }
            //        else
            //            if (l is EUCases.EULinksCheckerWordAddIn.FrontendWS.LinkTextPos)
            //            {
            //                pos = l.Pos;
            //                len = l.Length;
            //                string celex = (l as EUCases.EULinksCheckerWordAddIn.FrontendWS.LinkTextPos).Celex;
            //                string referrenceInfo = (l as EUCases.EULinksCheckerWordAddIn.FrontendWS.LinkTextPos).ReferrenceInfo;
            //                rng = doc.Range(pos, pos + len);
            //                doc.Hyperlinks.Add(
            //                    Anchor: rng,
            //                    Address: "http://eur-lex.europa.eu/legal-content/" + l.Language + "/TXT/?APIS=1&uri=CELEX%3A" + celex + (referrenceInfo == null ? "" : "&ref=" + referrenceInfo),
            //                    ScreenTip: rng.Text
            //                    );
            //                rng.Font.ColorIndex = Word.WdColorIndex.wdBlue;
            //            }
            //    }
            //    WordApp.Selection.Start = 0;
            //    WordApp.Selection.End = 0;
            //}
            //catch (Exception exc)
            //{
            //    //MessageBox.Show(exc.InnerException.Message);
            //    MessageBox.Show(exc.Message);
            //    MessageBox.Show(exc.StackTrace);
            //}
            //finally
            //{
            //    Application.System.Cursor = Word.WdCursorType.wdCursorNormal;
            //    od.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
            //}
            #endregion
#endif
            return true;
        }
        public bool PutLinksAndTermsHtml(string html, ref string o)
        {
            Application.System.Cursor = Word.WdCursorType.wdCursorWait;
            SetUtf8();
#if with_SvcRef
            o = html;
            //            S2F(@"c:\orig-html.html", o);
            ServiceReference.LinkingServiceClient cl = new ServiceReference.LinkingServiceClient();
            ServiceReference.TextPosWrapper res_cl = cl.PutLinksText(html);
            foreach (ServiceReference.LinkTextPos a in res_cl.LinksTextPos)
            {
                int pos = a.Pos,
                    len = a.Length,
                    pl = pos + len;
#if with_LinkHint
                string id = a.Celex,
                    doc_hint = GetDocHint(a.Language, a.Celex, a.ReferrenceInfo),
                    plain = StripHtml(doc_hint),//20150616
                    plainNQ = plain.Replace("\"", ""),//20150616
                    eurlex_uri = "http://eur-lex.europa.eu/legal-content/" + a.Language + "/TXT/?uri=CELEX:" + id;
                if (a.ReferrenceInfo != "") eurlex_uri += "#" + a.ReferrenceInfo;//"#"?
                //string first = o.Substring(pos), c = o.Substring(pos, len), rest = o.Remove(pl);//o.Substring(pl, o.Length - pl);
                string first = o.Remove(pos), c = o.Substring(pos, len), rest = o.Substring(pl, o.Length - pl);
                o = first + "<a href=\"" + eurlex_uri + "\" title=\"" + plainNQ + "\">" + c + "</a>" + rest;
#else//with_LinkHint
                string id = a.Celex,
                    eurlex_uri = "http://eur-lex.europa.eu/legal-content/" + a.Language + "/TXT/?uri=CELEX:" + id;
                if (a.ReferrenceInfo != "") eurlex_uri += "#" + a.ReferrenceInfo;//"#"?
                //string first = o.Substring(pos), c = o.Substring(pos, len), rest = o.Remove(pl);//o.Substring(pl, o.Length - pl);
                string first = o.Remove(pos), c = o.Substring(pos, len), rest = o.Substring(pl, o.Length - pl);
                o = first + "<a href=\"" + eurlex_uri + "\">" + c + "</a>" + rest;
#endif//with_LinkHint
                /*
                Word.Document doc = CurrentDoc;
                Word.Range rng = doc.Range(pos, pos + len);
                string rngTxt = rng.Text,
                    eurlex_uri = "http://eur-lex.europa.eu/legal-content/" + a.Language + "/TXT/?uri=CELEX:" + id;
                //if (a.ReferrenceInfo != "")eurlex_uri += "/" + a.ReferrenceInfo;
                var o = doc.Hyperlinks.Add(
                    Anchor: rng,
                    Address: eurlex_uri,
                    ScreenTip: plain,
                    SubAddress: a.ReferrenceInfo
                    //,TextToDisplay: plain
                    );
                rng.Font.ColorIndex = Word.WdColorIndex.wdBlue;//wdGreen;
                */
            }
            string f = System.IO.Directory.GetCurrentDirectory() + fn_HtmlRes;
            S2F(f, o);//S2F(@"c:\result-html.html", o);
            return true;
#else//with_SvcRef
            bool success = false;
            o = OutpHtml(html, ref success);// ProcessV3(html);
            /*20150618>>
                        o = html;
            ///20150618>>
            ///<<20150618
                        JsonResp jsonResult = this.PostRequestPlainText(o);
                        foreach (Entity a in jsonResult.results.entity)
                        {
                            int pos = a.begin,
                                len = a.len,
                                pl = pos + len;
#if with_LinkHint
                            string id = a.value,
                                doc_hint = GetDocHint(a.language, a.value, a.toPar),
                                plain = StripHtml(doc_hint),//20150616
                                plainNQ = plain.Replace("\"", ""),//20150616
                                eurlex_uri = "http://eur-lex.europa.eu/legal-content/" + a.language + "/TXT/?uri=CELEX:" + id;
                            if (a.toPar != "") eurlex_uri += "#" + a.toPar;//"#"?
                            //string first = o.Substring(pos), c = o.Substring(pos, len), rest = o.Remove(pl);//o.Substring(pl, o.Length - pl);
                            string first = o.Remove(pos), c = o.Substring(pos, len), rest = o.Substring(pl, o.Length - pl);
                            o = first + "<a href=\"" + eurlex_uri + "\" title=\"" + plainNQ + "\">" + c + "</a>" + rest;
#else//with_LinkHint
                            string id = a.value,
                                eurlex_uri = "http://eur-lex.europa.eu/legal-content/" + a.language + "/TXT/?uri=CELEX:" + id;
                            if (a.toPar != "") eurlex_uri += "#" + a.toPar;//"#"?
                            string first = o.Remove(pos), c = o.Substring(pos, len), rest = o.Substring(pl, o.Length - pl);
                            string clr = a.label == "reference" ? "green" : "red";
                            o = first + "<a href=\"" + eurlex_uri + "\" style=\"color:" + clr + "\">" + c + "</a>" + rest;
#endif//with_LinkHint
                        }
            <<20150618*/
            string f = System.IO.Directory.GetCurrentDirectory() + fn_HtmlRes;
            S2F(f, o);//S2F(@"c:\result-html.html", o);
            return success;
#endif//with_SvcRef
        }