DocumentBuilder

DocumentBuilder class

提供插入文本、图像和其他内容、指定字体、段落和章节格式的方法。

要了解更多信息,请访问文档生成器概述文档文章。

public class DocumentBuilder

构造函数

姓名描述
DocumentBuilder()初始化此类的新实例。
DocumentBuilder(Document)初始化此类的新实例。
DocumentBuilder(DocumentBuilderOptions)初始化此类的新实例。
DocumentBuilder(DocumentDocumentBuilderOptions)初始化此类的新实例。

特性

姓名描述
Bold { get; set; }如果字体格式为粗体,则为真。
CellFormat { get; }返回表示当前表格单元格格式属性的对象。
CurrentNode { get; }获取此 DocumentBuilder 中当前选定的节点。
CurrentParagraph { get; }获取当前选中的段落DocumentBuilder.
CurrentSection { get; }获取当前选中的部分DocumentBuilder.
CurrentStory { get; }获取当前选中的故事DocumentBuilder.
CurrentStructuredDocumentTag { get; }获取当前选中的结构化文档标签DocumentBuilder.
Document { get; set; }获取或设置Document该对象所附加到的对象。
Font { get; }返回表示当前字体格式属性的对象。
IsAtEndOfParagraph { get; }返回真的如果光标位于当前段落的末尾。
IsAtEndOfStructuredDocumentTag { get; }返回真的如果光标位于结构化文档标签的末尾。
IsAtStartOfParagraph { get; }返回真的如果光标位于当前段落的开头(光标前没有文本)。
Italic { get; set; }如果字体格式为斜体,则为真。
ListFormat { get; }返回表示当前列表格式属性的对象。
PageSetup { get; }返回代表当前页面设置和部分属性的对象。
ParagraphFormat { get; }返回表示当前段落格式属性的对象。
RowFormat { get; }返回表示当前表行格式属性的对象。
Underline { get; set; }获取/设置当前字体的下划线类型。

方法

