本文介绍了OpenOffice的使用.NET:如何遍历所有throught段落和文本阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何通过OpenOffice的Writer文档和文本输出的所有段落重复。
我有Java的例子,但不知道如何代码转换为C#。
Java的例子可以在这里找到:
我的C#代码:
InitOpenOfficeEnvironment();
XMultiServiceFactory multiServiceFactory =连接();
XComponentLoader componentLoader =
XComponentLoader)multiServiceFactory.createInstance(com.sun.star.frame.Desktop);
//设置属性
为PropertyValue [] =的PropertyValue新为PropertyValue [1];
为PropertyValue aProperty =新为PropertyValue();
aProperty.Name =隐藏;
aProperty.Value =新uno.Any(假);
的PropertyValue [0] = aProperty;
XComponent xComponent =
componentLoader.loadComponentFromURL(
@的文件:/// C:/code/test3.doc,
_blank,0,为PropertyValue);
XEnumerationAccess xEnumerationAccess =(XEnumerationAccess)xComponent;
XEnumeration xParagraphEnumeration = xEnumerationAccess.createEnumeration();
,而(xParagraphEnumeration.hasMoreElements())
{
//?
//问题是在这里nextElement()返回uno.Any但是
//我一些应该怎么弄XTextContent ????
uno.Any的TextElement = xParagraphEnumeration.nextElement();
//创建另一个枚举得到
//段落
XEnumeration xParaEnumerationAccess = textElement.createEnumeration的所有文字部分();
//第3步通过文字部分枚举,
//获取接口,每个文本部分
}
xComponent.dispose();
xComponent = NULL;
解决方案
OK,我找到了答案,我希望它会节省时间对于那些谁面临着同样的问题...
使用系统;
:使用System.IO;使用System.Runtime.InteropServices
;
使用的Microsoft.Win32;使用unoidl.com.sun.star.beans
;
使用unoidl.com.sun.star.container;
使用unoidl.com.sun.star.frame;
使用unoidl.com.sun.star.lang;
使用unoidl.com.sun.star.table;
使用unoidl.com.sun.star.text;
命名空间OOApp
{
类节目
{
静态无效的主要(字串[] args)
{
InitOpenOfficeEnvironment( );
XMultiServiceFactory multiServiceFactory =连接();
XComponent xComponent = NULL;
串docFileName = @C:\test3.doc
试
{
XComponentLoader componentLoader =
(XComponentLoader)multiServiceFactory
.createInstance(com.sun.star.frame.Desktop);
//设置属性
为PropertyValue [] =的PropertyValue新为PropertyValue [1];
为PropertyValue aProperty =新为PropertyValue();
aProperty.Name =隐藏;
aProperty.Value =新uno.Any(假);
的PropertyValue [0] = aProperty;
xComponent =
componentLoader
.loadComponentFromURL(PathConverter(docFileName),
_blank,0,为PropertyValue);
XTextDocument xTextDocument =((XTextDocument)xComponent);
XEnumerationAccess xEnumerationAccess =
(XEnumerationAccess)xTextDocument.getText();
XEnumeration xParagraphEnumeration =
xEnumerationAccess.createEnumeration();
,而(xParagraphEnumeration.hasMoreElements())
{
uno.Any元= xParagraphEnumeration.nextElement();
XServiceInfo信佛=(XServiceInfo)element.Value;
如果(xinfo.supportsService(com.sun.star.text.TextTable))
{
Console.WriteLine(发现表!);
XTextTable xTextTable =(XTextTable)element.Value;
的String [] = cellNames xTextTable.getCellNames();
的for(int i = 0; I< cellNames.Length;我++)
{
XCELL XCELL = xTextTable.getCellByName(cellNames [I]);
XTEXT xTextCell =(XTEXT)XCELL;
字符串strText的= xTextCell.getString();
Console.WriteLine(strText的);
}
}
,否则
{
XTextContent xTextElement =(XTextContent)element.Value;
XEnumerationAccess xParaEnumerationAccess =
(XEnumerationAccess)xTextElement;
//创建另一个枚举拿到款$ B $的
//所有文字部分b XEnumeration xTextPortionEnum =
xParaEnumerationAccess.createEnumeration();
//第3步通过文字部分枚举,获得接口
//每个单独的文本部分
,而(xTextPortionEnum.hasMoreElements())
{
XTextRange xTextPortion =
(XTextRange)xTextPortionEnum.nextElement()值。
Console.Write(xTextPortion.getString());
}
}
}
}
赶上(unoidl.com.sun.star.uno.Exception EXP1)
{
控制台。的WriteLine(exp1.Message);
Console.WriteLine(exp1.StackTrace);
}
赶上(System.Exception的EXP2)
{
Console.WriteLine(exp2.Message);
Console.WriteLine(exp2.StackTrace);
}
终于
{
xComponent.dispose();
xComponent = NULL;
}
Console.WriteLine(完成。);
到Console.ReadLine();
}
静态私人无效InitOpenOfficeEnvironment()
{
串baseKey;
// OpenOffice的是一个32位的应用程序,
//它的注册表位置是在64位操作系统
不同的,如果(Marshal.SizeOf(typeof运算(IntPtr的))== 8)
baseKey = @SOFTWARE\Wow6432Node\OpenOffice.org\
,否则
baseKey = @SOFTWARE\OpenOffice.org\
//获取URE目录
串键= baseKey + @Layers\URE\1
的RegistryKey章= Registry.CurrentUser.OpenSubKey(键);
如果(REG == NULL)=章Registry.LocalMachine.OpenSubKey(键);
串urePath = reg.GetValue(UREINSTALLLOCATION)作为字符串;
reg.Close();
urePath = Path.Combine(urePath,本);
//获取UNO路径
键= baseKey + @UNO\InstallPath
=章Registry.CurrentUser.OpenSubKey(键);
如果(REG == NULL)=章Registry.LocalMachine.OpenSubKey(键);
串unoPath = reg.GetValue(空)的字符串;
reg.Close();
线道路;
路径=的String.Format({0} {1},
System.Environment.GetEnvironmentVariable(PATH),urePath);
System.Environment.SetEnvironmentVariable(PATH,路径);
System.Environment.SetEnvironmentVariable(UNO_PATH,unoPath);
}
公共静态XMultiServiceFactory连接()
{
unoidl.com.sun.star.uno.XComponentContext m_xContext = NULL;
试
{
m_xContext = uno.util.Bootstrap.bootstrap();
的System.Console.WriteLine(OO:引导完成与成功);
}
赶上(System.Exception的EXP)
{
的System.Console.WriteLine(OO:引导<期间除外;+
exp.Message + >中);
的System.Console.WriteLine(OO:源+ exp.Source);
如果(exp.InnerException!= NULL)
{
的System.Console.WriteLine(OO:内部消息+
exp.InnerException.Message);
的System.Console.WriteLine(OO:内源+
exp.InnerException.Source);
}
}
回报率(m_xContext!= NULL)?
(XMultiServiceFactory)m_xContext.getServiceManager():空;
}
私人静态字符串PathConverter(字符串文件)
{
试
{
档= file.Replace(@\ ,/);
返回文件:///+文件;
}
赶上(System.Exception的前)
{
罚球前;
}
}
}
}
How to iterate through all paragraphs in OpenOffice Writer document and output text.I have Java examples, but don't know how to convert code to C#.Java example could be found here: http://wiki.services.openoffice.org/wiki/API/Samples/Java/Writer/TextDocumentStructure
My C# code:
InitOpenOfficeEnvironment();
XMultiServiceFactory multiServiceFactory = connect();
XComponentLoader componentLoader =
XComponentLoader)multiServiceFactory.createInstance("com.sun.star.frame.Desktop");
//set the property
PropertyValue[] propertyValue = new PropertyValue[1];
PropertyValue aProperty = new PropertyValue();
aProperty.Name = "Hidden";
aProperty.Value = new uno.Any(false);
propertyValue[0] = aProperty;
XComponent xComponent =
componentLoader.loadComponentFromURL(
@"file:///C:/code/test3.doc",
"_blank", 0, propertyValue);
XEnumerationAccess xEnumerationAccess = (XEnumerationAccess)xComponent;
XEnumeration xParagraphEnumeration = xEnumerationAccess.createEnumeration();
while ( xParagraphEnumeration.hasMoreElements() )
{
// ???
// The problem is here nextElement() returns uno.Any but
// I some how should get XTextContent????
uno.Any textElement = xParagraphEnumeration.nextElement();
// create another enumeration to get all text portions of
//the paragraph
XEnumeration xParaEnumerationAccess = textElement.createEnumeration();
//step 3 Through the Text portions Enumeration,
//get interface to each individual text portion
}
xComponent.dispose();
xComponent = null;
解决方案
OK, I found the answer, I hope it will save some time for those who faced the same problem…
using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.container;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.table;
using unoidl.com.sun.star.text;
namespace OOApp
{
class Program
{
static void Main(string[] args)
{
InitOpenOfficeEnvironment();
XMultiServiceFactory multiServiceFactory = Connect();
XComponent xComponent = null;
string docFileName = @"C:\test3.doc";
try
{
XComponentLoader componentLoader =
(XComponentLoader)multiServiceFactory
.createInstance("com.sun.star.frame.Desktop");
//set the property
PropertyValue[] propertyValue = new PropertyValue[1];
PropertyValue aProperty = new PropertyValue();
aProperty.Name = "Hidden";
aProperty.Value = new uno.Any(false);
propertyValue[0] = aProperty;
xComponent =
componentLoader
.loadComponentFromURL(PathConverter(docFileName),
"_blank", 0, propertyValue);
XTextDocument xTextDocument = ((XTextDocument)xComponent);
XEnumerationAccess xEnumerationAccess =
(XEnumerationAccess)xTextDocument.getText();
XEnumeration xParagraphEnumeration =
xEnumerationAccess.createEnumeration();
while (xParagraphEnumeration.hasMoreElements())
{
uno.Any element = xParagraphEnumeration.nextElement();
XServiceInfo xinfo = (XServiceInfo)element.Value;
if (xinfo.supportsService("com.sun.star.text.TextTable"))
{
Console.WriteLine("Found Table!");
XTextTable xTextTable = (XTextTable)element.Value;
String[] cellNames = xTextTable.getCellNames();
for (int i = 0; i < cellNames.Length; i++)
{
XCell xCell = xTextTable.getCellByName(cellNames[i]);
XText xTextCell = (XText)xCell;
String strText = xTextCell.getString();
Console.WriteLine(strText);
}
}
else
{
XTextContent xTextElement = (XTextContent)element.Value;
XEnumerationAccess xParaEnumerationAccess =
(XEnumerationAccess)xTextElement;
// create another enumeration to get all text portions of
//the paragraph
XEnumeration xTextPortionEnum =
xParaEnumerationAccess.createEnumeration();
//step 3 Through the Text portions Enumeration, get interface
//to each individual text portion
while (xTextPortionEnum.hasMoreElements())
{
XTextRange xTextPortion =
(XTextRange)xTextPortionEnum.nextElement().Value;
Console.Write(xTextPortion.getString());
}
}
}
}
catch (unoidl.com.sun.star.uno.Exception exp1)
{
Console.WriteLine(exp1.Message);
Console.WriteLine(exp1.StackTrace);
}
catch(System.Exception exp2)
{
Console.WriteLine(exp2.Message);
Console.WriteLine(exp2.StackTrace);
}
finally
{
xComponent.dispose();
xComponent = null;
}
Console.WriteLine("Done.");
Console.ReadLine();
}
static private void InitOpenOfficeEnvironment()
{
string baseKey;
// OpenOffice being a 32 bit app,
//its registry location is different in a 64 bit OS
if (Marshal.SizeOf(typeof(IntPtr)) == 8)
baseKey = @"SOFTWARE\Wow6432Node\OpenOffice.org\";
else
baseKey = @"SOFTWARE\OpenOffice.org\";
// Get the URE directory
string key = baseKey + @"Layers\URE\1";
RegistryKey reg = Registry.CurrentUser.OpenSubKey(key);
if (reg == null) reg = Registry.LocalMachine.OpenSubKey(key);
string urePath = reg.GetValue("UREINSTALLLOCATION") as string;
reg.Close();
urePath = Path.Combine(urePath, "bin");
// Get the UNO Path
key = baseKey + @"UNO\InstallPath";
reg = Registry.CurrentUser.OpenSubKey(key);
if (reg == null) reg = Registry.LocalMachine.OpenSubKey(key);
string unoPath = reg.GetValue(null) as string;
reg.Close();
string path;
path = string.Format("{0};{1}",
System.Environment.GetEnvironmentVariable("PATH"), urePath);
System.Environment.SetEnvironmentVariable("PATH", path);
System.Environment.SetEnvironmentVariable("UNO_PATH", unoPath);
}
public static XMultiServiceFactory Connect()
{
unoidl.com.sun.star.uno.XComponentContext m_xContext = null;
try
{
m_xContext = uno.util.Bootstrap.bootstrap();
System.Console.WriteLine("OO: bootstrap finished with success");
}
catch (System.Exception exp)
{
System.Console.WriteLine("OO: Exception during bootstrap <" +
exp.Message + ">");
System.Console.WriteLine("OO: Source" + exp.Source);
if (exp.InnerException != null)
{
System.Console.WriteLine("OO: Inner Message" +
exp.InnerException.Message);
System.Console.WriteLine("OO: Inner Source" +
exp.InnerException.Source);
}
}
return (m_xContext != null) ?
(XMultiServiceFactory)m_xContext.getServiceManager() : null;
}
private static string PathConverter(string file)
{
try
{
file = file.Replace(@"\", "/");
return "file:///" + file;
}
catch (System.Exception ex)
{
throw ex;
}
}
}
}
这篇关于OpenOffice的使用.NET:如何遍历所有throught段落和文本阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!