将文本从xps文档提取到文本框

时间:2015-08-12 10:05:47

标签: c# winforms xps xpsdocument

我在研究时继续使用此代码,但是将其复制到我的表单会在documentViewerElement部分The name 'documentViewerElement' does not exist in the current context

中给出错误
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();

我希望从xps文档中获取所有文本并将其放在富文本框中。

1 个答案:

答案 0 :(得分:2)

未定义documentViewerElement,因此您的错误。

在以下行中:

IXpsFixedPageReader _page 
= _document.FixedPages[documentViewerElement.MasterPageNumber];

documentViewerElement.MasterPageNumber只是页码,因此请将其更改为您要阅读的xps页面,例如

IXpsFixedPageReader _page 
= _document.FixedPages[0];

要阅读整个xps文件中的文字,您可以尝试以下操作(它与您的代码几乎相同,只是循环播放(取自here)。

private string ReadXpsFile(string fileName)
{
    XpsDocument _xpsDocument = new XpsDocument(fileName, System.IO.FileAccess.Read);
    IXpsFixedDocumentSequenceReader fixedDocSeqReader = _xpsDocument.FixedDocumentSequenceReader;
    IXpsFixedDocumentReader _document = fixedDocSeqReader.FixedDocuments[0];
    FixedDocumentSequence sequence = _xpsDocument.GetFixedDocumentSequence();
    string _fullPageText="";

    for (int pageCount = 0; pageCount < sequence.DocumentPaginator.PageCount; ++pageCount)
    {
        IXpsFixedPageReader _page = _document.FixedPages[pageCount];
        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"));
                        }
                    }
                }
            }
        }

        _fullPageText += _currentText.ToString();
    }

    return _fullPageText;
}