我有像这样的文字的word文档。在我自己的小项目中,我通过 Spire.Doc 将 .doc 或 .docx 文档转换为 .txt 文件并逐行获取,然后对它们进行一些工作,但现在出现了一个多色文本,我需要得到一个红线如下:逐个获取Word线,如果线颜色不是黑色,则将此彩色线添加到字符串中。我想忽略黑色文本,并与红色文本对齐。我怎样才能做到这一点?
问问题
468 次
1 回答
0
您可以使用以下代码获取红色文本:
Document document = new Document();
document.LoadFromFile("2.docx");
Section section = document.Sections[0];
StringBuilder sb = new StringBuilder();
foreach (Paragraph paragraph in section.Paragraphs)
{
foreach (DocumentObject obj in paragraph.ChildObjects)
{
if (obj is TextRange)
{
TextRange tr = obj as TextRange;
if (tr.CharacterFormat.TextColor.ToArgb() == Color.Red.ToArgb())
{
sb.AppendLine(tr.Text);
}
}
}
File.WriteAllText("RedText.txt", sb.ToString());
于 2018-04-10T09:52:47.680 回答