姓名描述
DeleteRow(int, int)从表中删除一行。
EndBookmark(string)将文档中的当前位置标记为书签结束。
EndColumnBookmark(string)将文档中的当前位置标记为列书签的结束位置。该位置必须位于表格单元格中。
EndEditableRange()将文档中的当前位置标记为可编辑范围的结束。
EndEditableRange(EditableRangeStart)将文档中的当前位置标记为可编辑范围的结束。
EndRow()结束文档中的表格行。
EndTable()结束文档中的表格。
InsertBreak(BreakType)在文档中插入指定类型的分隔符。
InsertCell()将表格单元格插入文档。
InsertChart(ChartType, double, double)将图表对象插入文档并将其缩放到指定大小。
InsertChart(ChartType, double, double, ChartStyle)将图表对象插入文档并将其缩放到指定大小。
InsertChart(ChartTypeRelativeHorizontalPosition, double, RelativeVerticalPosition, double, double, double, WrapType)将图表对象插入文档并将其缩放到指定大小。
InsertChart(ChartTypeRelativeHorizontalPosition, double, RelativeVerticalPosition, double, double, double, WrapTypeChartStyle)将图表对象插入文档并将其缩放到指定大小。
InsertCheckBox(string, bool, int)在当前位置插入复选框表单字段。
InsertCheckBox(string, bool, bool, int)在当前位置插入复选框表单字段。
InsertComboBox(string, string[], int)在当前位置插入组合框表单字段。
InsertDocument(DocumentImportFormatMode)在光标位置插入文档。
InsertDocument(DocumentImportFormatModeImportFormatOptions)在光标位置插入文档。
InsertDocumentInline(DocumentImportFormatModeImportFormatOptions)在光标位置插入内联文档。
InsertField(string)将 Word 字段插入文档并更新字段结果。
InsertField(FieldType, bool)将 Word 字段插入文档并选择性地更新字段结果。
InsertField(string, string)将 Word 字段插入文档而不更新字段结果。
InsertFootnote(FootnoteType, string)在文档中插入脚注或尾注。
InsertFootnote(FootnoteType, string, string)在文档中插入脚注或尾注。
InsertForms2OleControl(Forms2OleControl)插入Forms2OleControl对象进入当前位置。
InsertGroupShape(params ShapeBase[])将作为参数传递的形状分组到插入当前位置的新 GroupShape 节点中。
InsertGroupShape(double, double, double, double, params ShapeBase[])将作为参数传递的形状分组到指定大小的新 GroupShape 节点中,然后插入到指定位置。
InsertHorizontalRule()在文档中插入水平规则形状。
InsertHtml(string)将 HTML 字符串插入文档。
InsertHtml(string, bool)将 HTML 字符串插入文档。
InsertHtml(string, HtmlInsertOptions)在文档中插入 HTML 字符串。允许指定其他选项。
InsertHyperlink(string, string, bool)在文档中插入超链接。
InsertImage(byte[])将字节数组中的图像插入文档。图像以内联方式插入,且比例为 100%。
InsertImage(Image)插入来自 .NET 的图像Image 对象插入文档。图像以 100% 比例内联插入。
InsertImage(Stream)将流中的图像插入文档。图像以内联方式插入,且比例为 100%。
InsertImage(string)将文件或 URL 中的图片插入文档。图片以内联方式插入,且比例为 100%。
InsertImage(byte[], double, double)将字节数组中的内联图像插入文档并将其缩放到指定大小。
InsertImage(Image, double, double)从 .NET 插入内联图像Image 对象放入文档并将其缩放到指定大小。
InsertImage(Stream, double, double)将流中的内嵌图像插入文档并将其缩放到指定大小。
InsertImage(string, double, double)将文件或 URL 中的内嵌图像插入文档并将其缩放到指定大小。
InsertImage(byte[], RelativeHorizontalPosition, double, RelativeVerticalPosition, double, double, double, WrapType)在指定位置和大小插入字节数组中的图像。
InsertImage(Image, RelativeHorizontalPosition, double, RelativeVerticalPosition, double, double, double, WrapType)插入来自 .NET 的图像Image位于指定位置和大小的 对象。
InsertImage(Stream, RelativeHorizontalPosition, double, RelativeVerticalPosition, double, double, double, WrapType)将流中的图像插入到指定位置和大小。
InsertImage(string, RelativeHorizontalPosition, double, RelativeVerticalPosition, double, double, double, WrapType)从文件或 URL 中插入指定位置和大小的图像。
InsertNode(Node)在光标前插入一个节点。
InsertOleObject(Stream, string, bool, Stream)将嵌入的 OLE 对象从流插入到文档中。
InsertOleObject(string, bool, bool, Stream)将文件中嵌入或链接的 OLE 对象插入文档。使用文件扩展名检测 OLE 对象类型。
InsertOleObject(string, string, bool, bool, Stream)将文件中嵌入或链接的 OLE 对象插入文档。使用给定的 progID 参数检测 OLE 对象类型。
InsertOleObjectAsIcon(Stream, string, string, string)将嵌入的 OLE 对象作为图标从流插入文档。 允许指定图标文件和标题。使用给定的 progID 参数检测 OLE 对象类型。
InsertOleObjectAsIcon(string, bool, string, string)将嵌入或链接的 OLE 对象作为图标插入文档。 允许指定图标文件和标题。使用文件扩展名检测 OLE 对象类型。
InsertOleObjectAsIcon(string, string, bool, string, string)将嵌入或链接的 OLE 对象作为图标插入文档。 允许指定图标文件和标题。使用给定的 progID 参数检测 OLE 对象类型。
InsertOnlineVideo(string, double, double)将在线视频对象插入文档并将其缩放到指定大小。
InsertOnlineVideo(string, string, byte[], double, double)将在线视频对象插入文档并将其缩放到指定大小。
InsertOnlineVideo(string, RelativeHorizontalPosition, double, RelativeVerticalPosition, double, double, double, WrapType)将在线视频对象插入文档并将其缩放到指定大小。
InsertOnlineVideo(string, string, byte[], RelativeHorizontalPosition, double, RelativeVerticalPosition, double, double, double, WrapType)将在线视频对象插入文档并将其缩放到指定大小。
InsertParagraph()在文档中插入段落分隔符。
InsertShape(ShapeType, double, double)插入具有指定类型和大小的内联形状。
InsertShape(ShapeTypeRelativeHorizontalPosition, double, RelativeVerticalPosition, double, double, double, WrapType)插入具有指定位置、大小和文本换行类型的自由浮动形状。
InsertSignatureLine(SignatureLineOptions)在当前位置插入签名行。
InsertSignatureLine(SignatureLineOptionsRelativeHorizontalPosition, double, RelativeVerticalPosition, double, WrapType)在指定位置插入签名行。
InsertStructuredDocumentTag(SdtType)插入StructuredDocumentTag进入文档。
InsertStyleSeparator()将样式分隔符插入文档。
InsertTableOfContents(string)将 TOC(目录)字段插入文档。
InsertTextInput(string, TextFormFieldType, string, string, int)在当前位置插入文本表单字段。
MoveTo(Node)将光标移动到内联节点或段落末尾。
MoveToBookmark(string)将光标移动到书签。
MoveToBookmark(string, bool, bool)将光标更精确地移动到书签。
MoveToCell(int, int, int, int)将光标移动到当前部分的表格单元格。
MoveToDocumentEnd()将光标移动到文档末尾。
MoveToDocumentStart()将光标移动到文档的开头。
MoveToField(Field, bool)将光标移动到文档中的某个字段。
MoveToHeaderFooter(HeaderFooterType)将光标移动到当前节的页眉或页脚的开头。
MoveToMergeField(string)将光标移动到指定合并字段正前方的位置并删除合并字段。
MoveToMergeField(string, bool, bool)将合并字段移动到指定的合并字段。
MoveToParagraph(int, int)将光标移动到当前部分中的某个段落。
MoveToSection(int)将光标移动到指定部分正文的开头。
MoveToStructuredDocumentTag(int, int)将光标移动到当前部分的结构化文档标签。
MoveToStructuredDocumentTag(StructuredDocumentTag, int)将光标移动到结构化文档标签。
PopFont()检索先前保存在堆栈上的字符格式。
PushFont()将当前字符格式保存到堆栈上。
StartBookmark(string)将文档中的当前位置标记为书签的起始位置。
StartColumnBookmark(string)将文档中的当前位置标记为列书签的起始位置。该位置必须位于表格单元格中。
StartEditableRange()将文档中的当前位置标记为可编辑范围的开始。
StartTable()在文档中开始一个表格。
Write(string)在文档的当前插入位置插入一个字符串。
Writeln()在文档中插入段落分隔符。
Writeln(string)在文档中插入字符串和段落分隔符。

