查了一些资料,好用的基本使用的都是spire,但这个对于我的使用方式来说不太合适,所以最终没有使用,下面的代码做个记录
using Word = Microsoft.Office.Interop.Word;
Word.Application app = Globals.ThisAddIn.Application;
Word.Document doc = app.ActiveDocument;
Word.ListTemplate template = doc.ListTemplates.Cast<Word.ListTemplate>().FirstOrDefault(info=>info.Name.Equals("paragraphNumber"))??doc.ListTemplates.Add(true, "paragraphNumber"); // 获取或新建模版
Word.ListLevel level = template.ListLevels[1];
level.NumberFormat = @"[%1]"; // 设置格式
level.NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabicLZ3; // 设置样式
template.OutlineNumbered = true;
int count = doc.Paragraphs.Count;
Word.Range range;
for (int i = 1; i <= count; i++)
{
range = doc.Paragraphs[i].Range;
if (range.Frames.Count > 0)
{
continue;
}
text = range.Text;
title = Regex.Replace(text, "[\\s\\a\\p{P}]", "");
if (title.StartsWith("aaa"))
{
isDescription = false;
continue;
}
else if (title.StartsWith("ccc"))
{
isDescription = true;
continue;
}
if (isDescription) // 只保留ccc至aaa中间部分内容
{
if (doc.Paragraphs[i].Alignment == Word.WdParagraphAlignment.wdAlignParagraphCenter) // 居中的跳过
{
continue;
}
if (string.IsNullOrWhiteSpace(text)) // 空白的跳过
{
continue;
}
range.ListFormat.ApplyListTemplate(template, true); // 设置模版,编号连续
range.ListFormat.ListLevelNumber = 1;
}
}