Aspose.words编程指南之DOM树结构初识,Node类继承关系及说明
来源:互联网 发布:推荐初学c语言的书籍 编辑:程序博客网 时间:2024/05/29 19:39
转载请注明 http://blog.csdn.net/sinat_30276961/article/details/48136289
上一篇Aspose.words介绍介绍了Aspose.words for Android的特性,使用方法以及评估版的使用。这一篇开始,正式讲解它丰富的API。
第一个应用
1.创建你的Android Application。
2.添加jar库到工程下的libs,添加apk到工程下的assets。Android Studio用户,gradle一下。
Eclipse用户,clean一下。
这样一来,就可以使用它的类库了。
3.在manifest添加写到sdcard的权限,并注明application。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:name="com.aspose.words.AsposeWordsApplication" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
4.在activity里使用其基本方法
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_first_aspose); new Thread(new Runnable() { @Override public void run() { try { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.writeln("Hello World!"); String filePath = Environment.getExternalStorageDirectory().getPath() + File.separator; doc.save(filePath + "First_out.doc"); } catch (Exception e) { e.printStackTrace(); } } }).start(); }
运行一下,如果一切顺利,就会在sdcard根目录下生成”First_out.doc”文档。
我们用WPS软件打开看看。
ok,没问题,已经把Hello World!写进去,并且评估的水印在最上方清晰的显示着。
接着,我们来看看加载、保存和转换相关功能
加载、保存和转换
加载或者创建一个文档
创建文档
通过Document空的构造函数,创建一个新的空的文档。
Document doc = new Document();
注意:这种方式创建的空文档已经包含一个section和一个paragraph。
如果你要以动态形式创建一个文档,最常用的方式是使用DocumentBuilder来增加内容。
如下所示:
Document doc = new Document();DocumentBuilder builder = new DocumentBuilder(doc);builder.writeln("Hello World!");doc.save(getMyDir() + "DocumentBuilderAndSave Out.docx");
打开文档
把文档的路径以字符串形式传给Document构造函数。
// Open a document. The file is opened read only and only for the duration of the constructor.Document doc = new Document(getMyDir() + "Document.doc");
传递给Document构造函数一个stream,
// Open the stream. Read only access is enough for Aspose.Words to load a document.InputStream stream = new FileInputStream(getMyDir() + "Document.doc");// Load the entire document into memory.Document doc = new Document(stream);// You can close the stream now, it is no longer needed because the document is in memory.stream.close();// ... do something with the document
以加密的方式打开文档。通过使用LoadOptions,指定密码。
Document doc = new Document(getMyDir() + "Document.LoadEncrypted.doc", new LoadOptions("qwerty"));
接着讲保存文档。
保存文档
我们可以通过使用Document.save来保存文档。可以保存成File或stream形式。并且可以直接保存成支持的任何格式的文档(它会自动转换)。要查看支持哪些保存类型,看SaveFormat这个类。
保存成File
简单调用Document.save,指定一个file路径。Aspose.words会根据你指定的文档路径的后缀,确定需要转换成的格式。
doc.save(getMyDir() + "Document.OpenFromFile Out.doc");
保存成stream
传一个输出流给Document.save,通过SaveFormat指定转换成的格式。
Document doc = new Document(getMyDir() + "Document.doc");ByteArrayOutputStream dstStream = new ByteArrayOutputStream();doc.save(dstStream, SaveFormat.DOCX);// In you want to read the result into a Document object again, in Java you need to get the// data bytes and wrap into an input stream.ByteArrayInputStream srcStream = new ByteArrayInputStream(dstStream.toByteArray());
指定Save options
Document.save可以传入一个SaveOptions。每一个保存格式都有一个相应的save options类。比方说,PdfSaveOptions与SaveFormat.Pdf格式相对应。
如下例子展示了以html格式保存。
Document doc = new Document(getMyDir() + "Rendering.doc");// This is the directory we want the exported images to be saved to.File imagesDir = new File(getMyDir(), "Images");// The folder specified needs to exist and should be empty.if(imagesDir.exists()) imagesDir.delete();imagesDir.mkdir();// Set an option to export form fields as plain text, not as HTML input elements.HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.HTML);options.setExportTextInputFormFieldAsText(true);options.setImagesFolder(imagesDir.getPath());doc.save(getMyDir() + "Document.SaveWithOptions Out.html", options);
转换格式
简单有效的文档格式转换能力是Aspose.Words四大特性之一。
我们的word应用一般都有个共性,那就是或多或少需要用到格式之间的转换,比方说RTF -> doc, doc -> docx, doc -> pdf等等。
Aspose.words给我们提供了很便捷的方式。
LoadFormat列举了加载支持的格式,SaveFormat列举了导出或保存的格式。Aspose.words支持它所支持的格式的各种格式之间的转换。
转换只需两步:
1.通过Document类,加载你的文档。默认情况下,它会自动判断当前格式是什么格式。
2.调用Document.save,并指定你希望输出格式,然后它会自动帮你转换成你想要的格式,是不是so easy。
接着,我们来看看Aspose.words的设计理念,即Document Object Model,简称DOM。
Document Object Model
总览
Aspose.Words的文档对象模型(DOM)是一个Word文档在内存中的映射,Aspose.Words的DOM可以通过编程读取、操作和修改Word文档的内容和格式。熟悉和掌握DOM的结构和相应的类型,是使用Aspose.Words灵活编程的基础。
下图展示了一个Word文档例子和其结构:
当上述文档被Aspose.Words的DOM读取时,会创建如下结构的树形对象:
从上图的结构和对应的Word文档,我们可以看到DOM中相关对象的结构。Document, Section, Paragraph, Table, Shape, Run以及图中的其他椭圆形的都是Aspose.Words对象,这些对象具有树形的层级结构。图中的注释同样说明这些文档对象树中的对象具有多个属性。
Node相关类
Node类介绍
当Aspose.Words读取Word文档到内存中时,不同类型的文档元素被不同的类型对象所替代,每一个文本框的text, paragraph, table, section都是Node对象,甚至文档本身都是一个Node。Aspose.Words为每一种文档节点类型都定义了一个类。
下面是一个UML类图,表示DOM中不同node类型之间的关系。抽象类的名字用斜体表示。注意,Aspose.Words DOM中同样包括了一些非节点类型的类,例如Style, PageSetup, Font等等,它们没有在这幅图里面显示。
我们接着来看看主要类及其功能:
Aspose.Words 类类别描述DocumentDocumentDocument对象是文档树的根节点,提供访问整个文档的入口SectionDocumentSection对象对应文档中的一节BodyDocument是一节中的主要文本容器HeaderFooterDocument一节中的特殊页眉或者页脚文本容器GlossaryDocumentDocument代表一个Word文档中词汇表的根条目BuildingBlockDocument代表一个词汇表文档,如构件,自动图文集或一个自动更正条目ParagraphText一个文本段落,包含内联的节点RunText一个格式一致的文本块BookmarkStartText一个书签的起点标记BookmarkEndText一个书签的结束标记FieldStartText一个特殊的字符,指定一个单词字段的开始FieldSeparatorText一个特殊的字符,指定单词字段的分隔FieldEndText一个特殊的字符,指定一个单词字段的结束FormFieldText一个表单字段SpecialCharText特殊字符类型,没有具体的字符类型TableTablesWord文档中的表格RowTables一个表格对象的行CellTables表格行的单元格ShapeShapesWord文档中的图像,形状,文本框或者OLE对象GroupShapeShapes一组Shapes对象FootnoteAnnotations文档中包括文本的脚注或者尾注CommentAnnotations文档中包含文本的注释CommentRangeStartAnnotations一个相关的注释区域的开始CommentRangeEndAnnotations一个相关的注释区域的结束SmartTagMarkup在一个段落内围绕一个或多个内嵌结构的智能标记CustomXmlMarkupMarkup文档中的某些结构的自定义XML标记StructuredDocumentTagMarkup文档中的一种结构化的文档标签(内容控制)OfficeMathMath代表Office的数学对象,如函数,方程或者矩阵尼玛,这么多,不敲不知道,一敲才发现挺多的~~。
上述这些类是叶子节点,没有子类了。我们接着看看树节点,包含子类的节点。
类描述Node所有节点类的抽象基类。提供了基本的功能CompositeNode基本的节点类,可以包含其他节点对象, 提供了访问,插入,移除和选择子节点的功能Story一个Word文档的文本存储在几个Stories. 它是section层stories的基类,包含Body和HeaderFooter子类InlineStoryinline层节点的基类,包含一个story: Comment和Footnote.Inlineinline层节点的基类,由一个单独的格式一致的文本组成DocumentBase一个文档里主要文档和词汇表文档的抽象父类通过NodeType区别Node
我们可以通过调用getNodeType()获取到当前的Node是什么类型。比方说,paragraph node返回NodeType.Paragraph,table node返回NodeType.Table,等等。
Document doc = new Document();// Returns NodeType.Documentint type = doc.getNodeType();
文档的逻辑层级
我们要搞清楚的是,上面说到的层级关系只是Node的继承关系,不是一个dom树的层级关系。
下面的表格阐述了node节点在dom树的层级关系
节点层级类描述Document levelSection最上层文档节点,只包含Setions对象。一个Section是一个包含一些stories的容器,用来存放 主要文本和其他头注和脚注Block levelParagraph, Table, StructuredDocumentTag, CustomXmlMarkup表格和段落是块层元素,可以包含其他元素。自定义标记节点可以包含嵌套块级节点。Inline levelRun, FormField, SpecialChar, FieldChar, FieldStart, FieldSeparator, FieldEnd, Shape, GroupShape, Comment, Footnote, CommentRangeStart, CommentRangeEnd, SmartTag, StructuredDocumentTag, CustomXmlMarkup, BookmarkStart and BookmarkEnd.Inline存在与段落里,并且在文档里显示实质的内容。脚注、评论和形状可以包含块级元素。自定义标记节点可以包含嵌套inline层元素。关于DOM树结构和Node类继承关系的介绍就到这里,下一篇Aspose.words编程指南之DOM树再识,各层结构之间的关系会通过直观的结构图讲解一个Document包含的层级关系。
- Aspose.words编程指南之DOM树结构初识,Node类继承关系及说明
- Aspose.words编程指南之DOM树再识,各层结构之间的关系
- Aspose.words编程指南之DocumentBuilder一
- Aspose.words编程指南之DocumentBuilder二
- Aspose.words编程指南之Working with Document
- Aspose.words Mail Merge初识
- Aspose.words之插入水印
- 【原创】Aspose.Words组件介绍及使用—基本介绍与DOM概述(一)
- 【原创】Aspose.Words组件介绍及使用—基本介绍与DOM概述
- Aspose.Words组件介绍及使用—基本介绍与DOM概述
- Aspose.Words操作word excel ppt说明
- WebKit之Node的继承关系图
- Aspose.words之IReplacingCallback接口的应用
- android studio使用-查看类结构及继承关系
- 初识JavaScript及DOM
- Aspose.Words使用教程之表的合并与拆分
- Aspose.Words使用教程之插入文档元素(三)
- Aspose.Words使用教程之从零创建OOXML图表
- oracle的update更新
- 1135 -- 逆转字符串
- Effective C++ Item1
- 程序设计基石与实践之实现数字七段显示与发扑克牌问题
- STL
- Aspose.words编程指南之DOM树结构初识,Node类继承关系及说明
- 1136 -- 敲7
- 代码的健壮性和高效性
- 《IOS_作业C语言》高级指针(结构体指针、结构体数组指针、结构体数组指针,作为函数的参数使用、预编译指令)
- 数据库学习参考书目
- USACO-Section 2.1 Healthy Holsteins (状态压缩DFS)
- Hadoop第5周练习—MapReduce计算气象温度等例子
- 精通Hibernate——映射组成关系
- 1137 -- 新年快乐!