文档生成器概述

DocumentBuilder是一个功能强大的类,它与Document相关联,使您能够从头开始构建动态文档或向现有文档添加新元素。

DocumentBuilder

文档生成器或Aspose.WordsDOM

DocumentBuilder

当直接使用Aspose.WordsDOM的类时,也可以使用DocumentBuilder进行操作。 但是,直接使用Aspose.WordsDOM类通常需要比使用DocumentBuilder更多的代码行。

文档导航

文档导航基于虚拟光标的概念,您可以使用各种DocumentBuilder.MoveToXXX方法(如MoveToDocumentStartMoveToField)将其移动到文档中的另一个位置。 此虚拟光标指示调用方法时文本将插入的位置Write, Writeln, InsertBreak, 和其他人。 请参阅以下文章"使用光标导航"以了解有关虚拟光标的更多信息。

下面的代码示例演示如何导航到书签:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(DocumentBuilderMoveToBookmarkEnd.class);
// Open the document.
Document doc = new Document(dataDir + "DocumentBuilder.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToBookmark("CoolBookmark", false, true);
builder.writeln("This is a very cool bookmark.");
doc.save(dataDir + "output.doc");

文档构建和修改

Aspose.WordsAPI提供了几个类,它们负责格式化文档的各种元素。 每个类都封装了与特定文档元素(如文本、段落、节等)相关的格式属性。 例如,Font类表示字符格式设置属性,ParagraphFormat类表示段落格式设置属性,等等。 这些类的对象由相应的DocumentBuilder属性返回,这些属性与类具有相同的名称。 因此,您可以在文档构建期间访问它们并设置所需的格式。

您还可以使用Write方法或任何DocumentBuilder.InsertXXX方法(如InsertFieldInsertHtml和类似方法)在光标位置插入文本、checkbox、ole对象、图像、书签、表单字段和其他文档元素。

让我们看看如何使用DocumentBuilder创建一个简单的文档。

使用DocumentBuilder创建文档

首先,您需要创建一个DocumentBuilder并将其与Document对象相关联。 您可以通过调用其构造函数来创建DocumentBuilder的新实例,并将其传递给Document对象以附加到构建器。

要插入文本,请将需要插入到文档中的文本字符串传递给Write方法。

下面的代码示例演示如何使用文档生成器创建简单文档。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(HelloWorld.class);
// Create a blank document.
Document doc = new Document();
// DocumentBuilder provides members to easily add content to a document.
DocumentBuilder builder = new DocumentBuilder(doc);
// Write a new paragraph in the document with the text "Hello World!"
builder.writeln("Hello World!");
// Save the document in DOCX format. The format to save as is inferred from the extension of the file name.
// Aspose.Words supports saving any document in many more formats.
doc.save(dataDir + "HelloWorld_out_.docx");

指定文档格式

Font属性定义文本格式。 此对象包含不同的字体属性(字体名称、字体大小、颜色等)。 一些重要的字体属性也由DocumentBuilder属性表示,以允许您直接访问它们。 这些是Font.BoldFont.ItalicFont.Underline布尔属性。

下面的代码示例演示如何使用DocumentBuilder插入格式化文本:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Open the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Font font = builder.getFont();
font.setSize(16);
font.setColor(Color.DARK_GRAY);
font.setBold(true);
font.setName("Algerian");
font.setUnderline(2);
ParagraphFormat paragraphFormat = builder.getParagraphFormat();
paragraphFormat.setFirstLineIndent(12);
paragraphFormat.setAlignment(1);
paragraphFormat.setKeepTogether(true);
builder.write("This is a sample Paragraph");
doc.save(dataDir + "InsertParagraph_out.doc");