【问题标题】:C# Extract Text from .XPS DocumentC# 从 .XPS 文档中提取文本
【发布时间】:2019-02-06 19:02:48
【问题描述】:

我一直使用Another StackOverflow answer to this question 作为解决此问题的参考,但是我遇到了问题。我在FixedDocumentSequence 收到一个错误,说找不到它。我已经添加了对PresentationCorePresentationFrameworkWindowsBaseReachFramework 的引用,但我不太确定是否需要为FixedDocumentSequence 添加另一个引用。

这是我的代码:

public string convertXPS(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;




    }

【问题讨论】:

  • 我正在使用相同的参考代码做同样的事情,我不记得我做了什么使错误消失,因为我尝试了很多东西,但请查看上面添加 [STAThread]主程序并尝试,如果它有效并且您的 xps 文档已被读取,请注意,我坚持这个
  • 嘿伙计,我刚刚成功地从 XPS 读取文本,最后我很高兴我做到了,所以你可以用它来阅读文本,但不幸的是我的 xps 包含图像而不是文本,所以我需要从图像中读取文本

标签: c# c#-4.0


【解决方案1】:
[STAThread]
static void Main(string[] args)
{

    try
    {
        XpsDocument _xpsDocument = new XpsDocument(@"C:\Users\admin-\Desktop\testing.xps", 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();
        }
    }
    catch(Exception e)
    {

    }
}  

我认为代码没有太大变化,尝试添加帮助我读取xps的[STAThread],我也只使用上面提到的引用来读取文件,我也得到了同样的错误你得到了,但以某种方式解决了它,你离结果更近了 90%
还要看需要哪个引用添加System.Windows.Documents;

【讨论】:

    猜你喜欢
    • 2012-08-29
    • 2015-11-04
    • 2011-07-16
    • 2011-01-18
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多