Aspose. Words for Word operations, aspose. wordsword
Generally, word operations are performed on existing word Templates. You do not need to create them directly. There are many online tutorials. Let's take a look.
There are generally the following methods (if you forget the specific methods, the following is usually enough)
1. Use bookmarks to replace
As the name suggests, you need to define a bookmarks first, and then fill in the corresponding data or images in the bookmarks.
1> First insert the bookmarks on the word tab and define the name of the bookmarks.
2> use in a program
Document doc = new Document (tmppath); // load the template Aspose. words. documentBuilder builder = new Aspose. words. documentBuilder (doc); builder. moveToBookmark ("zy"); // jump to the position builder where the bookmark name is zy. write ("the text to be replaced by the bookmarks ");
The image is located at a position and pasted, as shown in the following figure.
builder.InsertImage(img, RelativeHorizontalPosition.Page, left, RelativeVerticalPosition.TopMargin, 0, width, height, WrapType.None);
The specific meaning of the parameter is clear.
2. replace with a table cell
Generally, tables are basically in the word template, So table operations are very important.
First find the corresponding table
Document doc = new Document (tmppath); // load the template Aspose. words. documentBuilder builder = new Aspose. words. documentBuilder (doc); NodeCollection allTables = doc. getChildNodes (NodeType. table, true); Table table = allTables [0] as Aspose. words. tables. table; // get the first Table
Then, the table is operated, but the cells in the default table are only readable, so I came up with A way to store cell A in cell B, then modify cell B, delete cell A, and add cell B to cell A. This perfectly replaces the cell, and the original cell width and height do not need to be adjusted, so I encapsulated it into a method example as follows:
/// <Summary> /// modify the data in the table /// </summary> /// <param name = "table"> table name </param> /// <param name = "doc"> document </param> /// <param name = "row"> modify row </param> /// <param name = "cell "> modify the column </param> /// <param name =" value "> modified value </param> private static Table EditCell (Table table, document doc, int row, int cell, string value) {Cell c = table. rows [row]. cells [cell]; Paragraph p = new Paragraph (doc); p. appendChild (new Run (doc, value); p. paragraphFormat. style. font. size = 10; p. paragraphFormat. style. font. name = "文 "; c. firstParagraph. remove (); c. appendChild (p); table. rows [row]. cells [cell]. remove (); table. rows [row]. cells. insert (cell, c); return table ;}
3> Delete the original cell, add a new cell, and then customize the property (high width)
This is not recommended, but I didn't think of a replacement for the method 2. It was an old version, but I 'd like to show it and remember my efforts.
The method is as follows:
Private static Aspose. words. tables. cell CreateCell (string value, Document doc, double cellwidth) {Aspose. words. tables. cell c1 = new Aspose. words. tables. cell (doc); c1.CellFormat. width = cellwidth; c1.CellFormat. borders. lineStyle = LineStyle. single; // c1.CellFormat. wrapText = false; Aspose. words. paragraph p = new Paragraph (doc); p. appendChild (new Run (doc, value); // Table table = new Table (doc); // p. appendChild (table); p. paragraphFormat. style. font. size = 10; p. paragraphFormat. style. font. name = "文 "; c1.AppendChild (p); return c1 ;}
4> adding data to cells is only applicable to tables. It is recommended and easy to use. Specifically, you can directly add values to a column in a row of a table. The example is as follows:
Builder. MoveToCell (0, 31, 1, 0); // jump to 0th tables, 31st rows, 1st columns builder. Write ("data to be written ");
Note: rows and columns start from 0, not from 1.
5> Replace the Domain Name
Similar to bookmarks, .... I won't, Baidu, right... Haha
Others: I am also going to make a small example, which will be uploaded later.