protected WordRange GetStartingRange(Word.Range aRunRange)
        {
            // the search range might have a different start value than the paragraphs we're going to acquire
            if (m_paraSearch == null)
            {
                m_paraSearch = aRunRange.Paragraphs.First;
                System.Diagnostics.Debug.Assert(m_paraSearch != null);
            }

            Word.Range aParagraphRange = m_paraSearch.Range;
            System.Diagnostics.Debug.Assert(SearchAreaStart <= aParagraphRange.End);

            // if the end of the paragraph is exactly at the beginning of the search offset, then skip to
            //  the next paragraph (this could be the case if we were searching for "\r" on a previous 'Find')
            if (SearchAreaStart == aParagraphRange.End)
            {
                m_paraSearch = m_paraSearch.Next(ref offset1);
                if (m_paraSearch == null)
                {
                    return(null); // done
                }
                aParagraphRange = m_paraSearch.Range;
            }
            else if (SearchAreaStart > aParagraphRange.Start)
            {
                aParagraphRange.Start = SearchAreaStart;
            }

            return(new WordRange(aParagraphRange));
        }
        private static string GetHeadlineDescription(Word.Paragraph para)
        {
            if (IsHeadlineParagraph(para) && IsHeadlineDescriptionParagraph(para.Next()))
            {
                return(CleanWordRangeText(para.Next().Range.Sentences.First.Text));
            }

            return(string.Empty);
        }
        private static bool IsHeadlineParagraph(Word.Paragraph para)
        {
            object nextCount      = 1;
            var    nextPara       = para.Next(ref nextCount);
            object followingCount = 2;
            var    followingPara  = para.Next(ref followingCount);

            if (nextPara == null)
            {
                return(true);
            }
            if (followingPara != null)
            {
                return((nextPara.PageBreakBefore == TrueValue) ||
                       ((followingPara.PageBreakBefore == TrueValue) && (IsHeadlineDescriptionParagraph(nextPara))));
            }
            else
            {
                return((nextPara.PageBreakBefore == TrueValue) ||
                       (IsHeadlineDescriptionParagraph(nextPara)));
            }
        }
        private static bool IsPageEndParagraph(Word.Paragraph para)
        {
            var nextPara = para.Next();

            if (nextPara == null)
            {
                return(true);
            }
            else
            {
                return(IsPageStartParagraph(nextPara));
            }
        }
Exemple #5
0
        /// <summary>
        /// Returns the starting index of the next paragraph
        /// </summary>
        /// <returns>starting index, -1 on error</returns>
        private int getStartOfNextPara()
        {
            if (WordApp == null)
            {
                return(-1);
            }

            Word.Paragraph para         = WordApp.Selection.Range.Paragraphs[1];
            int            start        = para.Range.Start;
            int            prevStart    = start;
            bool           flag         = false;
            int            iteration    = 0;
            bool           breakOnFirst = false;

            while (true)
            {
                Word.Paragraph next = para.Next();
                if (next == null)
                {
                    break;
                }

                start = next.Range.Start;

                Log.Debug("prevStart: " + prevStart + ", start: " + start);

                if (start - prevStart == 1)
                {
                    if (iteration == 0)
                    {
                        breakOnFirst = true;
                    }
                }
                else if (start - prevStart > 1)
                {
                    if (breakOnFirst || flag)
                    {
                        return(prevStart);
                    }

                    flag = true;
                }

                para      = next;
                prevStart = start;
                iteration++;
            }

            return(-1);
        }
Exemple #6
0
 private bool MatchPlainText()
 {
     if (workingDoc.Content.Text == modelDoc.Content.Text)
     {
         return(true);
     }
     Microsoft.Office.Interop.Word.Paragraph p = workingDoc.Paragraphs.First,
                                             q = modelDoc.Paragraphs.First;
     while (p != null && q != null)
     {
         Range[] r = p.Range.Characters.Cast <Range>().ToArray(),
         s = q.Range.Characters.Cast <Range>().ToArray();
         int i, j;
         for (i = 0, j = 0; i < r.Length && j < s.Length; ++i, ++j)
         {
             if (r[i].Text != s[i].Text)
             {
                 r[i].Select();
                 s[i].Select();
                 return(false);
             }
         }
         if (i < r.Length)
         {
             r[i].Select();
             return(false);
         }
         if (j < s.Length)
         {
             s[j].Select();
             return(false);
         }
         p = p.Next();
         q = q.Next();
     }
     if (p != null)
     {
         p.Range.Select();
         return(false);
     }
     if (q != null)
     {
         q.Range.Select();
         return(false);
     }
     return(true);
 }
