284 lines
11 KiB
C#
284 lines
11 KiB
C#
//using Microsoft.Office.Interop.Word;
|
||
using Section = Microsoft.Office.Interop.Word.Section;
|
||
using WdColor = Microsoft.Office.Interop.Word.WdColor;
|
||
using System.Drawing;
|
||
using UtilLib;
|
||
using Microsoft.Office.Interop.Word;
|
||
using Bookmark = Microsoft.Office.Tools.Word.Bookmark;
|
||
using System.Collections.Generic;
|
||
using Microsoft.Office.Tools.Word;
|
||
using System;
|
||
using System.Xml.Linq;
|
||
|
||
namespace AIProofread
|
||
{
|
||
public class DocumentUtil
|
||
{
|
||
static int markId = 0;
|
||
|
||
/// <summary>
|
||
/// 添加一个书签
|
||
/// </summary>
|
||
/// <param name="color"></param>
|
||
/// <param name="start"></param>
|
||
/// <param name="end"></param>
|
||
/// <returns></returns>
|
||
public static Bookmark AddBookmark(string color, int start, int end)
|
||
{
|
||
var doc = Globals.ThisAddIn.Application.ActiveDocument;
|
||
var maxOffset = doc.Range().End;
|
||
if (start > maxOffset || end > maxOffset)
|
||
{
|
||
return null;
|
||
}
|
||
var document = Globals.Factory.GetVstoObject(doc);
|
||
var r = document.Range(start, end);
|
||
var mark = document.Controls.AddBookmark(r, string.Format("ai_mark_{0}", markId++));
|
||
mark.Tag = "ai_proofread";
|
||
if (color != null)
|
||
{
|
||
// 给选区添加背景颜色
|
||
r.Shading.BackgroundPatternColor = (WdColor)ColorTranslator.ToOle(Colors.FromHex(color));
|
||
}
|
||
|
||
return mark;
|
||
}
|
||
|
||
public static void SectionAddMark(string color)
|
||
{
|
||
|
||
|
||
var document = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
|
||
// 获取当前文档对象
|
||
var doc = Globals.ThisAddIn.Application.ActiveDocument;
|
||
// 获取选中
|
||
var sections = document.Sections;
|
||
|
||
foreach (Section section in sections)
|
||
{
|
||
var r = section.Range;
|
||
|
||
//Bookmark mark = r.Bookmarks.Add();
|
||
var mark = document.Controls.AddBookmark(r, string.Format("ai_mark_{0}", markId++));
|
||
mark.Tag = "ai_proofread";
|
||
// 给选区添加背景颜色
|
||
r.Shading.BackgroundPatternColor = (WdColor)ColorTranslator.ToOle(Colors.FromHex(color));
|
||
}
|
||
}
|
||
|
||
public static System.Collections.Generic.List<string> GetAllBookmark()
|
||
{
|
||
var bookmarks = Globals.ThisAddIn.Application.ActiveDocument.Bookmarks;
|
||
System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();
|
||
foreach (Microsoft.Office.Interop.Word.Bookmark mark in bookmarks)
|
||
{
|
||
list.Add(mark.Name);
|
||
}
|
||
return list;
|
||
}
|
||
/// <summary>
|
||
/// 删除所有标签
|
||
/// </summary>
|
||
public static void RemoveBookmark()
|
||
{
|
||
RemoveBookmark(null);
|
||
}
|
||
|
||
public static void ClearProofreadMarks()
|
||
{
|
||
var document = Globals.ThisAddIn.Application.ActiveDocument;
|
||
var bookmarks = document.Bookmarks;
|
||
ControlCollection controls = Globals.Factory.GetVstoObject(document).Controls;
|
||
foreach (Microsoft.Office.Interop.Word.Bookmark mark in bookmarks)
|
||
{
|
||
if (Config.IsProofreadMark(mark.Name))
|
||
{
|
||
// 去除高亮
|
||
mark.Range.Shading.BackgroundPatternColor = WdColor.wdColorAutomatic;
|
||
// 去除下划线
|
||
//mark.Range.Underline = WdUnderline.wdUnderlineNone;
|
||
try
|
||
{
|
||
mark.Delete();
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Logger.Log("remove mark", e);
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
/// <summary>
|
||
/// 删除标签
|
||
/// </summary>
|
||
/// <param name="markId"></param>
|
||
public static void RemoveBookmark(string markId)
|
||
{
|
||
var document = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
|
||
if (document.Bookmarks.Count == 0) return;
|
||
// 不存在要移除的标签
|
||
if (null != markId && !document.Bookmarks.Exists(markId)) return;
|
||
var bookmarks = Globals.ThisAddIn.Application.ActiveDocument.Bookmarks;
|
||
foreach (Microsoft.Office.Interop.Word.Bookmark mark in bookmarks)
|
||
{
|
||
if (mark.Name == markId)
|
||
{
|
||
mark.Range.Shading.BackgroundPatternColor = (WdColor)WdColorIndex.wdAuto;
|
||
mark.Delete();
|
||
}
|
||
}
|
||
//foreach (var mark in document.Bookmarks)
|
||
//{
|
||
// if (markId != null)
|
||
// {
|
||
// if (mark.Name == markId)
|
||
// {
|
||
// // 删除
|
||
// mark.Delete();
|
||
// mark.Range.Shading.BackgroundPatternColor = (WdColor)WdColorIndex.wdAuto;
|
||
// return;
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// mark.Delete();
|
||
// }
|
||
//}
|
||
}
|
||
|
||
public static System.Collections.Generic.List<string> GetSectionText()
|
||
{
|
||
var document = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
|
||
System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();
|
||
if (document.Sections.Count == 0) return list;
|
||
foreach (Section item in document.Sections)
|
||
{
|
||
list.Add(item.Range.Text);
|
||
}
|
||
return list;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查找对应段落的range
|
||
/// </summary>
|
||
/// <param name="paragraphsIndex">段落index,从第1开始计数</param>
|
||
/// <param name="findText">要查找字符串</param>
|
||
/// <param name="wordStart">字符串原始起始位置</param>
|
||
/// <param name="wordEnd">字符串原始结束位置</param>
|
||
/// <returns></returns>
|
||
public static Range FindRange(int paragraphsIndex, string findText, int wordStart, int wordEnd)
|
||
{
|
||
// <param name="findIndex"></param> 暂时接口无法支持
|
||
var document = Globals.ThisAddIn.Application.ActiveDocument;
|
||
if (paragraphsIndex > document.Paragraphs.Count) return null;
|
||
var paragraph = document.Paragraphs[paragraphsIndex];
|
||
try
|
||
{
|
||
//int num = 0;
|
||
int num3 = 0;
|
||
|
||
// 段落开始和结束
|
||
object Start = paragraph.Range.Start;
|
||
object End = paragraph.Range.End;
|
||
|
||
// 段落选区
|
||
Range range = document.Range(ref Start, ref End);
|
||
|
||
var activeDocument = document;
|
||
// 查找对象位置
|
||
End = range.Start + wordStart;
|
||
Start = range.Start + wordEnd;
|
||
// 直接找到
|
||
var findRange = activeDocument.Range(ref End, ref Start);
|
||
// 判断对应选区是否是要找的文本
|
||
if (findRange.Text == findText)
|
||
{
|
||
return findRange;
|
||
}
|
||
else
|
||
{
|
||
// 查找
|
||
range.Find.MatchByte = true;
|
||
// 使用选区查找功能
|
||
Find find = range.Find;
|
||
// 节约变量
|
||
End = findText;
|
||
Start = true;
|
||
|
||
object MatchWholeWord = Type.Missing;
|
||
object MatchWildcards = Type.Missing;
|
||
object MatchSoundsLike = Type.Missing;
|
||
object MatchAllWordForms = Type.Missing;
|
||
object Forward = Type.Missing;
|
||
object Wrap = Type.Missing;
|
||
object Format = Type.Missing;
|
||
object ReplaceWith = Type.Missing;
|
||
object Replace = Type.Missing;
|
||
object MatchKashida = Type.Missing;
|
||
object MatchDiacritics = Type.Missing;
|
||
object MatchAlefHamza = Type.Missing;
|
||
object MatchControl = Type.Missing;
|
||
|
||
// 没有找到则执行查询
|
||
find.Execute(
|
||
ref End, ref Start, ref MatchWholeWord,
|
||
ref MatchWildcards, ref MatchSoundsLike,
|
||
ref MatchAllWordForms, ref Forward, ref Wrap,
|
||
ref Format, ref ReplaceWith, ref Replace,
|
||
ref MatchKashida, ref MatchDiacritics,
|
||
ref MatchAlefHamza, ref MatchControl
|
||
);
|
||
|
||
while (range.Find.Found)
|
||
{
|
||
var obj4 = range.Document;
|
||
MatchControl = range.Start;
|
||
MatchAlefHamza = range.End;
|
||
var range2 = obj4.Range(ref MatchControl, ref MatchAlefHamza);
|
||
num3 = range2.End;
|
||
if (range2.Text == findText)
|
||
{
|
||
return range2;
|
||
}
|
||
break;
|
||
//if (findIndex == num)
|
||
//{
|
||
|
||
//}
|
||
//num++;
|
||
//range.Find.MatchByte = true;
|
||
//Find find2 = range.Find;
|
||
//MatchAlefHamza = missword;
|
||
//MatchControl = true;
|
||
//MatchDiacritics = Type.Missing;
|
||
//MatchKashida = Type.Missing;
|
||
//Replace = Type.Missing;
|
||
//ReplaceWith = Type.Missing;
|
||
//Format = Type.Missing;
|
||
//Wrap = Type.Missing;
|
||
//Forward = Type.Missing;
|
||
//MatchAllWordForms = Type.Missing;
|
||
//MatchSoundsLike = Type.Missing;
|
||
//MatchWildcards = Type.Missing;
|
||
//MatchWholeWord = Type.Missing;
|
||
//Start = Type.Missing;
|
||
//End = Type.Missing;
|
||
//// 再次重复查找
|
||
//find2.Execute(ref MatchAlefHamza, ref MatchControl, ref MatchDiacritics, ref MatchKashida, ref Replace, ref ReplaceWith, ref Format, ref Wrap, ref Forward, ref MatchAllWordForms, ref MatchSoundsLike, ref MatchWildcards, ref MatchWholeWord, ref Start, ref End);
|
||
}
|
||
if (num3 == 0)
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
}
|
||
return null;
|
||
}
|
||
|
||
}
|
||
}
|