<分区>
我需要从 XPS 文档中提取特定页面的文本。 提取的文本应该写在一个字符串中。我需要这个来使用 Microsofts SpeechLib 读出提取的文本。 请仅使用 C# 示例。
谢谢
<分区>
我需要从 XPS 文档中提取特定页面的文本。 提取的文本应该写在一个字符串中。我需要这个来使用 Microsofts SpeechLib 读出提取的文本。 请仅使用 C# 示例。
谢谢
最佳答案
添加对 ReachFramework
和 WindowsBase
的引用以及以下 using
语句:
using System.Windows.Xps.Packaging;
然后使用这段代码:
XpsDocument _xpsDocument=new XpsDocument("/path",System.IO.FileAccess.Read);
IXpsFixedDocumentSequenceReader fixedDocSeqReader
=_xpsDocument.FixedDocumentSequenceReader;
IXpsFixedDocumentReader _document = fixedDocSeqReader.FixedDocuments[0];
IXpsFixedPageReader _page
= _document.FixedPages[documentViewerElement.MasterPageNumber];
StringBuilder _currentText = new StringBuilder();
System.Xml.XmlReader _pageContentReader = _page.XmlReader;
if (_pageContentReader != null)
{
while (_pageContentReader.Read())
{
if (_pageContentReader.Name == "Glyphs")
{
if (_pageContentReader.HasAttributes)
{
if (_pageContentReader.GetAttribute("UnicodeString") != null )
{
_currentText.
Append(_pageContentReader.
GetAttribute("UnicodeString"));
}
}
}
}
}
string _fullPageText = _currentText.ToString();
文本存在于 Glyphs
-> UnicodeString
字符串属性中。对于固定页面,您必须使用 XMLReader
。
关于c# - 从 XPS 文档中提取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12262197/