public void ConvertToTable(string strText, int row_cnt = -1, int col_cnt = -1, bool bSetBorders = true, string bookmark = "") { string strReturn = "" + (char)10; bool bUseReturn = false; if (strText.IndexOf(strReturn) >= 0) { bUseReturn = true; } strText = strText.Replace(strReturn, "#enter#"); if (bookmark == "") { if (m_pDoc == null) { m_pDoc = m_pApp.ActiveDocument; } if (m_pDoc == null) { return; } m_pDoc.Select(); m_pSelection = m_pApp.Selection; if (m_pSelection == null) { return; } m_pSelection.Collapse(0); object varInfo = m_pSelection.get_Information(MSWord.WdInformation.wdWithInTable); if (Convert.ToBoolean(varInfo) == true) { m_pDoc.Select(); m_pSelection = m_pApp.Selection; if (m_pSelection == null) { return; } m_pSelection.Collapse(0); } } else { m_pSelection.GoTo(-1, Type.Missing, Type.Missing, bookmark); }; m_pSelection.Font.Bold = 0; m_pSelection.Text = strText; m_pSelection.Paragraphs.SpaceAfter = 0; m_pSelection.Paragraphs.SpaceBefore = 0; m_pSelection.Paragraphs.LineSpacingRule = MSWord.WdLineSpacing.wdLineSpaceSingle; if (row_cnt < 0 && col_cnt < 0) { m_pTable = m_pSelection.ConvertToTable(1, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 0, 0); } else { m_pTable = m_pSelection.ConvertToTable(1, row_cnt, col_cnt, 0, 0, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 0, 0); } // выравниваем талицу (не содержимое!!!) if (m_pTable == null) { return; } MSWord.Rows pRows = m_pSelection.Rows; pRows.AllowBreakAcrossPages = 0; m_pSelection.Collapse(0); MSWord.Borders pBorders = m_pTable.Borders; if (pBorders == null) { return; } if (bSetBorders) { pBorders.Enable = 1; } else { pBorders.Enable = 0; } if (bUseReturn) { m_pTable.Select(); MSWord.Selection sel = m_pApp.Selection; MSWord.Find find = sel.Find; find.ClearFormatting(); MSWord.Replacement replace = find.Replacement; replace.ClearFormatting(); find.Text = "#enter#"; replace.Text = "^p"; find.MatchCase = false; find.MatchWholeWord = false; find.MatchWildcards = false; find.MatchSoundsLike = false; find.MatchAllWordForms = false; find.Wrap = MSWord.WdFindWrap.wdFindContinue; MSWord.WdReplace repl = MSWord.WdReplace.wdReplaceAll; find.Execute(find.Text, find.MatchCase, find.MatchWholeWord, find.MatchWildcards, find.MatchSoundsLike, find.MatchAllWordForms, Type.Missing, find.Wrap, Type.Missing, replace.Text, repl); } }
/// <summary> /// Creates the paragraph formatting of the selected text using the style settings and user preferences. /// </summary> /// <param name="use_frame">determines whether the text is put in a frame</param> /// <param name="use_numbering">determines whether line numbering is used</param> public void ParagraphFormatting(bool use_frame, bool use_numbering) { Word.WdColor frameAlternatingColor_ = (Word.WdColor)ColorTranslator.ToOle(frameAlternatingColor); Word.WdColor frameBgColor_ = (Word.WdColor)ColorTranslator.ToOle(frameBgColor); Word.WdColor frameBorderColor_ = (Word.WdColor)ColorTranslator.ToOle(frameBorderColor); Word.Selection sel = wordApp.Selection; // just to make the code cleaner sel.ParagraphFormat.LineSpacing = 12.0f; sel.ParagraphFormat.SpaceBefore = 0.0f; sel.ParagraphFormat.SpaceAfter = 0.0f; /* If line numbering is used, then the text block must be converted into a table, which can be quiet resource demanding */ if (use_numbering) { object missing = Type.Missing; object dontuse = false; Word.Table t = sel.ConvertToTable(Word.WdTableFieldSeparator.wdSeparateByParagraphs, ref missing, 1, ref missing, ref missing, ref missing, ref dontuse, ref dontuse, ref dontuse, ref dontuse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); t.Columns.Add(t.Columns[1]); t.Columns[1].Width = 35.0f; t.Columns[2].Width = sel.PageSetup.PageWidth - sel.PageSetup.RightMargin - sel.PageSetup.LeftMargin - 35.0f; if (use_frame) { t.Columns[1].Borders[Word.WdBorderType.wdBorderRight].LineStyle = Word.WdLineStyle.wdLineStyleSingle; t.Columns[1].Borders[Word.WdBorderType.wdBorderRight].Color = frameBorderColor_; t.Columns[1].Borders[Word.WdBorderType.wdBorderRight].LineWidth = Word.WdLineWidth.wdLineWidth300pt; t.Columns[1].Shading.BackgroundPatternColor = Word.WdColor.wdColorGray05; int i_ = 0; foreach (Word.Cell c in t.Columns[2].Cells) { if (frameAlternatingLines && (i_ % 2 == 1)) { c.Shading.BackgroundPatternColor = frameAlternatingColor_; } else { c.Shading.BackgroundPatternColor = frameBgColor_; } c.LeftPadding = 7.0f; i_++; } t.Borders[Word.WdBorderType.wdBorderTop].LineStyle = Word.WdLineStyle.wdLineStyleSingle; t.Borders[Word.WdBorderType.wdBorderTop].Color = frameBorderColor_; t.Borders[Word.WdBorderType.wdBorderTop].LineWidth = Word.WdLineWidth.wdLineWidth075pt; t.Borders[Word.WdBorderType.wdBorderLeft].LineStyle = Word.WdLineStyle.wdLineStyleSingle; t.Borders[Word.WdBorderType.wdBorderLeft].Color = frameBorderColor_; t.Borders[Word.WdBorderType.wdBorderLeft].LineWidth = Word.WdLineWidth.wdLineWidth075pt; t.Borders[Word.WdBorderType.wdBorderRight].LineStyle = Word.WdLineStyle.wdLineStyleSingle; t.Borders[Word.WdBorderType.wdBorderRight].Color = frameBorderColor_; t.Borders[Word.WdBorderType.wdBorderRight].LineWidth = Word.WdLineWidth.wdLineWidth075pt; t.Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleSingle; t.Borders[Word.WdBorderType.wdBorderBottom].Color = frameBorderColor_; t.Borders[Word.WdBorderType.wdBorderBottom].LineWidth = Word.WdLineWidth.wdLineWidth075pt; } int i = 0; foreach (Word.Cell c in t.Columns[1].Cells) { c.Range.Font.ColorIndex = Word.WdColorIndex.wdGray50; c.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; c.RightPadding = 7.0f; c.Range.InsertAfter((i + 1).ToString() + "."); i++; } } else if (use_frame) { sel.Paragraphs.Last.SpaceAfter = 12.0f; sel.Paragraphs.First.SpaceBefore = 12.0f; int i_ = 0; foreach (Word.Paragraph p in sel.Paragraphs) { if (frameAlternatingLines && (i_ % 2 == 1)) { p.Shading.BackgroundPatternColor = frameAlternatingColor_; } else { p.Shading.BackgroundPatternColor = frameBgColor_; } i_++; } sel.ParagraphFormat.Borders[Word.WdBorderType.wdBorderTop].LineStyle = Word.WdLineStyle.wdLineStyleSingle; sel.ParagraphFormat.Borders[Word.WdBorderType.wdBorderTop].Color = frameBorderColor_; sel.ParagraphFormat.Borders[Word.WdBorderType.wdBorderTop].LineWidth = Word.WdLineWidth.wdLineWidth075pt; sel.ParagraphFormat.Borders[Word.WdBorderType.wdBorderLeft].LineStyle = Word.WdLineStyle.wdLineStyleSingle; sel.ParagraphFormat.Borders[Word.WdBorderType.wdBorderLeft].Color = frameBorderColor_; sel.ParagraphFormat.Borders[Word.WdBorderType.wdBorderLeft].LineWidth = Word.WdLineWidth.wdLineWidth300pt; sel.ParagraphFormat.Borders[Word.WdBorderType.wdBorderRight].LineStyle = Word.WdLineStyle.wdLineStyleSingle; sel.ParagraphFormat.Borders[Word.WdBorderType.wdBorderRight].Color = frameBorderColor_; sel.ParagraphFormat.Borders[Word.WdBorderType.wdBorderRight].LineWidth = Word.WdLineWidth.wdLineWidth075pt; sel.ParagraphFormat.Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleSingle; sel.ParagraphFormat.Borders[Word.WdBorderType.wdBorderBottom].Color = frameBorderColor_; sel.ParagraphFormat.Borders[Word.WdBorderType.wdBorderBottom].LineWidth = Word.WdLineWidth.wdLineWidth075pt; sel.ParagraphFormat.Borders.DistanceFromLeft = 7; } }