/// <summary>
        /// Use paragraph sign for finding paragraphs. Instead of doc.Paragraphs. Much faster in large documents.
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>
        private List <SwParagraph> GetParagraphs(Document doc, IswItem currentItem, ThreadedWindowWrapper wrap)
        {
            List <SwParagraph> retParagraphs = new List <SwParagraph>();
            Range range       = doc.Content;
            int   totalLength = range.End;
            Range temprange;
            Find  find = range.Find;

            find.Text = "^p";
            find.ClearFormatting();
            object missing = Type.Missing;

            try
            {
                find.Execute(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);
                int start = 0;
                while (range.Find.Found)
                {
                    if (range.Start < start)
                    {
                        break;
                    }
                    temprange = doc.Range(start, range.End);
                    retParagraphs.Add(new SwParagraph(temprange, currentItem));
                    wrap.SetProgress((range.End * 100 / totalLength).ToString());
                    start = range.End;// -1;
                    range.Find.Execute(
                        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);
                }
            }
            catch (DllNotFoundException)
            {
                throw;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Exception: " + ex.Message + "  " + range.Start.ToString());
                throw;
            }
            return(retParagraphs);
        }
예제 #2
0
        public Import(IswItem topItem, List <SwParagraph> paragraphs, List <SwStyle> styles, List <SwStyle> description, string hostName, ThreadedWindowWrapper wrap)
        {
            LogToDescription("Importing from Office started at " + Convert.ToString(DateTime.Now));
            var             currentItem = topItem;
            List <WordItem> wordItems   = CreateWordItems(paragraphs, description);

            double progress = 0;
            double step     = (double)100 / (double)wordItems.Count;

            wrap.SetProgress(Convert.ToInt32(progress).ToString()); progress += step;

            int             handledItems        = 0;
            WdOutlineLevel  currentOutlineLevel = WdOutlineLevel.wdOutlineLevelBodyText;
            WdOutlineLevel  lastOutlineLevel    = 0;
            Stack <IswItem> parentItems         = new Stack <IswItem>();

            parentItems.Push(topItem);
            foreach (WordItem wordItem in wordItems)
            {
                if (wordItem.MainParagraph == null)
                {
                    topItem.Description = SWDescription.MakeDescription(SWUtility.RtfToRvfz(ConcatenateParagraphsRtf(wordItem.DescriptionParagraphs)));
                    continue;
                }
                currentOutlineLevel = wordItem.MainParagraph.OutlineLevel;

                if (currentOutlineLevel > lastOutlineLevel) // Sublevel -> Add to the stack
                {
                    currentItem = WriteItem(parentItems.Peek(), wordItem);
                    parentItems.Push(currentItem);
                }

                else // Higher level -> Remove items from the stack
                {
                    for (WdOutlineLevel i = currentOutlineLevel; (i <= lastOutlineLevel && parentItems.Count > 1); i++)
                    {
                        parentItems.Pop();
                    }
                    currentItem = WriteItem(parentItems.Peek(), wordItem);
                    parentItems.Push(currentItem);
                }

                lastOutlineLevel = currentOutlineLevel;

                wrap.SetProgress(Convert.ToInt32(progress).ToString()); progress += step;
                wrap.SetStatus(string.Format("Imported {0} paragraphs of {1}", ++handledItems, wordItems.Count));
            }
            LogToDescription("Script terminated gracefully at " + Convert.ToString(DateTime.Now));
            WriteLogToTopItem(topItem);
        }
        public ReadWord(string sFileName, IswItem currentItem, ThreadedWindowWrapper wrap, out bool failedWithLoadDLL)
        {
            failedWithLoadDLL = false;
            Application word     = new Application();
            Document    doc      = null;
            int         progress = 0;

            object fileName = sFileName;
            // Define an object to pass to the API for missing parameters
            object missing  = System.Type.Missing;
            object readOnly = true;

            try
            {
                doc = word.Documents.Open(ref fileName,
                                          ref missing, ref readOnly, 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);

                String             read = string.Empty;
                List <SwParagraph> data = new List <SwParagraph>();
                data       = GetParagraphs(doc, currentItem, wrap);
                Paragraphs = data;

                List <SwStyle> styles = new List <SwStyle>();
                foreach (var p in Paragraphs)
                {
                    var name = p.Style.Name;
                    foreach (var s in styles)
                    {
                        if (s.Name.Equals(p.Style.Name))
                        {
                            name = "";
                        }
                    }
                    if (name.Length > 0)
                    {
                        styles.Add(new SwStyle(name, (int)p.OutlineLevel));
                    }
                }
                StylesInUse = styles;
                //List<SwStyle> styles = new List<SwStyle>();
                //foreach (var p in Paragraphs)
                //{
                //    var name = p.Style.Name;
                //    foreach (var s in styles)
                //    {
                //        if (s.Name.Equals(p.Style.Name))
                //            name = "";
                //    }
                //    if (name.Length > 0)
                //        styles.Add(new SwStyle(name));
                //}
                //StylesInUse = styles;
            }
            catch (DllNotFoundException)
            {
                failedWithLoadDLL = true;
                return;
            }
            catch (Exception ex)
            {
                if (doc != null)
                {
                    ((_Document)doc).Close();
                }
                ((_Application)word).Quit();
                System.Diagnostics.Debug.WriteLine(ex.Message);
                throw;
            }
            ((_Document)doc).Close();
            ((_Application)word).Quit();
            wrap.SetProgress(progress.ToString()); progress += 20;
            wrap.SetProgress(progress.ToString()); progress += 20;
        }