是否可以检测使用C#从Powerpoint提取的下划线,斜体或粗体文本的特定行?

时间:2019-06-12 15:59:39

标签: c# powerpoint

我有一张带有1个文本框和多行文本的PowerPoint幻灯片。如果第2行以粗体显示,该如何检测?

Sentence one 
Sentence two -this one is italic for example
Sentence three
Sentence four

这是我的代码尝试执行的操作,我意识到由于powerpoint的文本全部在一个TextFrame.TextRange中,因此我不得不尝试将其拆分以检查要检测字体样式的各个行。因此,我尝试将每行的开始和结束位置保存到一个对象中,然后尝试使用TextRange.Characters()获取单个行并从此处检查其样式。但是我得到的只是msoTriStateMixed,因此给了我错误的结果。

Application pptApplication = new Application();
Presentation pptPresentation = pptApplication.Presentations.Open(filename, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
foreach (Slide _slide in pptPresentation.Slides) {
    foreach (Microsoft.Office.Interop.PowerPoint.Shape _shape in _slide.Shapes) {
        if(_shape.HasTextFrame == MsoTriState.msoTrue) {
            var textFrame = _shape.TextFrame;
            if(textFrame.HasText == MsoTriState.msoTrue) {
                var textRange = textFrame.TextRange;

                int startingPoint = 1;
                string[] lines = textRange.Text.ToString().Split(
                    new[] { "\n", "\r", "\r\n" },
                    StringSplitOptions.None
                );

                List<Line> LineList = new List<Line>();
                Line newLine = new Line();
                foreach(string x in lines) {
                    newLine.startingPos = startingPoint;
                    newLine.endingPos = startingPoint + x.Length;
                    LineList.add(newLine);
                    startingPoint += x.Length;
                }

                foreach(Line L in LineList) {
                    if (textRange.Characters(L.startingPos, L.endingPos - 1).Font.Bold == MsoTriState.msoTrue)
                        Console.WriteLine("bolded = true");
                    else
                        Console.WriteLine("bolded = false");
                }
            }
        }
    }
}

0 个答案:

没有答案