Word Interop InsertParagraphAfter 和下一行样式
Word Interop InsertParagraphAfter and next line style
我正在遍历现有 MS Word 文档的段落并在特定级别的标题后插入文本段落,但是当我插入文本时,它们继承了下面段落的一些样式 and/or下一个元素搞砸了。这是我的代码:
foreach (Word.Paragraph paragraph in doc.Paragraphs)
{
if (paragraph.get_Style(); != null && paragraph.get_Style() =="Heading 2")
{
paragraph.Range.InsertParagraphAfter();
paragraph.Next().Reset();
paragraph.Next().Range.Text = "New Text"
paragraph.Next().set_Style("My Style");
}
}
这很好用,除非我有以下条件
标题 2
- 列表项
- 列表项
- 列表项
我的最终结果如下:
标题 2
新文本
列表项
- 列表项
- 列表项
注意多余的空白点。那是我的问题。
这对我适用于 Word 2013
using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
Application app = new Application();
var doc = app.Documents.Open(@"C:\users\mhainc\desktop\test.docx");
foreach (Microsoft.Office.Interop.Word.Paragraph paragraph in doc.Paragraphs)
{
if (paragraph.get_Style() != null && paragraph.get_Style().NameLocal == "Heading 2")
{
paragraph.Range.InsertParagraphAfter();
paragraph.Next().Range.Text = "New Text\r\n";
paragraph.Next().Reset();
paragraph.Next().set_Style("Normal");
}
}
doc.Save();
doc.Close();
}
}
}
请注意,我已经更改了添加文本和重置调用的顺序,并在文本末尾添加了 \r\n 个字符(换行符)(没有换行符它也破坏了我的列表,但它正在从第一个列表项中删除项目符号,我无法用您的代码重现您的行为 :) )
如果 table 在您的文档中跟在标题 2 样式的标题之后,上述代码将无法正常工作。
为此,我构建了可以正确创建您 table 上方段落的代码。
using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
Application app = new Application();
var doc = app.Documents.Open(@"C:\users\mhainc\desktop\test.docx");
foreach (Microsoft.Office.Interop.Word.Paragraph paragraph in doc.Paragraphs)
{
if (paragraph.get_Style() != null && paragraph.get_Style().NameLocal == "Heading 2")
{
bool afterTableSplit = false;
if (paragraph.Next().Range.Tables.Count > 0)
{
//add dummy row to the table
object firstRow = paragraph.Next().Range.Tables[1].Rows[1];
firstRow = paragraph.Next().Range.Tables[1].Rows.Add(ref firstRow);
//split the table after the dummy row
paragraph.Next().Range.Tables[1].Split(2);
//delete the dummy row table
paragraph.Next().Range.Tables[1].Delete();
afterTableSplit = true;
}
paragraph.Range.InsertParagraphAfter();
paragraph.Next().Range.Text = "New Text";
if (!afterTableSplit) paragraph.Next().Range.Text += "\r\n";
paragraph.Next().Reset();
paragraph.Next().set_Style("Normal");
}
}
doc.Save();
doc.Close();
}
}
}
我正在遍历现有 MS Word 文档的段落并在特定级别的标题后插入文本段落,但是当我插入文本时,它们继承了下面段落的一些样式 and/or下一个元素搞砸了。这是我的代码:
foreach (Word.Paragraph paragraph in doc.Paragraphs)
{
if (paragraph.get_Style(); != null && paragraph.get_Style() =="Heading 2")
{
paragraph.Range.InsertParagraphAfter();
paragraph.Next().Reset();
paragraph.Next().Range.Text = "New Text"
paragraph.Next().set_Style("My Style");
}
}
这很好用,除非我有以下条件
标题 2
- 列表项
- 列表项
- 列表项
我的最终结果如下:
标题 2
新文本
列表项
- 列表项
- 列表项
注意多余的空白点。那是我的问题。
这对我适用于 Word 2013
using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
Application app = new Application();
var doc = app.Documents.Open(@"C:\users\mhainc\desktop\test.docx");
foreach (Microsoft.Office.Interop.Word.Paragraph paragraph in doc.Paragraphs)
{
if (paragraph.get_Style() != null && paragraph.get_Style().NameLocal == "Heading 2")
{
paragraph.Range.InsertParagraphAfter();
paragraph.Next().Range.Text = "New Text\r\n";
paragraph.Next().Reset();
paragraph.Next().set_Style("Normal");
}
}
doc.Save();
doc.Close();
}
}
}
请注意,我已经更改了添加文本和重置调用的顺序,并在文本末尾添加了 \r\n 个字符(换行符)(没有换行符它也破坏了我的列表,但它正在从第一个列表项中删除项目符号,我无法用您的代码重现您的行为 :) )
如果 table 在您的文档中跟在标题 2 样式的标题之后,上述代码将无法正常工作。
为此,我构建了可以正确创建您 table 上方段落的代码。
using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
Application app = new Application();
var doc = app.Documents.Open(@"C:\users\mhainc\desktop\test.docx");
foreach (Microsoft.Office.Interop.Word.Paragraph paragraph in doc.Paragraphs)
{
if (paragraph.get_Style() != null && paragraph.get_Style().NameLocal == "Heading 2")
{
bool afterTableSplit = false;
if (paragraph.Next().Range.Tables.Count > 0)
{
//add dummy row to the table
object firstRow = paragraph.Next().Range.Tables[1].Rows[1];
firstRow = paragraph.Next().Range.Tables[1].Rows.Add(ref firstRow);
//split the table after the dummy row
paragraph.Next().Range.Tables[1].Split(2);
//delete the dummy row table
paragraph.Next().Range.Tables[1].Delete();
afterTableSplit = true;
}
paragraph.Range.InsertParagraphAfter();
paragraph.Next().Range.Text = "New Text";
if (!afterTableSplit) paragraph.Next().Range.Text += "\r\n";
paragraph.Next().Reset();
paragraph.Next().set_Style("Normal");
}
}
doc.Save();
doc.Close();
}
}
}