using System;
using System.Linq;
using System.Text;
using System.Windows.Documents;
/// <summary>
/// 플로우 문서 헬퍼
/// </summary>
public static class FlowDocumentHelper
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 텍스트 추출하기 - ExtractText(flowDocument)
/// <summary>
/// 텍스트 추출하기
/// </summary>
/// <param name="flowDocument">플로우 문서</param>
/// <returns>텍스트</returns>
public static string ExtractText(FlowDocument flowDocument)
{
if(flowDocument == null)
{
throw new ArgumentNullException(nameof(flowDocument));
}
StringBuilder stringBuilder = new StringBuilder();
ExtractTextFromBlockCollection(flowDocument.Blocks, stringBuilder);
return stringBuilder.ToString();
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Private
#region 인라인 컬렉션에서 텍스트 추출하기 - ExtractTextFromInlineCollection(inlineCollection, stringBuilder)
/// <summary>
/// 인라인 컬렉션에서 텍스트 추출하기
/// </summary>
/// <param name="inlineCollection">인라인 컬렉션</param>
/// <param name="stringBuilder">문자열 빌더</param>
private static void ExtractTextFromInlineCollection(InlineCollection inlineCollection, StringBuilder stringBuilder)
{
foreach(Inline inline in inlineCollection)
{
if(inline is Run run)
{
stringBuilder.Append(run.Text);
}
else if(inline is LineBreak)
{
stringBuilder.AppendLine();
}
else if(inline is Span span)
{
ExtractTextFromInlineCollection(span.Inlines, stringBuilder);
}
}
}
#endregion
#region 테이블에서 텍스트 추출하기 - ExtractTextFromTable(table, stringBuilder)
/// <summary>
/// 테이블에서 텍스트 추출하기
/// </summary>
/// <param name="table">테이블</param>
/// <param name="stringBuilder">문자열 빌더</param>
private static void ExtractTextFromTable(Table table, StringBuilder stringBuilder)
{
foreach(TableRow tableRow in table.RowGroups.SelectMany(group => group.Rows))
{
foreach(TableCell tableCell in tableRow.Cells)
{
ExtractTextFromBlockCollection(tableCell.Blocks, stringBuilder);
stringBuilder.Append("\t");
}
stringBuilder.AppendLine();
}
}
#endregion
#region 리스트에서 텍스트 추출하기 - ExtractTextFromList(list, stringBuilder)
/// <summary>
/// 리스트에서 텍스트 추출하기
/// </summary>
/// <param name="list">리스트</param>
/// <param name="stringBuilder">문자열 빌더</param>
private static void ExtractTextFromList(List list, StringBuilder stringBuilder)
{
foreach(ListItem listItem in list.ListItems)
{
stringBuilder.Append("• ");
ExtractTextFromBlockCollection(listItem.Blocks, stringBuilder);
}
}
#endregion
#region 블럭 컬렉션에서 텍스트 추출하기 - ExtractTextFromBlockCollection(blockCollection, stringBuilder)
/// <summary>
/// 블럭 컬렉션에서 텍스트 추출하기
/// </summary>
/// <param name="blockCollection">블럭 컬렉션</param>
/// <param name="stringBuilder">문자열 빌더</param>
private static void ExtractTextFromBlockCollection(BlockCollection blockCollection, StringBuilder stringBuilder)
{
foreach(Block block in blockCollection)
{
if(block is Paragraph paragraph)
{
ExtractTextFromInlineCollection(paragraph.Inlines, stringBuilder);
stringBuilder.AppendLine();
}
else if(block is Section section)
{
ExtractTextFromBlockCollection(section.Blocks, stringBuilder);
}
else if(block is Table table)
{
ExtractTextFromTable(table, stringBuilder);
}
else if(block is List list)
{
ExtractTextFromList(list, stringBuilder);
}
}
}
#endregion
}