164 lines
5.8 KiB
C#
164 lines
5.8 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;
|
|
|
|
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 bookmarks = Globals.ThisAddIn.Application.ActiveDocument.Bookmarks;
|
|
ControlCollection controls = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument).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;
|
|
//mark.Delete();
|
|
}
|
|
try
|
|
{
|
|
if (controls.Contains(mark))
|
|
{
|
|
controls.Remove(mark);
|
|
}
|
|
}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;
|
|
}
|
|
}
|
|
}
|