回答
添加引用到ReachFramework
和WindowsBase
及以下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:对于答案的反馈将有所帮助。 – Sanjay
我得到的例外如下:错误类型'System.IO.Packaging.Package'在没有引用的程序集中定义。您必须添加对程序集“WindowsBase,版本= 3.0.0.0,文化=中立,PublicKeyToken = 31bf3856ad364e35”的引用。 – 2013-09-26 05:33:17
+清除它..伟大的工作。 – 2013-09-26 06:30:00
类的全码:
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丢失 – salle55
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包含多个页面)。请参阅:http://i.imgur.com/gpcKxCX.png – salle55
方法,返回所有网页的文本(修改阿米尔: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();
}
- 1. 从PDF文档中提取文本 - C#
- 2. 从文本文档中提取句子
- 3. 问号XPS文档
- 4. DOC到XPS文件文档
- 5. 获取的由XPS文档作家
- 6. 截取PrintDialog到XPS文档编写器
- 7. 批处理文件从文本文档中提取变量
- 8. 从Word文档中提取宏到c#文本文件#
- 9. 如何创建XPS文档?
- 10. XPS文档有多安全?
- 11. 如何验证XPS文档?
- 12. 从HTML文档中提取文本到单词列表中
- 13. 使用docsplit从内存中的文档中提取文本
- 14. C#从MemoryStream中打开Office文档和Xps文件
- 15. 如何从Word文档中提取RTF/HTML文本?
- 16. 如何从PDF文档中提取文本?
- 17. 如何仅从HTML文档中提取粗体文本?
- 18. 从未知内容类型的文档中提取文本
- 19. 如何从PDF文档中提取文本
- 20. 使用Python从word文档中提取图像和文本
- 21. 如何从文档中提取/识别文本?
- 22. 如何从LaTeX文档中提取重要的文本内容
- 23. 使用斯坦福NER从文本文档中提取地址?
- 24. 从腐败(?)中提取文本pdf文档
- 25. 从文本文档中读取信息
- 26. 文档文本提取和修改
- 27. 在文档查看器中显示XPS文档
- 28. 从HTML中提取文本
- 29. 从html中提取文本?
- 30. 从WPF中提取文本
既然您已为问题为C#,因此几乎所有的答案都将在C#中,但为什么只有C#。你对其他语言过敏吗? –
不,但我的公司在C#开发,我也必须这样做 –
那么,什么?使用任何其他语言创建,然后使用任何在线转换器(如http://www.developerfusion.com/tools/convert/csharp-to-vb/#convert-again)将其更改为您所需的语言。在我的最后一个公司,我用C#编码,现在用VB编写代码。它(语法)在前两天是一个问题。 –