(转)Aspose.words编程指南之Working with Document

https://blog.csdn.net/sinat_30276961/article/details/48159623

上一篇Aspose.words编程指南之DOM树再识,各层结构之间的关系从UML结构图直观的了解了DOM树层级之间的关系,并且通过代码展示了层级之间获取的方法。这一篇,我会接着讲解最核心的Node节点—Document,也就是一个文档的根节点。

Document总览

如果你仔细读过前面几篇的博客,那你一定知道,Document是一个文档的核心节点,也是根节点。它是一个的文档起始,没有它,你就别想创建一个word文档了。Document包含了所有内容。

通过Document,我们可以获得全局的text, bookmarks和form fields或者直属于各个Section。

Document里维护了一个Section的collection,也就是它的子节点。可以copy,插入或者移除。

Document可以在任意时候保存成文件或者流。甚至可以直接传给浏览器。

Document Properties

通过office软件,查看,编辑属性

要注意的是,这里讲的属性有别于前面几篇讲到的属性。

Document properties指的是一个文档所包含的全局属性。举个例子,你创建了一个word文档之后,可以通过鼠标右键查看它的一些属性。如下图所示:

这里写图片描述

这些是默认的,每个文档都会存在的属性。我们可以通过这个窗口,编辑属性。

我们也可以自定义一些属性,通过点击刚才窗口的自定义tab,然后可以新建属性:

这里写图片描述

上面这两类属性合起来就是Document Properties。

String fileName = getMyDir() + "Properties.doc";
Document doc = new Document(fileName);

System.out.println(MessageFormat.format("1. Document name: {0}", fileName));

System.out.println("2. Built-in Properties");
for (DocumentProperty prop : doc.getBuiltInDocumentProperties())
    System.out.println(MessageFormat.format("{0} : {1}", prop.getName(), prop.getValue()));

System.out.println("3. Custom Properties");
for (DocumentProperty prop : doc.getCustomDocumentProperties())
    System.out.println(MessageFormat.format("{0} : {1}", prop.getName(), prop.getValue()));

通过Aspose.words查看编辑属性

1.通过Document.BuiltInDocumentProperties,获取一个文档的默认属性。
2.通过Document.CustomDocumentProperties,获取一个文档的自定义属性。

Document.BuiltInDocumentProperties返回一个Properties.BuiltInDocumentProperties对象;Document.CustomDocumentProperties返回一个Properties.CustomDocumentProperties对象。
这两个对象都是collection,我们可以通过index和名称进行遍历。

Properties.CustomDocumentProperties允许我们做添加和移除属性动作。

Properties.DocumentProperty允许你获取名称,值和类型。

更新默认(内嵌)的属性

我们都知道,当我们修改了一个word文档,那么它的修改日期会自动帮我们更新。

但是Aspose.words没有这项便利服务,好吧,我们只能自己去修改了。
它给我们提供了一个方法:Document.UpdateWordCount,用来重新计算一些统计方面的数据。如下列举了哪些会去重新计算的属性:
BuiltInDocumentProperties.Characters,
BuiltInDocumentProperties.CharactersWithSpaces,
BuiltInDocumentProperties.Words,
BuiltInDocumentProperties.Paragraphs

增加或移除自定义属性

我们无法增加或移除默认的属性,不过可以操作自定义属性。

Document doc = new Document(getMyDir() + "Properties.doc");

CustomDocumentProperties props = doc.getCustomDocumentProperties();

if (props.get("Authorized") == null)
{
    props.add("Authorized", true);
    props.add("Authorized By", "John Smith");
    props.add("Authorized Date", new Date());
    props.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber());
    props.add("Authorized Amount", 123.45);
}
Document doc = new Document(getMyDir() + "Properties.doc");

doc.getCustomDocumentProperties().remove("Authorized Date");

代码很简单。

Clone Document

为啥要单独把它拎出来讲呢?

前面几篇讲到过,要创建一个Document,我们需要一步步添加DOM层级,比方说,先创建一个document,然后创建一个section,添加到document,然后是创建一个Body,添加到section,再创建一些paragraph,添加到body……这个过程漫长而麻烦,那么有没有一个简单快捷的方法,比方说克隆。

答案是有的!当然这个方案有个大前提,那就是你要克隆的文档与你想要创建的文档结构上类似。那么这样一来,我只要克隆一个,然后稍微做下修修改改,那不是很方便~~

而且,代码很简单:

Document doc = new Document(getMyDir() + "Document.doc");
Document clone = doc.deepClone();

哈哈,那不是可以给自己准备一些模板,然后直接克隆一下,再做修改?对了!就是这样!

下一篇,我将继续讲解一个核心的辅助类,DocumentBuilder。这个很好玩哦~~。Aspose.words编程指南之DocumentBuilder一

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值