评论

DocumentBuilder使构建过程Document更容易。 Document是由节点树组成的复合对象,虽然可以将 content 节点直接插入树中,但这需要很好地理解树结构。 DocumentBuilder是复杂结构的“外观”Document并允许 快速轻松地插入内容和格式。

创建一个DocumentBuilder并将其与Document

DocumentBuilder有一个内部光标,当您调用时,文本将插入其中 WriteWritelnInsertBreak 和其他方法。您可以导航DocumentBuilder使用各种 MoveToXXX 方法将光标移动到文档中的不同 location 。

使用Font属性来指定将应用于 从文档中的当前位置开始插入的所有文本的字符格式。

使用ParagraphFormat属性来指定 current 和所有将插入的段落的段落格式。

使用PageSetup属性来指定 current 部分和将插入的所有部分的页面和部分属性。

使用CellFormatRowFormat属性来指定表格单元格和行的格式属性。用户InsertCelland EndRow建立表的方法。

注意FontParagraphFormatPageSetup每当您导航到文档中的其他位置时,属性都会更新,以反映新位置可用的格式属性。

例子

展示如何使用文档生成器创建表格。

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// 启动表格,然后用两个单元格填充第一行。
builder.StartTable();
builder.InsertCell();
builder.Write("Row 1, Cell 1.");
builder.InsertCell();
builder.Write("Row 1, Cell 2.");

// 调用构建器的“EndRow”方法来开始新行。
builder.EndRow();
builder.InsertCell();
builder.Write("Row 2, Cell 1.");
builder.InsertCell();
builder.Write("Row 2, Cell 2.");
builder.EndTable();

doc.Save(ArtifactsDir + "DocumentBuilder.CreateTable.docx");

展示如何使用 DocumentBuilder 在文档中创建页眉和页脚。

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// 指定我们希望第一页、偶数页和奇数页使用不同的页眉和页脚。
builder.PageSetup.DifferentFirstPageHeaderFooter = true;
builder.PageSetup.OddAndEvenPagesHeaderFooter = true;

// 创建页眉,然后向文档添加三页以显示每种页眉类型。
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
builder.Write("Header for the first page");
builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
builder.Write("Header for even pages");
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Header for all other pages");

builder.MoveToSection(0);
builder.Writeln("Page1");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Page2");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Page3");

doc.Save(ArtifactsDir + "DocumentBuilder.HeadersAndFooters.docx");

展示如何构建具有自定义边框的表格。

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.StartTable();

// 为文档生成器设置表格格式选项
// 将它们应用于我们添加的每一行和单元格。
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

builder.CellFormat.ClearFormatting();
builder.CellFormat.Width = 150;
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.CellFormat.Shading.BackgroundPatternColor = Color.GreenYellow;
builder.CellFormat.WrapText = false;
builder.CellFormat.FitText = true;

builder.RowFormat.ClearFormatting();
builder.RowFormat.HeightRule = HeightRule.Exactly;
builder.RowFormat.Height = 50;
builder.RowFormat.Borders.LineStyle = LineStyle.Engrave3D;
builder.RowFormat.Borders.Color = Color.Orange;

builder.InsertCell();
builder.Write("Row 1, Col 1");

builder.InsertCell();
builder.Write("Row 1, Col 2");
builder.EndRow();

// 更改格式将应用于当前单元格,
// 以及我们随后使用构建器创建的任何新单元格。
// 这不会影响我们之前添加的单元格。
builder.CellFormat.Shading.ClearFormatting();

builder.InsertCell();
builder.Write("Row 2, Col 1");

builder.InsertCell();
builder.Write("Row 2, Col 2");

builder.EndRow();

// 增加行高以适应垂直文本。
builder.InsertCell();
builder.RowFormat.Height = 150;
builder.CellFormat.Orientation = TextOrientation.Upward;
builder.Write("Row 3, Col 1");

builder.InsertCell();
builder.CellFormat.Orientation = TextOrientation.Downward;
builder.Write("Row 3, Col 2");

builder.EndRow();
builder.EndTable();

doc.Save(ArtifactsDir + "DocumentBuilder.InsertTable.docx");

也可以看看