Aspose.Words使用教程之插入文檔元素(一)
1.插入文本的字符串:
插入文本的字符串需要通過DocumentBuilder.Write方法插入到文檔。文本格式是由字體屬性決定,這個對象包含不同的字體屬性(字體名稱,字體大小,顏色,等等)。
一些重要的字體屬性也由[{ { DocumentBuilder } }]屬性允許您直接訪問它們。這些都是布爾屬性[{{Font.Bold}}],[{{Font.Italic}}], and [{{Font.Underline}}]。
注意字符格式設(shè)置將適用于所有插入的文本。
Example
使用DocumentBuilder插入格式化文本
c#
DocumentBuilder builder = new DocumentBuilder(); // Specify font formatting before adding text. Aspose.Words.Font font = builder.Font; font.Size = 16; font.Bold = true; font.Color = Color.Blue; font.Name = "Arial"; font.Underline = Underline.Dash; builder.Write("Sample text.");
Visual Basic
Dim builder As New DocumentBuilder() ' Specify font formatting before adding text. Dim font As Aspose.Words.Font = builder.Font font.Size = 16 font.Bold = True font.Color = Color.Blue font.Name = "Arial" font.Underline = Underline.Dash builder.Write("Sample text.")
2.插入一個段落
DocumentBuilder.Writeln可以插入一段文本的字符串也能添加一個段落。當前字體格式也是由DocumentBuilder所規(guī)定。字體屬性和當前段落格式是由DocumentBuilder.ParagraphFormat屬性所決定。
Example
如何添加一個段落到文檔
C#
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Specify font formatting Aspose.Words.Font font = builder.Font; font.Size = 16; font.Bold = true; font.Color = System.Drawing.Color.Blue; font.Name = "Arial"; font.Underline = Underline.Dash; // Specify paragraph formatting ParagraphFormat paragraphFormat = builder.ParagraphFormat; paragraphFormat.FirstLineIndent = 8; paragraphFormat.Alignment = ParagraphAlignment.Justify; paragraphFormat.KeepTogether = true; builder.Writeln("A whole paragraph.");
Visual Basic
Dim doc As New Document() Dim builder As New DocumentBuilder(doc) ' Specify font formatting Dim font As Aspose.Words.Font = builder.Font font.Size = 16 font.Bold = True font.Color = System.Drawing.Color.Blue font.Name = "Arial" font.Underline = Underline.Dash ' Specify paragraph formatting Dim paragraphFormat As ParagraphFormat = builder.ParagraphFormat paragraphFormat.FirstLineIndent = 8 paragraphFormat.Alignment = ParagraphAlignment.Justify paragraphFormat.KeepTogether = True builder.Writeln("A whole paragraph.")
3.插入一張表
使用DocumentBuilder創(chuàng)建一個表的基本算法是非常簡單的:
1.使用[{{DocumentBuilder.StartTable}}]啟動表;
2.使用[{{DocumentBuilder.InsertCell}}]插入單元格,這自動生成一個新行,如果需要,使用 [{{DocumentBuilder.CellFormat}}]屬性來指定單元格格式;
3.使用DocumentBuilder.methods寫入單元格內(nèi)容;
4.重復步驟2和3,直到行內(nèi)容寫完;
5.調(diào)用[{{DocumentBuilder.EndRow}}]來結(jié)束當前的行,如果需要,使用[{ { DocumentBuilder.RowFormat }}]屬性來指定行格式;
6.重復步驟2 - 5直到表完成;
7.調(diào)用[{{DocumentBuilder.EndTable}}]來完成表的創(chuàng)建。
Example
如何創(chuàng)建一個2行2列的格式化表格:
C#
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.StartTable(); // Insert a cell builder.InsertCell(); // Use fixed column widths. table.AutoFit(AutoFitBehavior.FixedColumnWidths); builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; builder.Write("This is row 1 cell 1"); // Insert a cell builder.InsertCell(); builder.Write("This is row 1 cell 2"); builder.EndRow(); // Insert a cell builder.InsertCell(); // Apply new row formatting builder.RowFormat.Height = 100; builder.RowFormat.HeightRule = HeightRule.Exactly; builder.CellFormat.Orientation = TextOrientation.Upward; builder.Writeln("This is row 2 cell 1"); // Insert a cell builder.InsertCell(); builder.CellFormat.Orientation = TextOrientation.Downward; builder.Writeln("This is row 2 cell 2"); builder.EndRow(); builder.EndTable();
Visual Basic
Dim doc As New Document() Dim builder As New DocumentBuilder(doc) Dim table As Table = builder.StartTable() ' Insert a cell builder.InsertCell() ' Use fixed column widths. table.AutoFit(AutoFitBehavior.FixedColumnWidths) builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center builder.Write("This is row 1 cell 1") ' Insert a cell builder.InsertCell() builder.Write("This is row 1 cell 2") builder.EndRow() ' Insert a cell builder.InsertCell() ' Apply new row formatting builder.RowFormat.Height = 100 builder.RowFormat.HeightRule = HeightRule.Exactly builder.CellFormat.Orientation = TextOrientation.Upward builder.Writeln("This is row 2 cell 1") ' Insert a cell builder.InsertCell() builder.CellFormat.Orientation = TextOrientation.Downward builder.Writeln("This is row 2 cell 2") builder.EndRow() builder.EndTable()