Exemple #7
0
 //MatchPlainText already checked 2 documents have the same text
 private bool MatchAlignment()
 {
     Microsoft.Office.Interop.Word.Paragraph p = workingDoc.Paragraphs.First,
                                             q = modelDoc.Paragraphs.First;
     while (p != null)
     {
         if (p.Alignment != q.Alignment)
         {
             p.Range.Select();
             q.Range.Select();
             return(false);
         }
         p = p.Next();
         q = q.Next();
     }
     return(true);
 }
Exemple #8
0
 public void updateDoc(Dictionary <string, Object> keyValuePairs)
 {
     Word.Application wordApp = new Word.Application();
     Word.Document    doc     = wordApp.Documents.Open(documentPath, ReadOnly: false);
     try {
         doc.Activate();
         if (((string)(keyValuePairs["{id:kind}"])).Contains("Тренировк") || ((string)(keyValuePairs["{id:kind}"])).Contains("Самостояте") || ((string)(keyValuePairs["{id:kind}"])).Contains("Практическо"))
         {
             int countQuestions = 0;
             for (int e = 1; e <= doc.Paragraphs.Count; e++)
             {
                 if (doc.Paragraphs[e].Range.Text.Contains("{id:adjunct}"))
                 {
                     Dictionary <string, string> questions = (Dictionary <string, string>)(keyValuePairs["{id:questions}"]);
                     FindAndReplace(wordApp, "{id:adjunct}", "ПРИЛОЖЕНИЯ");
                     countQuestions = questions.Count;
                     Word.Paragraph p = doc.Paragraphs[e];
                     p.Range.InsertParagraphAfter();
                     p = p.Next();
                     for (int i = 0; i < questions.Count; i++)
                     {
                         p.Range.Text = (i + 1) + ". Карточка - задание на изучение " + (i + 1) + "-го вопроса.";
                         p.Range.InsertParagraphAfter();
                         p = p.Next();
                     }
                 }
                 int q           = 1;
                 int countTables = doc.Tables.Count;
                 if (doc.Paragraphs[e].Range.Text.Contains("{id:file"))
                 {
                     Word.Paragraph p = doc.Paragraphs[e];
                     p.Range.InsertParagraphAfter();
                     p.Next();
                     Dictionary <string, string> questions = (Dictionary <string, string>)(keyValuePairs["{id:questions}"]);
                     int temp = Int32.Parse(doc.Paragraphs[e].Range.Text.Trim().Replace("\r", "").Replace("\a", "").Replace("\n", "").Substring(8, 1));
                     KeyValuePair <string, string> question;
                     try
                     {
                         question = questions.ElementAt(temp - 1);
                     }
                     catch (Exception exception)
                     {
                         break;
                     }
                     p.Range.InsertFile(Path.GetFullPath(Path.Combine(System.Reflection.Assembly.GetExecutingAssembly().Location, @"../../../../../Resources/AdditionalDocs/Spravochnik.docx")));
                     int z = 0;
                     for (int i = 0; i < question.Key.Length; i += 30)
                     {
                         if (i > 0)
                         {
                             try
                             {
                                 FindAndReplace(wordApp, "{id:questionName}", question.Key.Substring(i + 1, 30) + "{id:questionName}");
                             }
                             catch (Exception qq)
                             {
                                 break;
                             }
                         }
                         else
                         {
                             FindAndReplace(wordApp, "{id:questionName}", question.Key.Substring(i, 30) + "{id:questionName}");
                         }
                         z = i;
                     }
                     z += 30;
                     FindAndReplace(wordApp, "{id:questionName}", question.Key.Substring(z + 1));
                     FindAndReplace(wordApp, "{id:countAjunct}", temp);
                     FindAndReplace(wordApp, "{id:questionDuration}", question.Value.Split(separator)[0]);
                 }
                 int afterInsert = doc.Tables.Count;
             }
         }
         if (((string)(keyValuePairs["{id:kind}"])).ToLower().Contains("полувз") || ((string)(keyValuePairs["{id:kind}"])).ToLower().Contains("ПРО 3 ЧЕЛОВЕК СПРОСИТЬ КАК НАЗЫВАЕТСЯ"))
         {
             foreach (Word.Paragraph paragraph in doc.Paragraphs)
             {
                 if (paragraph.Range.Text.Contains("{id:image}"))
                 {
                     if (((string)(keyValuePairs["{id:kind}"])).ToLower().Contains("ПРО 3 ЧЕЛОВЕК СПРОСИТЬ КАК НАЗЫВАЕТСЯ"))
                     {
                         paragraph.Range.InsertFile(Path.GetFullPath(Path.Combine(System.Reflection.Assembly.GetExecutingAssembly().Location, @"../../../../../Resources/AdditionalDocs/image2.docx")));
                     }
                     else
                     {
                         paragraph.Range.InsertFile(Path.GetFullPath(Path.Combine(System.Reflection.Assembly.GetExecutingAssembly().Location, @"../../../../../Resources/AdditionalDocs/image1.docx")));
                     }
                 }
             }
         }
         FindAndReplace(wordApp, "{id:cardOfTask}", "");
         FindAndReplace(wordApp, "{id:file1}", "");
         FindAndReplace(wordApp, "{id:file2}", "");
         FindAndReplace(wordApp, "{id:file3}", "");
         FindAndReplace(wordApp, "{id:file4}", "");
         FindAndReplace(wordApp, "{id:file5}", "");
         FindAndReplace(wordApp, "{id:file6}", "");
         FindAndReplace(wordApp, "{id:image}", "");
         FindAndReplace(wordApp, "{id:name}", keyValuePairs["{id:name}"]);
         FindAndReplace(wordApp, "{id:theme}", keyValuePairs["{id:theme}"]);
         FindAndReplace(wordApp, "{id:themeName}", keyValuePairs["{id:themeName}"]);
         FindAndReplace(wordApp, "{id:lesson}", keyValuePairs["{id:lesson}"]);
         FindAndReplace(wordApp, "{id:lessonName}", keyValuePairs["{id:lessonName}"]);
         FindAndReplace(wordApp, "{id:kind}", keyValuePairs["{id:kind}"]);
         FindAndReplace(wordApp, "{id:method}", keyValuePairs["{id:method}"]);
         FindAndReplace(wordApp, "{id:duration}", keyValuePairs["{id:duration}"]);
         FindAndReplace(wordApp, "{id:place}", keyValuePairs["{id:place}"]);
         string methodical = (string)keyValuePairs["{id:methodical}"];
         int    k          = 0;
         for (int i = 0; i < methodical.Length; i += 30)
         {
             if (i > 0)
             {
                 try
                 {
                     FindAndReplace(wordApp, "{id:methodical}", methodical.Substring(i + 1, 30) + "{id:methodical}");
                 }
                 catch (Exception e)
                 {
                     break;
                 }
             }
             else
             {
                 FindAndReplace(wordApp, "{id:methodical}", methodical.Substring(i, 30) + "{id:methodical}");
             }
             k = i;
         }
         k += 30;
         FindAndReplace(wordApp, "{id:methodical}", methodical.Substring(k + 1));
         string literature = (string)keyValuePairs["{id:literature}"];
         int    l          = 0;
         for (int i = 0; i < literature.Length; i += 30)
         {
             if (i > 0)
             {
                 try
                 {
                     FindAndReplace(wordApp, "{id:literature}", literature.Substring(i + 1, 30) + "{id:literature}");
                 }
                 catch (Exception e)
                 {
                     break;
                 }
             }
             else
             {
                 FindAndReplace(wordApp, "{id:literature}", literature.Substring(i, 30) + "{id:literature}");
             }
             l = i;
         }
         l += 30;
         FindAndReplace(wordApp, "{id:literature}", literature.Substring(l + 1));
         FindAndReplace(wordApp, "{id:technicalMeans}", keyValuePairs["{id:technicalMeans}"]);
         string introTime = (keyValuePairs["{id:intro}"]).ToString().Split(separator)[0];
         FindAndReplace(wordApp, "{id:intro}", introTime);
         string[] introQuestions = (keyValuePairs["{id:intro}"]).ToString().Split(separator)[1].Split(';');
         for (int e = 0; e < introQuestions.Length - 1; e++)
         {
             FindAndReplace(wordApp, "{id:introQuestions}", introQuestions[e] + ";\r\n{id:introQuestions}");
         }
         FindAndReplace(wordApp, "{id:introQuestions}", "");
         FindAndReplace(wordApp, "{id:material}", keyValuePairs["{id:material}"]);
         FindAndReplace(wordApp, "{id:educationalQuestions}", keyValuePairs["{id:educationalQuestions}"]);
         string conclusionTime = (keyValuePairs["{id:conclution}"]).ToString().Split(separator)[0];
         FindAndReplace(wordApp, "{id:conclution}", conclusionTime);
         string[] conclusionQuestions = (keyValuePairs["{id:conclution}"]).ToString().Split(separator)[1].Split(';');
         for (int e = 0; e < conclusionQuestions.Length - 1; e++)
         {
             FindAndReplace(wordApp, "{id:conclutionsQuestions}", conclusionQuestions[e] + ";\r\n{id:conclutionsQuestions}");
         }
         FindAndReplace(wordApp, "{id:conclutionsQuestions}", "");
         foreach (Word.Table table in doc.Tables)
         {
             Word.Range rangeTable = table.Range;
             foreach (Word.Row row in rangeTable.Rows)
             {
                 foreach (Word.Cell cell in row.Cells)
                 {
                     Word.Range range = cell.Range;
                     if (range.Text.Contains("{id:questions}"))
                     {
                         FindAndReplace(wordApp, "{id:questions}", "");
                         Word.Row temporary = row;
                         int      count     = 1;
                         foreach (KeyValuePair <string, string> question in (Dictionary <string, string>)keyValuePairs["{id:questions}"])
                         {
                             Object   oMissing = System.Reflection.Missing.Value;
                             Word.Row newRow   = table.Rows.Add(ref oMissing);
                             newRow.Cells[1].Range.Text = "2." + count;
                             newRow.Cells[2].Range.Text = question.Key;
                             newRow.Cells[3].Range.Text = question.Value.Split(separator)[0];
                             Regex  regex        = new Regex("^ ?[1-9].*$");
                             string questionFull = "";
                             if (regex.IsMatch(question.Key))
                             {
                                 questionFull = "Учебный вопрос. " + question.Key + " " + question.Value.Split(separator)[0] + ".\r\n";
                             }
                             else
                             {
                                 questionFull = "Учебный вопрос " + count + ". " + question.Key + " " + question.Value.Split(separator)[0] + ".\r\n";
                             }
                             int r = 0;
                             for (int e = 0; e < questionFull.Length; e += 30)
                             {
                                 if (e > 0)
                                 {
                                     try
                                     {
                                         FindAndReplace(wordApp, "{id:questionOfLesson}", questionFull.Substring(e, 30) + "{id:questionOfLesson}");
                                     }
                                     catch (Exception q)
                                     {
                                         break;
                                     }
                                 }
                                 else
                                 {
                                     FindAndReplace(wordApp, "{id:questionOfLesson}", questionFull.Substring(e, 30) + "{id:questionOfLesson}");
                                 }
                                 r = e;
                             }
                             r += 30;
                             FindAndReplace(wordApp, "{id:questionOfLesson}", questionFull.Substring(r) + "\r\n{id:contentOfQuestion}");
                             int    w    = 0;
                             string temp = question.Value.Split(separator)[1];
                             if (temp.Length > 30)
                             {
                                 for (int z = 0; z < temp.Length; z += 30)
                                 {
                                     if (z > 0)
                                     {
                                         try
                                         {
                                             FindAndReplace(wordApp, "{id:contentOfQuestion}", temp.Substring(z, 30) + "{id:contentOfQuestion}");
                                         }
                                         catch (Exception q)
                                         {
                                             break;
                                         }
                                     }
                                     else
                                     {
                                         FindAndReplace(wordApp, "{id:contentOfQuestion}", temp.Substring(z, 30) + "{id:contentOfQuestion}");
                                     }
                                     w = z;
                                 }
                                 FindAndReplace(wordApp, "{id:contentOfQuestion}", temp.Substring(w) + "\r\n{id:questionOfLesson}\r\n");
                                 temporary = newRow;
                                 count++;
                             }
                             else
                             {
                                 FindAndReplace(wordApp, "{id:contentOfQuestion}", temp.Substring(w) + "\r\n{id:questionOfLesson}\r\n");
                                 count++;
                             }
                         }
                         Object   missing   = System.Reflection.Missing.Value;
                         Word.Row newRowENd = table.Rows.Add(ref missing);
                         newRowENd.Cells[1].Range.Text = "3";
                         newRowENd.Cells[2].Range.Text = "Заключение";
                         newRowENd.Cells[3].Range.Text = (string)keyValuePairs["{id:conclution}"].ToString().Split(separator)[0] + " мин";
                     }
                     else if (range.Text.Contains("{id:goal}"))
                     {
                         FindAndReplace(wordApp, "{id:goal}", "");
                         int count = 1;
                         foreach (string goal in (List <string>)keyValuePairs["{id:goal}"])
                         {
                             Object   oMissing = System.Reflection.Missing.Value;
                             Word.Row newRow   = table.Rows.Add(ref oMissing);
                             newRow.Cells[1].Range.Text = count + "";
                             newRow.Cells[2].Range.Text = goal;
                             count++;
                         }
                     }
                 }
             }
         }
         FindAndReplace(wordApp, "{id:adjunct}", "");
         FindAndReplace(wordApp, "{id:questionOfLesson}", "");
         WordAPI.SaveFile(doc);
         WordAPI.Close(doc);
         updatePlan(keyValuePairs);
     }
     catch (Exception e)
     {
         doc.Close();
         new ExceptionWindow()
         .Show();
     }
 }