Extract texts from xps document to textbox

十年热恋 提交于 2019-12-25 13:14:11

问题


I keep running in to this code when researching, however copying this to my form gives me an error in the documentViewerElement part saying 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();

I'm hoping to get all the texts from an xps document and put it on a rich text box.


回答1:


documentViewerElement is not defined hence your error.

In the following line:

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

documentViewerElement.MasterPageNumber is just the page number, so change it to the xps page you want to read, e.g.

IXpsFixedPageReader _page 
= _document.FixedPages[0];

To read the text from the entire xps file you could try the following (it's pretty much the same as your code it's just looping (Taken from 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;
}


来源:https://stackoverflow.com/questions/31962073/extract-texts-from-xps-document-to-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!