【问题标题】:Extract text from a XPS Document [closed]从 XPS 文档中提取文本 [关闭]
【发布时间】:2012-08-29 00:53:33
【问题描述】:

我需要从 XPS 文档中提取特定页面的文本。 提取的文本应写入字符串。我需要这个来使用 Microsofts SpeechLib 读出提取的文本。 请仅在 C# 中提供示例。

谢谢

【问题讨论】:

  • 由于您已将问题标记为 C#,因此几乎所有答案都将在 C# 中,但为什么只有 C#。你对其他语言过敏吗?
  • 不,但我的公司是用 c# 开发的,我也必须这样做
  • 那又怎样?以任何其他语言创建,然后使用任何在线转换器(如developerfusion.com/tools/convert/csharp-to-vb/#convert-again)将其更改为您想要的语言。在我的上一家公司中,我使用 C# 编写代码,而目前我使用 VB 编写代码。并且它(语法)在前 2 天是个问题。
  • -1 WhatHaveYouTried.com (请更新您的问题以提供一些您尝试过的示例,我很乐意删除反对票。)

标签: c# text extraction xps


【解决方案1】:

添加对ReachFrameworkWindowsBase 的引用以及以下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

【讨论】:

  • @Tim Trabold:回答的反馈会很有帮助。
  • 我收到异常:错误 1 ​​类型“System.IO.Packaging.Package”在未引用的程序集中定义。您必须添加对程序集 'WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 的引用。
  • + 清除它.. 干得好。
  • 我正在查看我正在尝试解析的 .xps,但我发现有时显示为多个字符串(尽管都在一行上)的内容会作为一个字符串出现!跨度>
  • 如何使用 asp.net 读取 xps 文件,但出现错误,当前内容中不存在 documentviewer 元素
【解决方案2】:

从所有页面返回文本的方法(修改了 Amir:s 的代码,希望没问题):

/// <summary>
///   Get all text strings from an XPS file.
///   Returns a list of lists (one for each page) containing the text strings.
/// </summary>
private static List<List<string>> ExtractTextFromXps(string xpsFilePath)
{
   var xpsDocument = new XpsDocument(xpsFilePath, FileAccess.Read);
   var fixedDocSeqReader = xpsDocument.FixedDocumentSequenceReader;
   if (fixedDocSeqReader == null)
      return null;

   const string UnicodeString = "UnicodeString";
   const string GlyphsString = "Glyphs";

   var textLists = new List<List<string>>();
   foreach (IXpsFixedDocumentReader fixedDocumentReader in fixedDocSeqReader.FixedDocuments)
   {
      foreach (IXpsFixedPageReader pageReader in fixedDocumentReader.FixedPages)
      {
         var pageContentReader = pageReader.XmlReader;
         if (pageContentReader == null)
            continue;

         var texts = new List<string>();
         while (pageContentReader.Read())
         {
            if (pageContentReader.Name != GlyphsString)
               continue;
            if (!pageContentReader.HasAttributes)
               continue;
            if (pageContentReader.GetAttribute(UnicodeString) != null)
               texts.Add(pageContentReader.GetAttribute(UnicodeString));
         }
         textLists.Add(texts);   
      }
   }
   xpsDocument.Close();
   return textLists;
}

用法:

var txtLists = ExtractTextFromXps(@"C:\myfile.xps");

int pageIdx = 0;
foreach (List<string> txtList in txtLists)
{
   pageIdx++;
   Console.WriteLine("== Page {0} ==", pageIdx);
   foreach (string txt in txtList)
      Console.WriteLine(" "+txt);
   Console.WriteLine();
}

【讨论】:

    【解决方案3】:
        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;
        }
    

    【讨论】:

    • 我使用此代码得到 ArgumentOutOfRangeException,_document.FixedPages 仅包含单个元素(即使 XPS 包含多个页面)。见:i.imgur.com/gpcKxCX.png
    【解决方案4】:

    类的完整代码:

    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Windows.Xps.Packaging;
    
    namespace XPS_Data_Transfer
    {
        internal static class XpsDataReader
        {
            public static List<string> ReadXps(string address, int pageNumber)
            {
                var xpsDocument = new XpsDocument(address, System.IO.FileAccess.Read);
                var fixedDocSeqReader = xpsDocument.FixedDocumentSequenceReader;
                if (fixedDocSeqReader == null) return null;
    
                const string uniStr = "UnicodeString";
                const string glyphs = "Glyphs";
                var document = fixedDocSeqReader.FixedDocuments[pageNumber - 1];
                var page = document.FixedPages[0];
                var currentText = new List<string>();
                var pageContentReader = page.XmlReader;
    
                if (pageContentReader == null) return null;
                while (pageContentReader.Read())
                {
                    if (pageContentReader.Name != glyphs) continue;
                    if (!pageContentReader.HasAttributes) continue;
                    if (pageContentReader.GetAttribute(uniStr) != null)
                        currentText.Add(Dashboard.CleanReversedPersianText(pageContentReader.GetAttribute(uniStr)));
                }
                return currentText;
            }
        }
    }
    

    从自定义文件的自定义页面返回字符串数据列表。

    【讨论】:

    • Dashboard.CleanReversedPersianText 丢失
    猜你喜欢
    • 1970-01-01
    • 2015-11-04
    • 2011-07-16
    • 2011-10-23
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 2010-10-20
    相关资源
    最近更新 更多