Closed CodingOctocat closed 4 years ago
I tried it many times to find a solution that didn't corrupt the document and worked as expected:
public static void RemoveAllParagraphs(XWPFTableCell cell, Predicate<string> match = default)
{
match ??= _ => true;
var removes = new List<int>();
for (int i = cell.Paragraphs.Count - 1; i >= 0; i--)
{
if (match(cell.Paragraphs[i].ParagraphText))
{
removes.Add(i);
}
}
int x = 0;
if (removes.Count == cell.Paragraphs.Count)
{
x = 1;
}
for (int i = 0; i < removes.Count - x; i++)
{
cell.RemoveParagraph(removes[i]);
}
string p0Text = cell.Paragraphs[0].ParagraphText;
if (cell.Paragraphs.Count == 1 && p0Text != "" && match(p0Text))
{
cell.Paragraphs[0].ReplaceText(p0Text, "");
}
}
This is my code:
This code can satisfy me, but obviously it's not perfect. And I don't understand why the commented code above corrupts the document.