Docx - 删除文档部分
Docx - Removing section of document
有没有办法删除文档中可以指定开始和结束标记的部分?
我需要一种方法,我可以通过传入我的开始和结束捕获来删除文档的一部分,(@@DELETEBEGIN 和@@DELETEEND)
例如我的文档中有这个:
Hello, welcome to this document
@@DELETEBEGIN{Some values to check in the code}
Some text that will be removed if the value is true
@@DELETEEND
Final Line
我想我已经找到了解决办法,至少对我有用,如果有什么我可以做得更好的,请告诉我:
deleteCommand 将是@@DELETEBEGIN 字符串,deleteEndCommand 将是@@DELETEEND
private void RemoveSection(DocX doc, string deleteCommand, string deleteEndCommand)
{
try
{
int deleteStart = 0;
int deleteEnd = 0;
//Get the array of the paragraphs containing the start and end catches
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
if (doc.Paragraphs[i].Text.Contains(deleteCommand))
deleteStart = i;
if (doc.Paragraphs[i].Text.Contains(deleteEndCommand))
deleteEnd = i;
}
if (deleteStart > 0 && deleteEnd > 0)
{
//delete from the paraIndex as the arrays will shift when a paragraph is deleted
int paraIndex = deleteStart;
for (int i = deleteStart; i <= deleteEnd; i++)
{
doc.RemoveParagraphAt(paraIndex);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
如果您需要删除从@@DELETEBEGIN 到@@DELETEEND 的文本,其中@@DELETEBEGIN 不在Paragraph
的开头并且@@DELETEEND 不在[=11] 的结尾=],这段代码应该可以工作。
DocX document = DocX.Load("C:\Users\phil\Desktop\text.docx");
bool flag = false;
List<List<string>> list1 = new List<List<string>>();
List<string> list2 = new List<string>();
foreach (Novacode.Paragraph item in document.Paragraphs)
{
//use this if you need whole text of a paragraph
string paraText = item.Text;
var result = paraText.Split(' ');
int count = 0;
list2 = new List<string>();
//use this if you need word by word
foreach (var data in result)
{
string word = data.ToString();
if (word.Contains("@@DELETEBEGIN")) flag = true;
if (word.Contains("@@DELETEEND"))
{
flag = false;
list2.Add(word);
}
if (flag) list2.Add(word);
count++;
}
list1.Add(list2);
}
for (int i = 0; i < list1.Count(); i++)
{
string temp = "";
for (int y = 0; y < list1[i].Count(); y++)
{
if (y == 0)
{
temp = list1[i][y];
continue;
}
temp += " " + list1[i][y];
}
if (!temp.Equals("")) document.ReplaceText(temp, "");
}
document.Save();
我必须感谢这个 循环遍历每个单词。
有没有办法删除文档中可以指定开始和结束标记的部分?
我需要一种方法,我可以通过传入我的开始和结束捕获来删除文档的一部分,(@@DELETEBEGIN 和@@DELETEEND)
例如我的文档中有这个:
Hello, welcome to this document
@@DELETEBEGIN{Some values to check in the code}
Some text that will be removed if the value is true
@@DELETEEND
Final Line
我想我已经找到了解决办法,至少对我有用,如果有什么我可以做得更好的,请告诉我:
deleteCommand 将是@@DELETEBEGIN 字符串,deleteEndCommand 将是@@DELETEEND
private void RemoveSection(DocX doc, string deleteCommand, string deleteEndCommand)
{
try
{
int deleteStart = 0;
int deleteEnd = 0;
//Get the array of the paragraphs containing the start and end catches
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
if (doc.Paragraphs[i].Text.Contains(deleteCommand))
deleteStart = i;
if (doc.Paragraphs[i].Text.Contains(deleteEndCommand))
deleteEnd = i;
}
if (deleteStart > 0 && deleteEnd > 0)
{
//delete from the paraIndex as the arrays will shift when a paragraph is deleted
int paraIndex = deleteStart;
for (int i = deleteStart; i <= deleteEnd; i++)
{
doc.RemoveParagraphAt(paraIndex);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
如果您需要删除从@@DELETEBEGIN 到@@DELETEEND 的文本,其中@@DELETEBEGIN 不在Paragraph
的开头并且@@DELETEEND 不在[=11] 的结尾=],这段代码应该可以工作。
DocX document = DocX.Load("C:\Users\phil\Desktop\text.docx");
bool flag = false;
List<List<string>> list1 = new List<List<string>>();
List<string> list2 = new List<string>();
foreach (Novacode.Paragraph item in document.Paragraphs)
{
//use this if you need whole text of a paragraph
string paraText = item.Text;
var result = paraText.Split(' ');
int count = 0;
list2 = new List<string>();
//use this if you need word by word
foreach (var data in result)
{
string word = data.ToString();
if (word.Contains("@@DELETEBEGIN")) flag = true;
if (word.Contains("@@DELETEEND"))
{
flag = false;
list2.Add(word);
}
if (flag) list2.Add(word);
count++;
}
list1.Add(list2);
}
for (int i = 0; i < list1.Count(); i++)
{
string temp = "";
for (int y = 0; y < list1[i].Count(); y++)
{
if (y == 0)
{
temp = list1[i][y];
continue;
}
temp += " " + list1[i][y];
}
if (!temp.Equals("")) document.ReplaceText(temp, "");
}
document.Save();
我必须感谢这个