从XPS文档中提取文本 [英] Extract text from a XPS Document

查看:2870
本文介绍了从XPS文档中提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个XPS文档中提取特定的页面的文本。
所提取的文本应该写在一个字符串。我需要这个读出微软使用的SpeechLib提取的文本。
请只在C#中的例子。

i need to extract the text of a specific page from a XPS document. The extracted text should be written in a string. I need this to read out the extracted text using Microsofts SpeechLib. Please examples only in C#.

感谢

推荐答案

添加引用 ReachFramework WindowsBase 及以下使用声明:

Add References to ReachFramework and WindowsBase and the following using statement:

using System.Windows.Xps.Packaging;



然后用这个代码:

Then use this code:

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();

文本在字形存在 - > 的UnicodeString 字符串属性。你必须使用的XMLReader 固定页面。

Text exists in Glyphs -> UnicodeString string attribute. You have to use XMLReader for fixed page.

这篇关于从XPS文档中提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