Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created June 3, 2024 12:53
Show Gist options
  • Save bjoerntx/3cf42f086e9b9f21327790df92f6269b to your computer and use it in GitHub Desktop.
Save bjoerntx/3cf42f086e9b9f21327790df92f6269b to your computer and use it in GitHub Desktop.
List<string> ExtractTextBlocks(string paragraphStyleName, ServerTextControl serverTextControl, bool includeRemainingText)
{
List<string> textBlocks = new List<string>();
bool capturing = false;
StringBuilder currentBlock = new StringBuilder();
for (int i = 1; i < serverTextControl.Paragraphs.Count - 1; i++)
{
Paragraph paragraph = serverTextControl.Paragraphs[i];
if (paragraph.FormattingStyle == paragraphStyleName)
{
if (capturing)
{
textBlocks.Add(currentBlock.ToString().Trim());
currentBlock.Clear();
}
else
{
capturing = true;
}
}
else if (capturing)
{
currentBlock.AppendLine(paragraph.Text);
}
}
// Add remaining text if still capturing at the end
if (includeRemainingText && (capturing || currentBlock.Length > 0))
{
textBlocks.Add(currentBlock.ToString().Trim());
}
return textBlocks;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment