DocumentBuilder
时间: 2023-08-11 13:06:55 浏览: 44
DocumentBuilder 是 Aspose.Words API 中的一个类,用于逐步构建文档。与 Document 类不同,它并不需要一次性加载整个文档到内存中。您可以使用 DocumentBuilder 逐步构建文档,以避免内存溢出问题。以下是使用 DocumentBuilder 的示例代码:
1. 创建一个新文档并添加段落:
```
DocumentBuilder builder = new DocumentBuilder();
builder.writeln("Hello World!");
```
2. 在文档中插入图片:
```
DocumentBuilder builder = new DocumentBuilder();
Shape img = builder.insertImage("image.png");
img.setWidth(100);
img.setHeight(100);
```
3. 在文档中插入表格:
```
DocumentBuilder builder = new DocumentBuilder();
Table table = builder.startTable();
builder.insertCell();
builder.write("Cell 1");
builder.insertCell();
builder.write("Cell 2");
builder.endRow();
builder.insertCell();
builder.write("Cell 3");
builder.insertCell();
builder.write("Cell 4");
builder.endTable();
```
以上示例代码展示了使用 DocumentBuilder 创建文档、插入图片和表格的方式。您可以根据自己的需求使用 DocumentBuilder 逐步构建文档。使用 DocumentBuilder 可以有效地减少内存的使用,避免内存溢出问题。